Direction for a VBA Logic loop to clean data

smd747

Board Regular
Joined
Apr 24, 2011
Messages
214
I have 159740 lines of data that look simular to this below, item number the next line or several lines locations numbers.
I am think possibly some kind of a loop but looking for a starting direction. to put the data into a table file to work with. Any thoughts how to do this??

Thanks

Sheet1

ABCD
1Total For Unit of Measure:8383
2 XH3G 3" DUAL TITE GASKETS XH (75PK 342
3 100 100 521 502 4 700 14 1 00004519329EA6363
4Total For Unit of Measure:6363
5 XH4G 4" DUAL TITE GASKETS XH (60PK 343
6 100 100 500 503 2 4 3 1 00004236988EA6565
7Total For Unit of Measure:6565
8 XH5G 5" DUAL TITE GASKETS XH (30PKQ 344
9 100 100 621 602 10 600 13 1 00004598010EA4343
10Total For Unit of Measure:4343
11 XH8G 8" DUAL TITE GASKETS XH (20PK 346
12 100 100 500 503 12 1 4 1 00003603261EA2020
13 100 100 500 503 12 1 4 1 00003603261EA1010
14

<COLGROUP> <COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 354px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"> </COLGROUP><TBODY>
</TBODY>


Excel tables to the web >> Excel Jeanie HTML 4
 
smd747,

We now have two raw data worksheets, with two different structures:


Excel 2007
ABCD
1Product.......... Description................ Cmx Whs Zone Aisle Bay Level Pos Slot Location.........on handAvailableItem...
2Cmx Whs Zone Aisle Bay Level Pos Slot Location.......UomOn Hand..Available
3SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5
4100 100 851 851 2 1 45 1 00002152007EA66
5Total For Unit of Measure:66
Stock Status Report (3)



Excel 2007
ABCD
1Cmx Whs Zone Aisle Bay Level Pos Slot Location.......UomOn Hand..Available
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5
3100 100 851 851 2 1 45 1 00002152007EA66
4Total For Unit of Measure:66
Stock Status Report



Sample raw data on worksheet Stock Status Report:


Excel 2007
ABCD
1Cmx Whs Zone Aisle Bay Level Pos Slot Location.......UomOn Hand..Available
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5
3100 100 851 851 2 1 45 1 00002152007EA66
4Total For Unit of Measure:66
5SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6
6100 100 851 851 2 1 28 1 00002151951EA4646
7Total For Unit of Measure:4646
8SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8
9100 100 010 VENDOR RETURNSEA77
10Total For Unit of Measure:77
11SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15
12100 100 850 850 1 1 6 1 00001203349EA124124
13100 100 850 850 1 1 6 1 00001203350EA123123
14100 100 850 850 1 1 6 1 00001203351EA111111
15100 100 850 850 1 1 6 1 00001203352EA2323
16
Stock Status Report


After the new macro designed for the Stock Status Report worksheet:


Excel 2007
ABCDE
1ProductUomOn Hand..AvailableCmx Whs Zone Aisle Bay Level Pos Slot Location.......
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5EA66100 100 851 851 2 1 45 1 00002152007
3SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6EA4646100 100 851 851 2 1 28 1 00002151951
4SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8EA77100 100 010 VENDOR RETURNS
5SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15EA124124100 100 850 850 1 1 6 1 00001203349
6
Stock Status Report


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub ReorgDataStockStatusReport()
' hiker95, 01/12/2014, ME749822
Dim oa As Variant, na As Variant
Dim i As Long, ii As Long
Dim lr As Long, n As Long
Sheets("Stock Status Report").Activate
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
oa = Range("A1:D" & lr)
n = Application.CountIf(Columns(2), "EA")
ReDim na(1 To n, 1 To 5)
For i = 2 To UBound(oa, 1) - 1
  If oa(i, 2) = "" And oa(i + 1, 2) = "EA" Then
    ii = ii + 1
    na(ii, 1) = oa(i, 1)
    na(ii, 2) = oa(i + 1, 2)
    na(ii, 3) = oa(i + 1, 3)
    na(ii, 4) = oa(i + 1, 4)
    na(ii, 5) = oa(i + 1, 1)
  End If
Next i
Range("A1:D" & lr).ClearContents
Range("A1").Resize(, 5).Value = Array("Product", "Uom", "On Hand..", "Available", "Cmx Whs Zone Aisle Bay Level Pos Slot Location.......")
Range("A2").Resize(UBound(na, 1), UBound(na, 2)) = na
Columns.AutoFit
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgDataStockStatusReport macro.
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I tried the box.com and been attempting to get it to work for an hour. Signed up can't get the shared link to work.
I tried in skydrive try this link http://sdrv.ms/1ahIDNW

get a subscript out of range on "ReDim na(1 To n, 1 To 5)"

thanks again for working through this with me
 
Upvote 0
smd747,

The data in column B that contains the EA's actually has a leading space, like this " EA".

Before I continue any further, please verify that the EA's have a leading space character.
 
Upvote 0
smd747,

The data in column B that contains the EA's actually has a leading space, like this " EA".

