How to make the VBA code I have loop

ostrdevi

New Member
Joined
Apr 25, 2022
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
Hi, I am trying to write up a code that will go through this Excel sheet I have that has it's information split into individual columns. I have code that works but I have to manually change the range every time , I am really hoping there is a way to have this Loop or something that is faster. Here is the code I have right now and an Example of the Excel sheet I am working with. I had to change one of the values in the excel because I don't think I can show it by so I replaced it with the words, "fill". This should really affect anything because the values that are actually supposed to be there are three letters. Thank you in advance to anyone who takes the time to read this and or try and help with it, I really appreciate it.

1650925503678.png
1650925089305.png
 

Attachments

  • 1650925339319.png
    1650925339319.png
    35.3 KB · Views: 3
  • 1650925361583.png
    1650925361583.png
    28.4 KB · Views: 2
  • 1650925400053.png
    1650925400053.png
    28.4 KB · Views: 2

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello, and welcome to the forum!

I may be confused as to what the final output is that you're looking for. Could you provide some context?

  • Are you looking for column "O" to contain First+Mid+Last name for all records in your data set?
  • Do you always get First+Mid+Last from columns E/F/G or do you also need to grab it from B/D/C & J/K/L?
  • If you are trying to loop through EFG then BCD then JKL, where do you want the outputs of the loop to live? All in column O? Or in a different spot?
It would be helpful if you could provide a screenshot of what you would like the Excel to look like once the code has finished.

Thanks!
 
Upvote 0
Hello, and welcome to the forum!

I may be confused as to what the final output is that you're looking for. Could you provide some context?

  • Are you looking for column "O" to contain First+Mid+Last name for all records in your data set?
  • Do you always get First+Mid+Last from columns E/F/G or do you also need to grab it from B/D/C & J/K/L?
  • If you are trying to loop through EFG then BCD then JKL, where do you want the outputs of the loop to live? All in column O? Or in a different spot?
It would be helpful if you could provide a screenshot of what you would like the Excel to look like once the code has finished.

Thanks!
Thank you for responding so fast, I am trying to have it loop through ABC then EFG then IJK. All of the outputs going into O is fine, I just need all of the outputs in the same column. I went through and did ABC and put it in O, I would like all of the data that could be in ABC, EFG, and IJK to be added together kind of like this.
1650928147090.png
 
Upvote 0
Give this a shot:
VBA Code:
Sub FillName()

Dim i As Long
Dim j As Long
Dim StartClm As Integer
Dim jlrow As Long
Dim DestClm As Integer
Dim Destlrow As Long
Dim LoopAmt As Integer

DestClm = 15 '15 represents Column O - Which will be the destination of the macro
LoopAmt = Application.CountIf(Range("1:1"), "ULD")

If LoopAmt = 0 Then Exit Sub

For i = 1 To LoopAmt
    StartClm = (i * 4) - 3
    jlrow = Cells(Rows.Count, StartClm).End(xlUp).Row
    
    For j = 2 To jlrow
        Destlrow = Cells(Rows.Count, DestClm).End(xlUp).Row + 1
        Cells(Destlrow, DestClm) = Cells(j, StartClm + 1) & Cells(j, StartClm + 2) & Cells(j, StartClm + 3)
    Next j
  
Next i

End Sub

This will determine the number of times it needs to loop based off the # of times "ULD" is in row 1. This will loop through any number of rows you have (And you can have a different number of rows in each section (a-d, e-h, i-l, etc...). You can add as many sections as you'd like, just make sure to update the "15" to a different number if you'd like the results to be in a column other than "O".

Let me know if you need any additional help!
 
Upvote 0
Give this a shot:
VBA Code:
Sub FillName()

Dim i As Long
Dim j As Long
Dim StartClm As Integer
Dim jlrow As Long
Dim DestClm As Integer
Dim Destlrow As Long
Dim LoopAmt As Integer

DestClm = 15 '15 represents Column O - Which will be the destination of the macro
LoopAmt = Application.CountIf(Range("1:1"), "ULD")

If LoopAmt = 0 Then Exit Sub

For i = 1 To LoopAmt
    StartClm = (i * 4) - 3
    jlrow = Cells(Rows.Count, StartClm).End(xlUp).Row
   
    For j = 2 To jlrow
        Destlrow = Cells(Rows.Count, DestClm).End(xlUp).Row + 1
        Cells(Destlrow, DestClm) = Cells(j, StartClm + 1) & Cells(j, StartClm + 2) & Cells(j, StartClm + 3)
    Next j
 
