Concatenate Each Row To Cells In Column

  • Hi all,


    I am trying to concatenate multiple lines of comments into one string and place that string in the comment field corresponding to the start of each record set


    I believe the code below is close, but I am receiving this error

    Quote

    Run-time error '91':
    Object variable or with block variable not set


    Debug points to this line

    Code
    rngComment = .Cells(intCounter, 5)


    I'm not sure what I am doing wrong.
    Can you please nudge me in the right direction?
    Many thanks!
    Regards,
    marc


  • Re: Concatenate Each Row To Cells In Column


    Thanks Dave,


    Code
    intCounter = 2
    .Cells(intCounter,5) = "71 - Vending Services"


    Thanks!
    Regards,
    marc

  • Re: Concatenate Each Row To Cells In Column


    Thanks Dave,


    I'm not receiving any more errors.
    But I definitely have a logic problem


    The final string is correct for the first record (Rows 2-4)
    But not the second record (Rows 5-6)


    Rows 3 and 4 are updating with comments from rows 5 and 6, respectively
    The intent is that rows 2,3,4 should concatentate and be placed in row 2 - which works


    Then 6 should be concatenated onto 5 and place in row 5 - this does not work
    Apparently I'm not updating intRecord correctly. After buggering with it for 2 hours I"m not sure how to.


    Any ideas?
    Updated code below


    Thanks much!
    Regards,
    marc


    bump
    Hi all,
    Does anyone have any ideas as to the code logic below
    I can't figure out what I'm doing wrong
    Thanks!
    marc


  • Re: Concatenate Each Row To Cells In Column


    Bump.


    Hi all,
    I've pulled out my hair (what little I had left)
    I just can't figure out what I'm doing wrong here.
    Any ideas?


    See most recent code post above.
    Many thanks,
    marc

  • Re: Concatenate Each Row To Cells In Column


    Hi,


    Try something like..


    [vba]Sub kTest()
    Dim i As Long, r As Long, strComm As String, strRng As Range
    r = Range("a" & Rows.Count).End(xlUp).Row
    Set strRng = Cells(2, 5)
    For i = 2 To r
    If Cells(i, 1) <> Cells(i + 1, 1) Then
    strComm = strComm & " " & Cells(i, 5)
    strRng = Mid$(strComm, 2)
    strComm = ""
    Set strRng = Cells(i + 1, 5)
    Else
    strComm = strComm & " " & Cells(i, 5)
    End If
    Next
    End Sub[/vba]


    HTH

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!