Consulting

Results 1 to 9 of 9

Thread: Solved: concantonate cells using a loop

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: concantonate cells using a loop

    Can someone tell me if this code will work. It compiles but the result is nothing:

    [VBA] Sub concantonate()
    Dim i As Integer

    For i = 1 To 11
    If Cells(i, 2).Value = "inv" Then
    Cells(i, 2).value = Cells(i, 2).value & Cells(i, 3).value
    End If
    Next i
    End Sub [/VBA]
    Peace of mind is found in some of the strangest places.

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What's it meant to do?

    What values are actually in the cells?

    Have you tried stepping through it with F8?

    By the way since you haven't referenced a worksheet the Cells(x,y) will refer to the currently active sheet, which may or may not be the one you want.

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    cell (i, 3) has a value of INV and cell(i, 4) has the value of DATE I want cell (i, 3) to read INV DATE
    Peace of mind is found in some of the strangest places.

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    And have you tried stepping through it?

    By the way in the code you have columns 2 and 3, and in your latest post columns 3 and 4 are the only one's mentioned.

  5. #5
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks for the reply. I found a work around. BTW how can you loop through a range and concantonate a & b? ex a1 & b1
    Peace of mind is found in some of the strangest places.

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What exactly do you mean?

    Where do you want the concatenation to end up?
    [vba]

    Dim I As Long

    For I = 1 To 10
    Range("C" & I) = Range("A" & I) & Range("B" & I)
    Next I[/vba]

  7. #7
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks Norie. Solved
    Peace of mind is found in some of the strangest places.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by austenr
    Can someone tell me if this code will work. It compiles but the result is nothing:

    [VBA] Sub concantonate()
    Dim i As Integer

    For i = 1 To 11
    If Cells(i, 2).Value = "inv" Then
    Cells(i, 2).value = Cells(i, 2).value & Cells(i, 3).value
    End If
    Next i
    End Sub [/VBA]
    I am confused. This works fine for me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Austen, check out http://www.vbaexpress.com/kb/getarticle.php?kb_id=580 for a function to concatenate a range of cells
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •