Concat宏不起作用,编码错误

时间:2018-09-07 03:53:30

标签: excel vba excel-vba

我的代码似乎无法正常工作,我不确定为什么吗?

Sub Concat()

'Formula to combine the member AC# and Parish Name
Sheets("Risk Partner Data").Select
    Dim ACParish As String, i As String
    Dim rng As Range
    Set rng = Range("A" & Rows.Count).End(x1Up)
    ACParish = rng.Row
    For i = 2 To ACParish
        AcrtiveWorkbook.Sheets("Calc Data").Cells(i, 1) = Cells(i, 1) & Cells(1, 2)
    Next i
End Sub

表示编译错误,类型不匹配,并在For i = 2中突出显示“ i”

我的目标: 在另一张表(风险合作伙伴数据)中,我有 F E 列,这些列是文本和数字的组合。我希望它为列中的所有活动单元格运行。

我是vba的新手。

1 个答案:

答案 0 :(得分:2)

i在For ... Next中被用作整数,但是您已将其声明为字符串;它应该是一个Long。与ACParish相同。

AcrtiveWorkbook中有一个错字。

您不需要。选择工作表以访问其值。

... = .Cells(i, 1) & .Cells(1, 2)应该是... = .Cells(i, 1) & .Cells(i, 2)吗?

Sub Concat()

'Formula to combine the member AC# and Parish Name
    Dim ACParish As long, i As long

    with workSheets("Risk Partner Data")
        ACParish  = .Range("A" & Rows.Count).End(xlUp).row
        For i = 2 To ACParish
            .parent.workSheets("Calc Data").Cells(i, 1) = .Cells(i, 1) & .Cells(1, 2)
        Next i
    end with

End Sub