The data in column B that contains the blank looking cells actually contains a space character, like this " ".


Sample raw data in worksheet Stock Status Report (not all of the 36,201 rows are shown for brevity):


Excel 2007
ABCD
1Cmx Whs Zone Aisle Bay Level Pos Slot Location.......UomOn Hand..Available
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5
3100 100 851 851 2 1 45 1 00002152007EA66
4Total For Unit of Measure:66
5SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6
6100 100 851 851 2 1 28 1 00002151951EA4646
7Total For Unit of Measure:4646
8SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8
9100 100 010 VENDOR RETURNSEA77
10Total For Unit of Measure:77
11SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15
12100 100 850 850 1 1 6 1 00001203349EA124124
13Total For Unit of Measure:124124
14SVP210SH LEN 2X10 FT SRV WGT SH SOIL PP 16
15100 100 850 850 1 1 3 1 00001203346EA3939
16Total For Unit of Measure:3939
17SVP35SH LEN 3X5 FT SRV WGT SH SOIL PIP 17
18100 100 850 850 1 1 5 1 00001203348EA9898
19Total For Unit of Measure:9898
20SVP310SH LEN 3X10 FT SRV WGT SH SOIL PP 18
21100 100 850 850 1 1 2 1 00001203269EA5656
22Total For Unit of Measure:5656
23SVP45SH LEN 4X5 FT SRV WGT SH SOIL PIP 19
24100 100 850 850 1 1 4 1 00001203347EA190190
25100 100 850 850 1 1 4 1 00004708459EA10
26Total For Unit of Measure:191190
Stock Status Report


After the macro in worksheet Stock Status Report (not all of the 10,545 rows of results are shown for brevity):


Excel 2007
ABCDE
1ProductUomOn Hand..AvailableCmx Whs Zone Aisle Bay Level Pos Slot Location.......
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5EA66100 100 851 851 2 1 45 1 00002152007
3SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6EA4646100 100 851 851 2 1 28 1 00002151951
4SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8EA77100 100 010 VENDOR RETURNS
5SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15EA124124100 100 850 850 1 1 6 1 00001203349
6SVP210SH LEN 2X10 FT SRV WGT SH SOIL PP 16EA3939100 100 850 850 1 1 3 1 00001203346
7SVP35SH LEN 3X5 FT SRV WGT SH SOIL PIP 17EA9898100 100 850 850 1 1 5 1 00001203348
8SVP310SH LEN 3X10 FT SRV WGT SH SOIL PP 18EA5656100 100 850 850 1 1 2 1 00001203269
9SVP45SH LEN 4X5 FT SRV WGT SH SOIL PIP 19EA190190100 100 850 850 1 1 4 1 00001203347
Stock Status Report



The macro run time to produce the above screenshot was 0.188 seconds.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub ReorgDataStockStatusReportV2()
' hiker95, 01/12/2014, ME749822
Dim oa As Variant, na As Variant
Dim i As Long, ii As Long
Dim lr As Long, n As Long
Sheets("Stock Status Report").Activate
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
oa = Range("A1:D" & lr)
n = Application.CountIf(Columns(2), " EA")
ReDim na(1 To n, 1 To 5)
For i = 2 To UBound(oa, 1) - 1
  If oa(i, 2) = " " And oa(i + 1, 2) = " EA" Then
    ii = ii + 1
    na(ii, 1) = oa(i, 1)
    na(ii, 2) = oa(i + 1, 2)
    na(ii, 3) = oa(i + 1, 3)
    na(ii, 4) = oa(i + 1, 4)
    na(ii, 5) = oa(i + 1, 1)
  End If
Next i
Range("A1:D" & lr).ClearContents
Range("A1").Resize(, 5).Value = Array("Product", "Uom", "On Hand..", "Available", "Cmx Whs Zone Aisle Bay Level Pos Slot Location.......")
Range("A2").Resize(UBound(na, 1), UBound(na, 2)) = na
Columns.AutoFit
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgDataStockStatusReportV2 macro.


In the future I would suggest that you start with a screenshot of your actual raw data, and, supply a screenshot of what the results should look like (manually formatted by you). That way we probably can find a solution on the first go.
 
Last edited:
Upvote 0
Wow, lighting fast and perfect. trying to understand and learn from your code. I had so many test sheets and workbooks trying to figure this out but your correct you get lost sometimes and I will
"In the future I would suggest that you start with a screenshot of your actual raw data, and, supply a screenshot of what the results should look like (manually formatted by you). That way we probably can find a solution on the first go. "

hiker95 thanks for a great solution and for your time and help educating me. Thank you smd747
 
Upvote 0
smd747,

I just added a few lines of code to the macro to trim all the text data in the worksheet.

Sample raw data:


