using vba to clean up data

padadof2

New Member
Joined
Jan 11, 2010
Messages
44
I have to clean up data and I'm hoping some excel Guru's can help me out. I get data has everything in one cell, separated by spaces, and I'd like to get it into a proper formatting, having one word per cell, but there's a caveat. Every even row, I need all that text in one cell

Here is the data I get in a single cell:
0696-0600 J1 Each 2.0000 N
TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 2
0697-0600 J1 Each 2.0000 N
RESET TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 2
0802-0001 C1, M Cubic Yard 99.0000 N
TOPSOIL FURNISHED AND PLACED
0867-0012 M2 Linear Foot 170.0000 N
COMPOST FILTER SOCK, 12" DIAMETER
0901-0001 Q Lump Sum 1.0000 N
MAINTENANCE AND PROTECTION OF TRAFFIC DURING CONSTRUCTION
0855-0003 M2 Each 1.0000 N
PUMPED WATER FILTER BAG
0855-0004 M2 Each 1.0000 N
REPLACEMENT PUMPED WATER FILTER BAG

Here is what I would like the vba to do:
0696-0600 TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 22Each
0697-0600 RESET TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 22Each
0802-0001 TOPSOIL FURNISHED AND PLACED99Cubic Yard
0867-0012 COMPOST FILTER SOCK, 12" DIAMETER170Linear Foot
0901-0001 MAINTENANCE AND PROTECTION OF TRAFFIC DURING CONSTRUCTION1Lump Sum
0855-0003 PUMPED WATER FILTER BAG1Each
0855-0004 REPLACEMENT PUMPED WATER FILTER BAG1Each


This is what I've come up with so far:
VBA Code:
Sub pdtest()
Dim MyArray() As String, MyString As String, I As Variant, N As Integer, r As Integer, lr As Integer

    lr = Cells(Rows.Count, 1).End(xlUp).Row
    I = 2
        For r = 1 To lr Step 2
            Cells(r, 1).Select
            MyString = ActiveCell.Value

            MyArray = Split(MyString, " ")

            For N = 0 To UBound(MyArray)
                Cells(lr + 1, N + 1).Value = MyArray(N)
            Next N
            
            Cells(lr + 1, 2).Value = Cells(I, 1).Value
            I = I + 2
    lr = lr + 1
Next r
End Sub

Hopefully some guru's are willing to help
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
try this.

create a new sheet named "Sheet2"


VBA Code:
Sub pdtest()

Dim rs As Worksheet
Set rs = Worksheets("Sheet2")
rs.Cells.ClearContents

    lr = Cells(Rows.Count, "A").End(xlUp).Row
     wr = 1
    
        For r = 1 To lr Step 2
        
           rs.Cells(wr, "A") = Split(Cells(r, "A"), " ")(0) 'item
           rs.Cells(wr, "C") = Split(Cells(r, "A"), " ")(3) 'qty
           rs.Cells(wr, "D") = Split(Cells(r, "A"), " ")(2) 'u/m
           
           rs.Cells(wr, "b") = Cells(r + 1, "A")
    wr = wr + 1

Next r
End Sub

hth,
Ross
 
Upvote 0
try this.

create a new sheet named "Sheet2"


VBA Code:
Sub pdtest()

Dim rs As Worksheet
Set rs = Worksheets("Sheet2")
rs.Cells.ClearContents

    lr = Cells(Rows.Count, "A").End(xlUp).Row
     wr = 1
   
        For r = 1 To lr Step 2
       
           rs.Cells(wr, "A") = Split(Cells(r, "A"), " ")(0) 'item
           rs.Cells(wr, "C") = Split(Cells(r, "A"), " ")(3) 'qty
           rs.Cells(wr, "D") = Split(Cells(r, "A"), " ")(2) 'u/m
          
           rs.Cells(wr, "b") = Cells(r + 1, "A")
    wr = wr + 1

Next r
End Sub

hth,
Ross
Not quite.
 

Attachments

  • TestResults.PNG
    TestResults.PNG
    21.2 KB · Views: 8