Next i

End Sub

This will determine the number of times it needs to loop based off the # of times "ULD" is in row 1. This will loop through any number of rows you have (And you can have a different number of rows in each section (a-d, e-h, i-l, etc...). You can add as many sections as you'd like, just make sure to update the "15" to a different number if you'd like the results to be in a column other than "O".

Let me know if you need any additional help!
Thank you so much, This worked amazingly but it using Columns BCD for example and I need it to use ABC, is it possible to change this? It is outputting 0350fill506 and using the Tare column instead of the ULD column when it outputs. The output I am hoping for is AAX0350fill. Thanks again, I hope this doesn't come across as rude, I don't mean it to.
 
Upvote 0
Not rude at all, and that's totally on me, I should have read closer what you were asking for. Here's the update:
VBA Code:
Sub FillName()

Dim i As Long
Dim j As Long
Dim StartClm As Integer
Dim jlrow As Long
Dim DestClm As Integer
Dim Destlrow As Long
Dim LoopAmt As Integer

DestClm = 15 '15 represents Column O - Which will be the destination of the macro
LoopAmt = Application.CountIf(Range("1:1"), "ULD")

If LoopAmt = 0 Then Exit Sub

For i = 1 To LoopAmt
    StartClm = (i * 4) - 3
    jlrow = Cells(Rows.Count, StartClm).End(xlUp).Row
    
    For j = 2 To jlrow
        Destlrow = Cells(Rows.Count, DestClm).End(xlUp).Row + 1
        Cells(Destlrow, DestClm) = Cells(j, StartClm) & Cells(j, StartClm + 1) & Cells(j, StartClm + 2)
    Next j
  
Next i

End Sub
 
Upvote 0
Not rude at all, and that's totally on me, I should have read closer what you were asking for. Here's the update:
VBA Code:
Sub FillName()

Dim i As Long
Dim j As Long
Dim StartClm As Integer
Dim jlrow As Long
Dim DestClm As Integer
Dim Destlrow As Long
Dim LoopAmt As Integer

DestClm = 15 '15 represents Column O - Which will be the destination of the macro
LoopAmt = Application.CountIf(Range("1:1"), "ULD")

If LoopAmt = 0 Then Exit Sub

For i = 1 To LoopAmt
    StartClm = (i * 4) - 3
    jlrow = Cells(Rows.Count, StartClm).End(xlUp).Row
   
    For j = 2 To jlrow
        Destlrow = Cells(Rows.Count, DestClm).End(xlUp).Row + 1
        Cells(Destlrow, DestClm) = Cells(j, StartClm) & Cells(j, StartClm + 1) & Cells(j, StartClm + 2)
    Next j
 
Next i

End Sub
this works perfectly, Thank you so much for all of your help. I really appreciate it! I can't put into words how much I really do appreciate it. You are awesome!
 
Upvote 0
Glad I could help!
is it possible to have the code also grab the tare weight and put it in the cell next to the first output? so the final output for example would be, AAX0350fill in O2 and 506 in P2 because 506 is associated with the can's name, AAX0350?
1650938036009.png
 
Upvote 0
Try this:
VBA Code:
Sub FillName()

Dim i As Long
Dim j As Long
Dim StartClm As Integer
Dim jlrow As Long
Dim DestClm As Integer
Dim Destlrow As Long
Dim LoopAmt As Integer

DestClm = 15 '15 represents Column O - Which will be the destination of the macro
LoopAmt = Application.CountIf(Range("1:1"), "ULD")

If LoopAmt = 0 Then Exit Sub

For i = 1 To LoopAmt
    StartClm = (i * 4) - 3
    jlrow = Cells(Rows.Count, StartClm).End(xlUp).Row
    
    For j = 2 To jlrow
        Destlrow = Cells(Rows.Count, DestClm).End(xlUp).Row + 1
        Cells(Destlrow, DestClm) = Cells(j, StartClm) & Cells(j, StartClm + 1) & Cells(j, StartClm + 2)
        Cells(Destlrow, DestClm + 1) = Cells(j, StartClm + 3)
    Next j
  
Next i

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top