0

我为单元格和工作表添加了循环,但出现语法错误。

Sub check() 
Dim ws As Worksheet 
Dim i As Long 
For Each ws In Worksheets 

For i = 5 To 10

ws.Range("F" & i).Formula = _
 "=VLookup((CONCATENATE(C1,"" "",C5)),'C:\Documents[TestData.xlsx]Sheet1'!$A$2:$G$28,7, FALSE)"
Sheets("Sheet1").Range("F5").Value = Sheets("Sheet1").Range("F5").Value

Next
End Sub
4

1 回答 1

0

这是你正在尝试的吗?

Sub check()
    Sheets("Sheet1").Range("F5").Formula = _
    "=VLookup((CONCATENATE(C1,"" "",C5)),'C:\Documents[TestData.xlsx]Sheet1'!$A$2:$G$28,7, FALSE)"
    Sheets("Sheet1").Range("F5").Value = Sheets("Sheet1").Range("F5").Value
End Sub

跟进

要将其放入循环中,您必须将行存储在变量中然后使用它。例如

Range("F5")也可以写成Range("F" & "5")

所以现在你所要做的就是继续5循环替换。请参阅下面的示例

Sub check()
    Dim i As Long

    '~~> From Row 5 to row 10
    '~~> Chnage as applicable
    For i = 5 To 10
        Sheets("Sheet1").Range("F" & i).Formula = _
        "=VLookup((CONCATENATE(C1,"" "",C" & i & _
        ")),'C:\Documents[TestData.xlsx]Sheet1'!$A$2:$G$28,7, FALSE)"

        Sheets("Sheet1").Range("F" & i).Value = Sheets("Sheet1").Range("F" & i).Value
    Next i
End Sub
于 2012-08-13T18:49:25.067 回答