PDA

View Full Version : Looping Through Two Columns



jazz2409
04-21-2020, 03:23 AM
Hi,

I have two columns:

26400

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:

26401

I don't know where to start. Please help :(

I also posted here: https://superuser.com/questions/1544090/looping-through-two-columns-in-excel-vba

jazz2409
04-21-2020, 05:16 AM
Anyone??

jazz2409
04-21-2020, 05:28 AM
Hello????

jazz2409
04-21-2020, 05:31 AM
Can anyone answer please??????

YasserKhalil
04-21-2020, 06:23 AM
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

p45cal
04-21-2020, 07:19 AM
also cross posted without links AGAIN, at least at:
https://www.mrexcel.com/board/threads/looping-through-two-columns.1131487/
https://www.reddit.com/r/excel/comments/g5ddhf/looping_through_two_columns/
but knowing this poster's history probably in many other places too.

paulked
04-21-2020, 07:36 AM
I've had my pacience tested... I'm negative.

jazz2409
04-21-2020, 11:24 PM
also cross posted without links AGAIN, at least at:
https://www.mrexcel.com/board/threads/looping-through-two-columns.1131487/
https://www.reddit.com/r/excel/comments/g5ddhf/looping_through_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.

YasserKhalil
04-21-2020, 11:54 PM
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?

jazz2409
04-22-2020, 08:51 AM
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.