Need help with a loop

EngSantiago

New Member
Joined
Mar 19, 2014
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello coders,

I'm working on macro do fix some columns and report but I'm stuck at the following. The below is an example of the report but depending on the report, it may have over 1K rows. The serial numbers on column 4 are associated with the Part number on column 2. That is, the serial numbers with level 2 on column 1 belong to the part number with level 1 above that is above the serial numbers. I would like to copy the data on columns 2 and 3 in the empty cells below for each serial number and later on delete all the rows with level 1. The amount of serial numbers per Part Number varies so I think a loop will do the trick but I just don't know how where to begin as I have never had to create a loop before.

LevelPart NumberQTYSerial Number
0555555555-009
1 777777777-0011 ea
204025C103K
204025C103L
204025C103M
1 666666666-0013 ea
212065C103K
212065C103L
212065C103M
212065C103N

The output should look like this:
LevelPart NumberQTYSerial Number
0
2 777777777-0011 ea04025C103K
2 777777777-0011 ea04025C103L
2 777777777-0011 ea04025C103M
2 666666666-0012 ea12065C103K
2 666666666-0012 ea12065C103L
2 666666666-0012 ea12065C103M
2 666666666-0012 ea12065C103N

Any help is appreciated. I'll be trying a couple of things in the mean time.

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
What about

Book1
ABCD
1LevelPart NumberQTYSerial Number
20555555555-009
31777777777-0011 ea
4204025C103K
5204025C103L
6204025C103M
71666666666-0012 ea
8212065C103K
9212065C103L
10212065C103M
11212065C103N
12
13
14LevelPart NumberQTYSerial Number
152777777777-0011 ea04025C103K
162777777777-0011 ea04025C103L
172777777777-0011 ea04025C103M
182666666666-0012 ea12065C103K
192666666666-0012 ea12065C103L
202666666666-0012 ea12065C103M
212666666666-0012 ea12065C103N
Sheet1
Cell Formulas
RangeFormula
B15:B21B15=IF(A15<>"",OFFSET(B2,AGGREGATE(14,6,($B$2:B4<>"")*(ROW($B$2:B4)),1)-ROW(B2),0),"")
C15:C21C15=IF(A15<>"",OFFSET(C2,AGGREGATE(14,6,($C$2:C4<>"")*(ROW($C$2:C4)),1)-ROW(C2),0),"")
 
Upvote 0
See if this (no looping) does what you want. Test with a copy of your data.

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Sub Rearrange()
  With ActiveSheet.UsedRange
    With .Columns(2).Resize(, 2)
      .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
      .Value = .Value
      .Rows(2).ClearContents
    End With
    .AutoFilter Field:=1, Criteria1:="1"
    .Offset(1).EntireRow.Delete
    .Parent.AutoFilterMode = False
  End With
End Sub
 
Upvote 0
See if this (no looping) does what you want. Test with a copy of your data.

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Sub Rearrange()
  With ActiveSheet.UsedRange
    With .Columns(2).Resize(, 2)
      .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
      .Value = .Value
      .Rows(2).ClearContents
    End With
    .AutoFilter Field:=1, Criteria1:="1"
    .Offset(1).EntireRow.Delete
    .Parent.AutoFilterMode = False
  End With
End Sub

This step here
VBA Code:
      .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
is filling in the empty cells on the column C (QTY) but not the but not the values on column B (part number).
 
Upvote 0
is filling in the empty cells on the column C (QTY) but not the but not the values on column B (part number).
So column B must not be completely empty. Can you tell us more about what is actually in those column B cells?
 
Upvote 0
They have some leading spaces.
So there could be more than one space character in any of those 'empty' cells.
Also, the term 'leading spaces' normally implies that something else follows those spaces. Is that so for your 'empty' cells?

How do the space characters get there in the first place? Can they be eliminated from the initial report?
 
Upvote 0
So there could be more than one space character in any of those 'empty' cells.
Also, the term 'leading spaces' normally implies that something else follows those spaces. Is that so for your 'empty' cells?

How do the space characters get there in the first place? Can they be eliminated from the initial report?
I was able to make get clean data and the code is working fine. Now, there is a change in gears. I'm not going to use the filter and delete functions from your code because I don't want to delete the higher levels. Instead, I inserted a new row on column A named "Next Higher Assembly". In this new column A, I want to call out the higher assembly above each line item. i.e. based on the below table cell A3 and A6 (for 777777777-001 and 777777777-002) should call out 555555555-009, cells A4 and A5 (for 666666666-001 and 666666666-002) should call out 777777777-001, A8 (for 444444444-002) should call out 444444444-001, and so on.

Next Higher AssemblyLevelPart NumberQTYSerial Number
0555555555-0091
1777777777-0011
2666666666-0011666666666-001
2666666666-0021666666666-002
1777777777-0024
2444444444-0011
3444444444-0021444444444-002
2777777777-0032
3888888888-0012888888888-001

Thanks for your help! Much appreciated.
 
Upvote 0
See if this is it.

VBA Code:
Sub NextHigher()
  With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
    .Formula = "=XLOOKUP(B2-1,B$1:B1,C$1:C1,"""",0,-1)"
    .Value = .Value
  End With
End Sub

My results:

EngSantiago.xlsm
ABCDE
1Next Higher AssemblyLevelPart NumberQTYSerial Number
20555555555-0091
3555555555-0091777777777-0011
4777777777-0012666666666-0011666666666-001
5777777777-0012666666666-0021666666666-002
6555555555-0091777777777-0024
7777777777-0022444444444-0011
8444444444-0013444444444-0021444444444-002
9777777777-0022777777777-0032
10777777777-0033888888888-0012888888888-001
Sheet1
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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