0

我有一个三步过程,最终应该给我布尔值 True/False 以及偶尔的 #N/A 或 #VALUE (我实际上希望将其保留为错误)。我正在使用具有多个命名工作表的工作簿,并通过 VLookup 从一个选项卡中提取单元格值,替换这些值中的字符串,然后将这些值放入要评估的公式中。这是我到目前为止所拥有的;我在代码中添加了注释,解释了我卡在哪里。

Public Sub DetermineRowsToExamine()

'Define what our Rows are for the calculations
    Dim NumRecords As Long
    NumRecords = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").Range("B" & Rows.Count).End(xlUp).Row
    Dim CellsForFormula As Range
    Set CellsForFormula = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").Range("g2", "G" & NumRecords)


'Now I Insert the VLookup
    Dim WSLogic As Worksheet
    Dim WSData As Worksheet
    Set WSData = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data")
    Set WSLogic = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Logic Statements")

    CellsForFormula.Value = Application.WorksheetFunction.VLookup(WSData.Range("B2"), WSLogic.Range("A:D"), 4, False)

'This works in principle, but the problem is the "B2" in the VLookup - I need the "B2" to change to "B3" related
'to each row, just as it would if I pasted the rows down the columns as an cell formula

'Now I want to take that value and perform a replacement:
    CellsForFormula.Value = Application.WorksheetFunction.Substitute(Range("g2"), "ZZZ", "C2")

'Again, this works great, but I need it to replace "G2" or "G3" or whatever cell it's in.

'Finally, I then want to evaluate that cell as if it were a formula. When the above calculations are working,
'I end up with:  AND(LEN(C2)=10,OR(LEFT(C2,2)="57",LEFT(C2,2)="13"))
'I want to evaluate this as a formula, basically making it =AND(LEN(C2)=10,OR(LEFT(C2,2)="57",LEFT(C2,2)="13"))


End Sub

我想我只是不理解的是如何让 VLookup 和 Substitute 函数中的 Cell 引用与我所在的任何行相关。

4

1 回答 1

0

同意@AlanWaage 的说法,您应该将实际VLOOKUP公式放入单元格中。然后,要让公式相对于其位置进行更新,您可以使用复制和粘贴。最后,一旦你有了公式,你就可以复制和粘贴值,并根据现在CellsForFormula范围内的值进行替换。请参阅下面的更新代码:

' Put the formula in the first cell of your range
CellsForFormula(1, 1).Formula = _
    "=VLOOKUP(Paste Daily Data!B2,Logic Statements!$A$1:$D$10000,4,0)"

' Copy the formula to the rest of your range
CellsForFormula(1, 1).Copy _
    Destination:=Range(CellsForFormula(2, 1), CellsForFormula(NumRecords - 1, 1))

' Copy and paste values to just get the values in your range
CellsForFormula.Copy
CellsForFormula.PasteSpecial Paste:=xlPasteValues

' Execute your replacement
for each cell in CellsForFormula
    cell.Replace("ZZZ", cell.offset(0,-4).Address)
    'Use the line below instead of the line above if you only
    'want to replace if the entire value is "ZZZ"
    'cell.Replace("ZZZ", cell.offset(0,-4).Address,xlWhole)
    cell.Formula = "=" & cell.value
Next cell

- -编辑 - -

请注意,我做了Logic Statements!$A$1:$D$10000而不是Logic Statements!A:D; 这比让 Excel 检查 A:D 列中的所有行要快得多。根据需要使此数字更大或更小,但除非必要,否则应尽量避免在公式中使用完整的列/行引用

于 2013-10-23T17:29:50.317 回答