Upvote 0
How about
VBA Code:
Sub padadof()
   Dim NxtRw As Long, y As Long
   Dim Tmp As Variant
   Dim Cl As Range
   
   With Sheets("Sheet1")
      .Range("C:F").ClearContents
      For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         If Cl.Row Mod 2 = 1 Then
            NxtRw = NxtRw + 1
            .Range("C" & NxtRw).Value = Split(Cl.Value, " ")(0)
            Tmp = Split(StrReverse(Cl.Value), " ", 3)
            .Range("E" & NxtRw) = StrReverse(Tmp(1))
            Tmp = StrReverse(Tmp(2))
            y = Len(Tmp) - Len(Replace(Tmp, " ", ""))
            If y = 2 Then
               .Range("F" & NxtRw) = Split(Tmp, " ")(2)
            Else
               .Range("F" & NxtRw) = Split(Tmp, " ", y)(y - 1)
            End If
         Else
            .Range("D" & NxtRw) = Cl.Value
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Possibly more robust depending on your data.
VBA Code:
Sub padadof2()
   Dim NxtRw As Long
   Dim Tmp As Variant
   Dim Cl As Range
 
   With Sheets("Sheet1")
      .Range("C:F").ClearContents
      For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         If Cl.Row Mod 2 = 1 Then
            NxtRw = NxtRw + 1
            .Range("C" & NxtRw).Value = Split(Cl.Value)(0)
            Tmp = Split(StrReverse(Cl.Value), , 3)
            .Range("E" & NxtRw) = StrReverse(Tmp(1))
            Tmp = StrReverse(Tmp(2))
            If Len(Split(Tmp)(2)) > 2 Then
               .Range("F" & NxtRw) = Split(Tmp, , 3)(2)
            Else
               .Range("F" & NxtRw) = Split(Tmp, , 4)(3)
            End If
         Else
            .Range("D" & NxtRw) = Cl.Value
         End If
      Next Cl
   End With
End Sub
+Fluff 1.xlsm
A
10696-0600 J1, M Each 2.0000 N
2 TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 2
30697-0600 J1 Each 2.0000 N
4 RESET TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 2
50802-0001 C1, M Cubic Yard 99.0000 N
6 TOPSOIL FURNISHED AND PLACED
70867-0012 M2 Linear Foot 170.0000 N
8 COMPOST FILTER SOCK, 12" DIAMETER
90901-0001 Q Lump Sum 1.0000 N
10 MAINTENANCE AND PROTECTION OF TRAFFIC DURING CONSTRUCTION
110855-0003 M2 Boxed in 10s 1.0000 N
12 PUMPED WATER FILTER BAG
130855-0004 M2 Each 1.0000 N
14 REPLACEMENT PUMPED WATER FILTER BAG
15
Main

+Fluff 1.xlsm
CDEF
10696-0600 TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 22Each
20697-0600 RESET TEMPORARY IMPACT ATTENUATING DEVICE, TYPE V (STANDARD), TEST LEVEL 22Each
30802-0001 TOPSOIL FURNISHED AND PLACED99Cubic Yard
40867-0012 COMPOST FILTER SOCK, 12" DIAMETER170Linear Foot
50901-0001 MAINTENANCE AND PROTECTION OF TRAFFIC DURING CONSTRUCTION1Lump Sum
60855-0003 PUMPED WATER FILTER BAG1Boxed in 10s
70855-0004 REPLACEMENT PUMPED WATER FILTER BAG1Each
8
Main
 
Last edited:
Upvote 0
thank you guys, I really appreciate the help. Unfortunately I am not able to get fluffys code to work on this snippet of data.
1090-1018 S2 Square Foot 113.0000 N
PRESTRESSED CONCRETE BEAM REPAIRS
9000-0502 C1, S4 Cubic Foot 225.0000 N
INTRUSION GROUT BAGS
9000-0504 X Lump Sum 1.0000 N
WATER TESTING TECHNICIAN
9000-9001 S2 Cubic Foot 4.0000 N
REPAIR DETERIORATED CONCRETE


I'm not sure why but this is what I get

array testing with penndot items.xlsm
CDEF
11090-1018 PRESTRESSED CONCRETE BEAM REPAIRS113Square Foot
29000-0502 INTRUSION GROUT BAGSNCubic Foot 225.0000
39000-0504 WATER TESTING TECHNICIAN1Lump Sum
49000-9001 REPAIR DETERIORATED CONCRETENCubic Foot 4.0000
Sheet4 (4)


It looks like the tmp string changes, but I'm not savvy enough to know for sure why

