A Challenge for you Excel experts

mljohn

Board Regular
Joined
Aug 26, 2004
Messages
196
Office Version
  1. 365
Platform
  1. Windows
I have a large list of parts that I want to change how the data is laid out.

It was laid out by the part number and which machines went to that part number. The machines were all together in one cell.

Now I want to have each machine have it's own row.

I'll need a way to automate the process since I have so many records.

I have a link to my Box.net account that shows an example spreadsheet.

http://www.box.net/shared/cl8l0vi9qh

I hope you are up to the challenge, I sure need the help.

Thanks

Matt
 
mljohn,


Per your latest workbook, and instructions (I am displaying a worksheet Compatible with for discussion):


Excel Workbook
CKN
271HL-5130There is a pattern. For example: Row #271
2725140HL-5130
2735150D5150D These are all the HL- machines
2745150DLT5150DLT They need the "HL-" added to them
2755170DN5170DN
2765170DNLT5170DNLT
277DCP-8040DCP-8040 These are all the DCP- machines
2788045D8045D They need the "DCP-" added to them
279MFC-8220MFC-8220 These are all the MFC- machines
28084408440 They need the "MFC-" added to them
2818640D8640D
2828840D8840D
2838840DN8840DN
Compatible with




We can do the above for 12 items per your instructions.



But. The following screenshot, column L, contains a list of the unique items from your lastest column C which contains 3,691 rows of data. There are 1,003 unique items. Column P shows the unique list sorted ascending.


What are we going to do with the the rest?


Excel Workbook
LOP
1Compatible with UNIQUE contains 1003 unique itemsCompatible with UNIQUE SORTED
2LaserWriter12/640+5
312/640PS32
4LaserWriter16/60040
516/600PS80
6LaserWriterPro600104
7630(EX)120
8LaserWriter350(BX)150
9DCP-1200160
101400170
11Fax-4750250
125750300
138350p310
148750p320
15HL-1030322
161230325
171240330
181250340
191270n400
201435401
211440420
221470n425
Compatible with
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Arg!

I don't know what you mean.

I am soooo frustrated with my communication skills.

Example: from row #271

This is what they typed in. The data entry person was lazy. She typed in "HL-5130" and instead of typing "HL-5140" she just typed "5140", etc until she hit the "DCP" machines where she only typed the "DCP-" on the first machine.

If you will look on the "Results" worksheet in the "Compatible with" column you will see the pattern. Sometimes they type in the full machine model and sometimes they take the shortcut and don't fill in the first letters after the first machine.

HL-5130
5140
5150D
5150DLT
5170DN
5170DNLT
DCP-8040
8045D
MFC-8220
8440
8640D
8840D
8840DN

This is what I want.

HL-5130
HL-5140
HL-5150D
HL-5150DLT
HL-5170DN
HL-5170DNLT
DCP-8040
DCP-8045D
MFC-8220
MFC-8440
MFC-8640D
MFC-8840D
MFC-8840DN

This example is just for the Brothers. If you look down the "Compatible with" column you will see the pattern where they don't type in the full model number on every machine.

I don't want to manually fill in all the missing data.

Can we write a visual basic code that finds the models that weren't filled out and fix them?
 
Upvote 0
Wouldn't that code need to take what's been entered and match it against all possible variations that might match a correct name?

What if there are conflicts? eg 2 models of printers, each from different manufacturers, both have '5140' in them?
 
Upvote 0
Norie,

Thank you very much.


mljohn,

Can we write a visual basic code that finds the models that weren't filled out and fix them?

Yes, but:

The only way we can fill in all the missing printer model information (and be accurate), we will need a complete list of all the printer models to compare each item in worksheet Results, column C.

We need something like this, for all the printer models:

HL-5130
HL-5140
HL-5150D
HL-5150DLT
HL-5170DN
HL-5170DNLT
DCP-8040
DCP-8045D
MFC-8220
MFC-8440
MFC-8640D
MFC-8840D
MFC-8840DN
 
Last edited:
Upvote 0
OK, I quit. I can't explain this properly.

I need the very list you suggest I provide. That is the whole purpose for this. I can't give it to you if I haven't created it yet.

Can't the code be done one line at a time?

If the cell contains a number with no alpha characters before any numbers and the cell above is not a blank cell and contains alpha characters before numbers then add the characters preceding the "-" character including the "-" character. Such as "HL-".

If not then leave it the way it is.
 