Excel 2007
ABCD
1Cmx Whs Zone Aisle Bay Level Pos Slot Location.......UomOn Hand..Available
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5
3100 100 851 851 2 1 45 1 00002152007EA66
4Total For Unit of Measure:66
5SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6
6100 100 851 851 2 1 28 1 00002151951EA4646
7Total For Unit of Measure:4646
8SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8
9100 100 010 VENDOR RETURNSEA77
10Total For Unit of Measure:77
11SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15
12100 100 850 850 1 1 6 1 00001203349EA124124
13Total For Unit of Measure:124124
14SVP210SH LEN 2X10 FT SRV WGT SH SOIL PP 16
15100 100 850 850 1 1 3 1 00001203346EA3939
16Total For Unit of Measure:3939
17SVP35SH LEN 3X5 FT SRV WGT SH SOIL PIP 17
18100 100 850 850 1 1 5 1 00001203348EA9898
19Total For Unit of Measure:9898
20SVP310SH LEN 3X10 FT SRV WGT SH SOIL PP 18
21100 100 850 850 1 1 2 1 00001203269EA5656
22Total For Unit of Measure:5656
23SVP45SH LEN 4X5 FT SRV WGT SH SOIL PIP 19
24100 100 850 850 1 1 4 1 00001203347EA190190
25100 100 850 850 1 1 4 1 00004708459EA10
26Total For Unit of Measure:191190
Stock Status Report


At the beginning of the macro you would then see this:


Excel 2007
ABCD
1Cmx Whs Zone Aisle Bay Level Pos Slot Location.......UomOn Hand..Available
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5
3100 100 851 851 2 1 45 1 00002152007EA66
4Total For Unit of Measure:66
5SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6
6100 100 851 851 2 1 28 1 00002151951EA4646
7Total For Unit of Measure:4646
8SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8
9100 100 010 VENDOR RETURNSEA77
10Total For Unit of Measure:77
11SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15
12100 100 850 850 1 1 6 1 00001203349EA124124
13Total For Unit of Measure:124124
14SVP210SH LEN 2X10 FT SRV WGT SH SOIL PP 16
15100 100 850 850 1 1 3 1 00001203346EA3939
16Total For Unit of Measure:3939
17SVP35SH LEN 3X5 FT SRV WGT SH SOIL PIP 17
18100 100 850 850 1 1 5 1 00001203348EA9898
19Total For Unit of Measure:9898
20SVP310SH LEN 3X10 FT SRV WGT SH SOIL PP 18
21100 100 850 850 1 1 2 1 00001203269EA5656
22Total For Unit of Measure:5656
23SVP45SH LEN 4X5 FT SRV WGT SH SOIL PIP 19
24100 100 850 850 1 1 4 1 00001203347EA190190
25100 100 850 850 1 1 4 1 00004708459EA10
26Total For Unit of Measure:191190
Stock Status Report


Then after the macro is finished you will see this:


Excel 2007
ABCDE
1ProductUomOn Hand..AvailableCmx Whs Zone Aisle Bay Level Pos Slot Location.......
2SV3CO 3 SRV WGT CI CLEANOUT W/PLUG 5EA66100 100 851 851 2 1 45 1 00002152007
3SV4CO 4 SRV WGT CI CLEANOUT W/PLUG 6EA4646100 100 851 851 2 1 28 1 00002151951
4SV4CRLTTY2RS 4 SRV WGT CR LTTY W/2 RH SO CA 8EA77100 100 010 VENDOR RETURNS
5SVP25SH LEN 2X5 FT SRV WGT SH SOIL PPE 15EA124124100 100 850 850 1 1 6 1 00001203349
6SVP210SH LEN 2X10 FT SRV WGT SH SOIL PP 16EA3939100 100 850 850 1 1 3 1 00001203346
7SVP35SH LEN 3X5 FT SRV WGT SH SOIL PIP 17EA9898100 100 850 850 1 1 5 1 00001203348
8SVP310SH LEN 3X10 FT SRV WGT SH SOIL PP 18EA5656100 100 850 850 1 1 2 1 00001203269
9SVP45SH LEN 4X5 FT SRV WGT SH SOIL PIP 19EA190190100 100 850 850 1 1 4 1 00001203347
Stock Status Report


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub ReorgDataStockStatusReportV3()
' hiker95, 01/12/2014, ME749822
Dim oa As Variant, na As Variant
Dim i As Long, ii As Long
Dim lr As Long, n As Long
Sheets("Stock Status Report").Activate
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
With Range("A1:D" & lr)
  .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
oa = Range("A1:D" & lr)
n = Application.CountIf(Columns(2), "EA")
ReDim na(1 To n, 1 To 5)
For i = 2 To UBound(oa, 1) - 1
  If oa(i, 2) = "" And oa(i + 1, 2) = "EA" Then
    ii = ii + 1
    na(ii, 1) = oa(i, 1)
    na(ii, 2) = oa(i + 1, 2)
    na(ii, 3) = oa(i + 1, 3)
    na(ii, 4) = oa(i + 1, 4)
    na(ii, 5) = oa(i + 1, 1)
  End If
Next i
Range("A1:D" & lr).ClearContents
Range("A1").Resize(, 5).Value = Array("Product", "Uom", "On Hand..", "Available", "Cmx Whs Zone Aisle Bay Level Pos Slot Location.......")
Range("A2").Resize(UBound(na, 1), UBound(na, 2)) = na
Columns.AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgDataStockStatusReportV3 macro.
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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