***EDIT I had extra space in two of the cells, which was causing the issue. Thank you Fluffy, This works amazing!
 
Last edited:
Upvote 0
You have trailing spaces in some of those cells, try
VBA Code:
Sub padadof2()
   Dim NxtRw As Long
   Dim Tmp As Variant
   Dim Cl As Range
   
   With Sheets("main")
      .Range("C:F").ClearContents
      For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         If Cl.Row Mod 2 = 1 Then
            NxtRw = NxtRw + 1
            .Range("C" & NxtRw).Value = Split(Trim(Cl.Value))(0)
            Tmp = Split(StrReverse(Trim(Cl.Value)), , 3)
            .Range("E" & NxtRw) = StrReverse(Tmp(1))
            Tmp = StrReverse(Tmp(2))
            If Len(Split(Tmp)(2)) > 2 Then
               .Range("F" & NxtRw) = Split(Tmp, , 3)(2)
            Else
               .Range("F" & NxtRw) = Split(Tmp, , 4)(3)
            End If
         Else
            .Range("D" & NxtRw) = Cl.Value
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
You have trailing spaces in some of those cells, try
VBA Code:
Sub padadof2()
   Dim NxtRw As Long
   Dim Tmp As Variant
   Dim Cl As Range
  
   With Sheets("main")
      .Range("C:F").ClearContents
      For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         If Cl.Row Mod 2 = 1 Then
            NxtRw = NxtRw + 1
            .Range("C" & NxtRw).Value = Split(Trim(Cl.Value))(0)
            Tmp = Split(StrReverse(Trim(Cl.Value)), , 3)
            .Range("E" & NxtRw) = StrReverse(Tmp(1))
            Tmp = StrReverse(Tmp(2))
            If Len(Split(Tmp)(2)) > 2 Then
               .Range("F" & NxtRw) = Split(Tmp, , 3)(2)
            Else
               .Range("F" & NxtRw) = Split(Tmp, , 4)(3)
            End If
         Else
            .Range("D" & NxtRw) = Cl.Value
         End If
      Next Cl
   End With
End Sub
yes, I just caught that as well. thank you again for your help!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Fluff, Can we revisit this code? It looks like there may be another string or two of text at the end of the odd lines at times which throw things off. Thanks again for your time

Book1
ABCDEFG
10605-2644 K Each 1.0000 N0605-2644 Type D Endwall For 30" Pipe1EA
2 TYPE D ENDWALL FOR 30" PIPE4605-2700 Inlet Grate LeaflessNEach 20.0000
34605-2700 K Each 20.0000 N Std0605-2730 Type M Concrete Top Unit And Grate4Set
4 INLET GRATE LEAFLESS4605-2850 Standard Inlet Box, Height < /= 10' Includes Pipe StubsNEach 3.0000
50605-2730 K Set 4.0000 N4605-4010 Remove Existing InletNEach 2.0000
6 TYPE M CONCRETE TOP UNIT AND GRATE4605-4011 Remove Existing EndwallNEach 7.0000
74605-2850 K Each 3.0000 N Proj0608-0001 Mobilization1Lump Sum 1.0000 N
8 STANDARD INLET BOX, HEIGHT < /= 10' INCLUDES PIPE STUBS0609-0002 Inspector's Field Office And Inspection Facilities, Type A1LS
94605-4010 C1, H Each 2.0000 N Std0609-0009 Equipment PackageNLump Sum 1.0000
10 REMOVE EXISTING INLET0610-7001 4" Pavement Base Drain168LF
114605-4011 C1, H Each 7.0000 N Std0616-0009 Steel End Section, Metallic Coated, 16 Gage For 18" Pipe2EA
12 REMOVE EXISTING ENDWALL
130608-0001 X Lump Sum 1.0000 N 1 Added
14 MOBILIZATION
150609-0002 X Lump Sum 1.0000 N
16 INSPECTOR'S FIELD OFFICE AND INSPECTION FACILITIES, TYPE A
170609-0009 X Lump Sum 1.0000 N Std
18 EQUIPMENT PACKAGE
190610-7001 H2 Linear Foot 168.0000 N
20 4" PAVEMENT BASE DRAIN
210616-0009 H Each 2.0000 N
22 STEEL END SECTION, METALLIC COATED, 16 GAGE FOR 18" PIPE
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,850
Members
449,194
Latest member
HellScout

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