Upvote 0
mljohn,


If the cell contains a number with no alpha characters before any numbers and the cell above is not a blank cell and contains alpha characters before numbers then add the characters preceding the "-" character including the "-" character. Such as "HL-".

If not then leave it the way it is.


Now I understand.



Sample raw data in worksheet Sheet1:


Excel Workbook
ABCDEFGHIJ
1CategoryPrinterCompatible withOemPartNoMSEPartNoPremiumPartNoPageYieldPriceMSRPStreetPrice
2TONERBROTHERDCP-7030, 7040, 7045N,HL-2140, 2150N, 2170W, MFC-7320, 7340, 7345DN, 7345N, 7440N, 7840WDR36058-03-361458-03-361012,000$46.00$75.00$99.99
3TONERBROTHERDCP-8020, 8025D, HL-1650, 1650N, 1650NPLUS, 1670N, 1850, 1870n, 5040, 5050, 5050LT, 5070N, MFC-8420, 8420 D, 8820D, 8820DNDR50058-03-501458-03-501020,000$62.00$109.00$198.29
4
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
ABCDEFGHIJ
1CategoryPrinterCompatible withOemPartNoMSEPartNoPremiumPartNoPageYieldPriceMSRPStreetPrice
2TONERBROTHERDCP-7030DR36058-03-361458-03-361012,000$46.00$75.00$99.99
3TONERBROTHERDCP-7040DR36058-03-361458-03-361012,000$46.00$75.00$99.99
4TONERBROTHERDCP-7045NDR36058-03-361458-03-361012,000$46.00$75.00$99.99
5TONERBROTHERHL-2140DR36058-03-361458-03-361012,000$46.00$75.00$99.99
6TONERBROTHERHL-2150NDR36058-03-361458-03-361012,000$46.00$75.00$99.99
7TONERBROTHERHL-2170WDR36058-03-361458-03-361012,000$46.00$75.00$99.99
8TONERBROTHERMFC-7320DR36058-03-361458-03-361012,000$46.00$75.00$99.99
9TONERBROTHERMFC-7340DR36058-03-361458-03-361012,000$46.00$75.00$99.99
10TONERBROTHERMFC-7345DNDR36058-03-361458-03-361012,000$46.00$75.00$99.99
11TONERBROTHERMFC-7345NDR36058-03-361458-03-361012,000$46.00$75.00$99.99
12TONERBROTHERMFC-7440NDR36058-03-361458-03-361012,000$46.00$75.00$99.99
13TONERBROTHERMFC-7840WDR36058-03-361458-03-361012,000$46.00$75.00$99.99
14
15
16
17TONERBROTHERDCP-8020DR50058-03-501458-03-501020,000$62.00$109.00$198.29
18TONERBROTHERDCP-8025DDR50058-03-501458-03-501020,000$62.00$109.00$198.29
19TONERBROTHERHL-1650DR50058-03-501458-03-501020,000$62.00$109.00$198.29
20TONERBROTHERHL-1650NDR50058-03-501458-03-501020,000$62.00$109.00$198.29
21TONERBROTHERHL-1650NPLUSDR50058-03-501458-03-501020,000$62.00$109.00$198.29
22TONERBROTHERHL-1670NDR50058-03-501458-03-501020,000$62.00$109.00$198.29
23TONERBROTHERHL-1850DR50058-03-501458-03-501020,000$62.00$109.00$198.29
24TONERBROTHERHL-1870nDR50058-03-501458-03-501020,000$62.00$109.00$198.29
25TONERBROTHERHL-5040DR50058-03-501458-03-501020,000$62.00$109.00$198.29
26TONERBROTHERHL-5050DR50058-03-501458-03-501020,000$62.00$109.00$198.29
27TONERBROTHERHL-5050LTDR50058-03-501458-03-501020,000$62.00$109.00$198.29
28TONERBROTHERHL-5070NDR50058-03-501458-03-501020,000$62.00$109.00$198.29
29TONERBROTHERMFC-8420DR50058-03-501458-03-501020,000$62.00$109.00$198.29
30TONERBROTHERMFC-8420DDR50058-03-501458-03-501020,000$62.00$109.00$198.29
31TONERBROTHERMFC-8820DDR50058-03-501458-03-501020,000$62.00$109.00$198.29
32TONERBROTHERMFC-8820DNDR50058-03-501458-03-501020,000$62.00$109.00$198.29
33
Results





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 ReorgDataV2()
' hiker95, 03/28/2011
' http://www.mrexcel.com/forum/showthread.php?t=538961
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, Sp, s As Long, H As String, NR As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
Set wR = Worksheets("Results")
wR.Range("A1:J1").Value = w1.Range("A1:J1").Value
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
NR = 2
For a = 2 To LR Step 1
  H = ""
  H = Replace(w1.Cells(a, 3), " ", "")
  Sp = Split(H, ",")
  s = UBound(Sp) + 1
  wR.Range("A" & NR).Resize(s, 2).Value = w1.Range("A" & a).Resize(, 2).Value
  wR.Range("C" & NR).Resize(s).Value = Application.Transpose(Sp)
  wR.Range("D" & NR).Resize(s, 7).Value = w1.Range("D" & a).Resize(, 7).Value
  NR = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row + 3
