以某种方式替换公式
Make a replacement in formulas in a certain way
我在工作簿中有多个公式 =VLOOKUP()
。现在他们看起来像这样:
=VLOOKUP(CELL_REFERENCE;BOOK_NAME_AND_COLUMNS;COLUMN_NUMBER;0).
我需要使用 VBA 或 Excel 的内置函数找到一个解决方案,以替换所有 VLOOKUP 公式,使其看起来像这样,但保留所有单元格引用和插入的数字:
=VLOOKUP(VALUE(CELL_REFERENCE);BOOK_NAME_AND_COLUMNS;COLUMN_NUMBER;0)
CELL_REFERENCE、BOOK_NAME_AND_COLUMNS、COLUMN_NUMBER 可以因公式而异,例如:
CELL_REFERENCE - A1,
BOOK_NAME_AND_COLUMNS - LIB!$A:$J;
COLUMN_NUMBER - 3.
我试过使用通配符替换内置函数,但是没有用。有没有什么方法可以使用 VBA 或 excel 内置函数,我不知道?
修改所有工作表中的公式
Option Explicit
Sub ReplaceVLookup()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet
Dim rg As Range
Dim fCell As Range
Dim FirstAddress As String
Dim cPos As Long ' comma
Dim lpPos As Long ' left parentheses
Dim fString As String
Dim lString As String
Dim rString As String
Application.ScreenUpdating = False
For Each ws In wb.Worksheets
Set rg = ws.UsedRange
Set fCell = rg.Find("=VLOOKUP(*", , xlFormulas, xlWhole)
If Not fCell Is Nothing Then
FirstAddress = fCell.Address
Do
fString = fCell.Formula
If InStr(fString, "=VLOOKUP(VALUE") <> 1 Then
cPos = InStr(fString, ",")
lString = Left(fString, cPos - 1)
rString = Right(fString, Len(fString) - cPos + 1)
lpPos = InStr(fString, "(")
fCell.Formula = Left(lString, lpPos) & "VALUE(" _
& Mid(lString, lpPos + 1, cPos - lpPos) & ")" & rString
End If
Set fCell = rg.FindNext(fCell)
Loop Until fCell.Address = FirstAddress
End If
Next ws
Application.ScreenUpdating = True
MsgBox """VALUE"" added.", vbInformation
End Sub
我在工作簿中有多个公式 =VLOOKUP()
。现在他们看起来像这样:
=VLOOKUP(CELL_REFERENCE;BOOK_NAME_AND_COLUMNS;COLUMN_NUMBER;0).
我需要使用 VBA 或 Excel 的内置函数找到一个解决方案,以替换所有 VLOOKUP 公式,使其看起来像这样,但保留所有单元格引用和插入的数字:
=VLOOKUP(VALUE(CELL_REFERENCE);BOOK_NAME_AND_COLUMNS;COLUMN_NUMBER;0)
CELL_REFERENCE、BOOK_NAME_AND_COLUMNS、COLUMN_NUMBER 可以因公式而异,例如:
CELL_REFERENCE - A1,
BOOK_NAME_AND_COLUMNS - LIB!$A:$J;
COLUMN_NUMBER - 3.
我试过使用通配符替换内置函数,但是没有用。有没有什么方法可以使用 VBA 或 excel 内置函数,我不知道?
修改所有工作表中的公式
Option Explicit
Sub ReplaceVLookup()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet
Dim rg As Range
Dim fCell As Range
Dim FirstAddress As String
Dim cPos As Long ' comma
Dim lpPos As Long ' left parentheses
Dim fString As String
Dim lString As String
Dim rString As String
Application.ScreenUpdating = False
For Each ws In wb.Worksheets
Set rg = ws.UsedRange
Set fCell = rg.Find("=VLOOKUP(*", , xlFormulas, xlWhole)
If Not fCell Is Nothing Then
FirstAddress = fCell.Address
Do
fString = fCell.Formula
If InStr(fString, "=VLOOKUP(VALUE") <> 1 Then
cPos = InStr(fString, ",")
lString = Left(fString, cPos - 1)
rString = Right(fString, Len(fString) - cPos + 1)
lpPos = InStr(fString, "(")
fCell.Formula = Left(lString, lpPos) & "VALUE(" _
& Mid(lString, lpPos + 1, cPos - lpPos) & ")" & rString
End If
Set fCell = rg.FindNext(fCell)
Loop Until fCell.Address = FirstAddress
End If
Next ws
Application.ScreenUpdating = True
MsgBox """VALUE"" added.", vbInformation
End Sub