'错误1004'连接字符串时

时间:2015-09-08 17:50:42

标签: excel vba excel-formula

我已将字符串连接起来,将索引匹配公式粘贴到列的行中。每当我尝试运行这段代码时,我都会遇到Runtime Error' 1004,但我无法看到我的错误。这是我的代码:

Dim j As Long

'Loop down the rows in mainfile

For j = 2 To lastFullRow2



    Dim firstArgument As String
    firstArgument = "Sheet2!" & valuecolumnLetter & "2:" & valuecolumnLetter  & lastFullRow1 & ""
    'MsgBox "firstArgument" & firstArgument

    Dim secondArgument As String
    secondArgument = "Sheet2!" & parameter1columnLetter & "2:" & parameter1columnLetter & lastFullRow1 & ""
    'MsgBox "secondArgument " & secondArgument

    Dim thirdArgument As String
    thirdArgument = "Sheet2!" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ""
    'MsgBox "thirdArgument " & thirdArgument

    Dim fourthArgument As String
    fourthArgument = "Sheet2!" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ""
    'MsgBox "fourthArgument " & fourthArgument


    Dim condition3 As String
    condition3 = "Sheet3!" & "D2:" & D & j & ""
    'MsgBox "condition3 " & condition3


    Dim patid1 As String
    patid1 = "Sheet2!" & "D2:" & D & lastFullRow2 & ""
    'MsgBox "patid1 " & patid1

        With ws_mainfile


Dim commandstring As String

commandstring = "=INDEX(" & firstArgument & ",MATCH(1,(" & secondArgument & "=" & condition1 & ")*(" & thirdArgument & "=" & condition2 & ")*(" & patid1 & "=" & condition3 & "),0))"



ws_mainfile.Range("AN" & j).FormulaArray = commandstring



        End With

Next j

调试器说错误发生在ws_mainfile.Range ... =命令串行。

1 个答案:

答案 0 :(得分:3)

condition3 = "Sheet3!" & "D2:" & D & j & ""

patid1 = "Sheet2!" & "D2:" & D & lastFullRow2 & ""

您是否定义了变量D及其值是什么?

也许你的意思是:

condition3 = "Sheet3!" & "D2:D" & j 

patid1 = "Sheet2!" & "D2:D" & lastFullRow2 

也没有必要将空字符串连接到这些行的末尾。