Consulting

Results 1 to 10 of 10

Thread: Looping Through Two Columns

  1. #1
    VBAX Tutor
    Joined
    Jan 2020
    Posts
    204
    Location

    Exclamation Looping Through Two Columns

    Hi,

    I have two columns:

    A.PNG

    What I am trying to do in Excel VBA is copy everything that's in column A into another sheet and then copy each cell on column B alongside each value in column A until everything in column B has been copied. Like this:

    b.jpg

    I don't know where to start. Please help

    I also posted here: https://superuser.com/questions/1544...s-in-excel-vba
    Last edited by jazz2409; 04-21-2020 at 03:58 AM.

  2. #2
    VBAX Tutor
    Joined
    Jan 2020
    Posts
    204
    Location
    Anyone??

  3. #3
    VBAX Tutor
    Joined
    Jan 2020
    Posts
    204
    Location
    Hello????

  4. #4
    VBAX Tutor
    Joined
    Jan 2020
    Posts
    204
    Location
    Can anyone answer please??????

  5. #5
    Try this code
    Sub Test() 
       Dim a, b, x, i As Long, j As Long, k As Long
        
        a = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value
        b = Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row).Value
        ReDim x(1 To UBound(a) * UBound(b), 1 To 2)
        For i = LBound(b) To UBound(b)
            For j = LBound(a) To UBound(a)
                k = k + 1
                x(k, 1) = a(j, 1)
                x(k, 2) = b(i, 1)
            Next j
        Next i
        Range("H1").Resize(UBound(x, 1), UBound(x, 2)).Value = x
    End Sub

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,887
    also cross posted without links AGAIN, at least at:
    https://www.mrexcel.com/board/thread...lumns.1131487/
    https://www.reddit.com/r/excel/comme...h_two_columns/
    but knowing this poster's history probably in many other places too.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    I've had my pacience tested... I'm negative.
    Semper in excretia sumus; solum profundum variat.

  8. #8
    VBAX Tutor
    Joined
    Jan 2020
    Posts
    204
    Location
    Quote Originally Posted by p45cal View Post
    also cross posted without links AGAIN, at least at:
    https://www.mrexcel.com/board/thread...lumns.1131487/
    https://www.reddit.com/r/excel/comme...h_two_columns/
    but knowing this poster's history probably in many other places too.
    I apologize I just forgot to edit my post and those links. I only posted here, Reddit, SuperUser, MrExcel. I deleted the one in SuperUser and Reddit hours ago. I would've deleted the one in MrExcel if there is an option to delete but there isn't.

    I had to post to 4 sites with hours or minutes in between each post because it was urgent. I was only given 3 hours to solve it or I will be fired. I've made a few projects since the last time I posted here but I am really having difficulties with looping statements. I am only 20 and a single mother of 2 living in Alabama. In our current situation I cannot lose my job. Losing my job means my children won't have anything to eat. I have no one to turn to. I hope you understand.

  9. #9
    Hi Jazz
    The idea is not to delete the threads from other forums. The idea is to share the links in all the threads so as not to waste others' time.
    Have you checked the code I posted yet?

  10. #10
    VBAX Tutor
    Joined
    Jan 2020
    Posts
    204
    Location
    Quote Originally Posted by YasserKhalil View Post
    Hi Jazz
    The idea is not to delete the threads from other forums. The idea is to share the links in all the threads so as not to waste others' time.
    Have you checked the code I posted yet?
    Yes, I have. Thank you for your example.
    I actually have 7 columns before the date column. That's what I am experimenting on now.

Tags for this Thread

Posting Permissions

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