Next a
wR.Range("C2:G" & NR + s).HorizontalAlignment = xlCenter
wR.Range("H2:J" & NR + s).NumberFormat = "[$$-409]#,##0.00_);([$$-409]#,##0.00)"
LR = wR.Cells(Rows.Count, 3).End(xlUp).Row
H = ""
For a = 2 To LR Step 1
  If Cells(a, 3) = "" Then
    H = ""
  ElseIf InStr(Cells(a, 3), "-") > 0 Then
    Sp = Split(Cells(a, 3), "-")
    H = Sp(0) & "-"
  Else
    Cells(a, 3) = H & Cells(a, 3)
  End If
Next a
wR.UsedRange.Columns.AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub


Then run the ReorgDataV2 macro.
 
Upvote 0
wonderful! Yes this is better.

I noticed that it took all the spaces out between the machine and model number. Example: Imageclass MF5530 = ImageclassMF5530

Can the spaces not be removed?

Also, I noticed that some of the Canons it did not work on.
I highlighted those in yellow.

Also, it did not work on many others. I guess that might be because those did not have a "-". I highlighted those in orange. I also created a "Sheet 2" with a list of machine names that were not effected.

Can those also be fixed?

Here is the new link: http://www.box.net/shared/hf9p5sjz5f

Thanks
 
Last edited:
Upvote 0
mljohn,


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 ReorgDataV3()
' hiker95, 03/30/2011
' http://www.mrexcel.com/forum/showthread.php?t=538961
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, aa As Long, Sp, s As Long, H As String, NR As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
Set wR = Worksheets("Results")
wR.Range("A1:J1").Value = w1.Range("A1:J1").Value
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
NR = 2
For a = 2 To LR Step 1
  H = w1.Cells(a, 3)
  Sp = Split(H, ",")
  s = UBound(Sp) + 1
  wR.Range("A" & NR).Resize(s, 2).Value = w1.Range("A" & a).Resize(, 2).Value
  For aa = LBound(Sp) To UBound(Sp)
    Sp(aa) = Trim(Sp(aa))
  Next aa
  wR.Range("C" & NR).Resize(s).Value = Application.Transpose(Sp)
  wR.Range("D" & NR).Resize(s, 7).Value = w1.Range("D" & a).Resize(, 7).Value
  NR = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row + 3
Next a
wR.Range("C2:G" & NR + s).HorizontalAlignment = xlCenter
wR.Range("H2:J" & NR + s).NumberFormat = "[$$-409]#,##0.00_);([$$-409]#,##0.00)"
LR = wR.Cells(Rows.Count, 3).End(xlUp).Row
H = ""
For a = 2 To LR Step 1
  If Cells(a, 3) = "" Then
    H = ""
  ElseIf InStr(Cells(a, 3), "-") > 0 Then
    Sp = Split(Cells(a, 3), "-")
    H = Sp(0) & "-"
  Else
    Cells(a, 3) = H & Cells(a, 3)
  End If
Next a
wR.UsedRange.Columns.AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub


Then run the ReorgDataV3 macro.



If the above macro is not acceptable, then click on the Post Reply button, and just enter the word BUMP, and click on the Submit Reply button, and someone else will assist you.
 
Upvote 0
Thank you for fixing the spaces.

I was worried that I might wear you out. Sorry about that.

Hiker95, you have amazed me with you skill. Sorry about the communication problems.

Thank you for your help. You have gotten me a long ways toward fixing this night mare of price lists that I am buried in.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,855
Members
452,948
Latest member
UsmanAli786

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