Home » excel » excel – Concat macro not working, error in coding

excel – Concat macro not working, error in coding

Posted by: admin May 14, 2020 Leave a comment

Questions:

My code seems to not be working, and I’m not sure why?

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

Says that Compile error, Type mismatch and highlights the “i” in For i = 2

My objective:
In another sheet (Risk Partner Data) I have Columns F & E, these are a mixture of text and numbers. I want it to run for all of the active cells in the columns.

I’m new to vba.

How to&Answers:

i is being used as an integer in the For … Next but you’ve declared it as a string; it should be a Long. Same for ACParish.

There is a typo in AcrtiveWorkbook.

You don’t need to .Select a worksheet in order mto access it’s values.

Should ... = .Cells(i, 1) & .Cells(1, 2) be ... = .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