I am new to VBA. How do I split the entire row based on the values in col D and col F? I have around 1,200 rows.
Note: I am looking to Replace the old values in the same sheet.
My Old Sheet looks something like this:
A B C D E F G H
9/9/2015 9:54 500 glass 2 1 590 ABC 123 NULL
6/8/2015 8:55 501 glass 3 to 4 1 400 to 500 XYZ 259 NULL
5/8/2015 8:55 502 glass 1 to 2 1 675 to 750 J8H 1X4 NULL
1/11/2015 9:55 503 glass Base to 2 1 425 to 575 J1K 2N1 NULL
1/1/2015 8:55 504 glass 3 2 1030 to 1050 H7G 3B5 NULL
16/1/2015 9:55 505 glass 2 2 1600 to 1800 H7W 5E4 NULL
The expected sheet should look like this
A B C D E F G H
9/9/2015 9:54 500 glass 2 1 590 ABC 123 NULL
6/8/2015 8:55 501 glass 3 1 400 XYZ 259 NULL
6/8/2015 8:55 501 glass 4 1 500 XYZ 259 NULL
5/8/2015 8:55 502 glass 1 1 675 ABC 123 NULL
5/8/2015 8:55 502 glass 2 1 750 ABC 123 NULL
1/11/2015 9:55 503 glass Base 1 425 ABC 123 NULL
1/11/2015 9:55 503 glass 2 1 575 ABC 123 NULL
1/1/2015 8:55 504 glass 3 2 1040 ABC 123 NULL
16/1/2015 9:55 505 glass 2 2 1700 ABC 123 NULL
- Split into two when col. D = range, (row 2 (old sheet))
- Split when both D & F are range, (row 3 and 4 (old sheet))
- Take Average when only F is range. (row 5 and 6 (old sheet))
- Ignore otherwise. (row 1 (old sheet))
EDIT:
This is how i tried coding it:
Sub Split()
Dim cl As Range, x&, z&, k
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
With ActiveSheet 'replace `activesheet` by `sheets("specifysheetname")` if required
x = .Cells(.Rows.Count, "D").End(xlUp).Row: z = 1
For Each cl In .Range("D1:D" & x)
If LCase(cl.Value2) Like "*to*" And _
LCase(cl.Offset(, 1).Value2) Like "*to*" Then
Dic.Add z, Split(cl.Value2)(0) & ";" & Split(cl.Offset(, 1).Value2)(0): z = z + 1
Dic.Add z, Split(cl.Value2)(2) & ";" & Split(cl.Offset(, 1).Value2)(2)
ElseIf Not (LCase(cl.Value2) Like "*to*") And _
LCase(cl.Offset(, 1).Value2) Like "*to*" Then
Dic.Add z, cl.Value2 & ";" & Application.Average(Split(cl.Offset(, 1).Value)(0), _
Split(cl.Offset(, 1).Value)(2))
Else
Dic.Add z, cl.Value2 & ";" & cl.Offset(, 1).Value2
End If
z = z + 1
Next cl
For Each k In Dic
.Cells(k, "D").Value2 = Split(Dic(k), ";")(0)
.Cells(k, "F").Value2 = Split(Dic(k), ";")(1)
Next k
End With
End Sub
Seems a bit messed up. I am not getting the desired result. I have difficulty copying the other values of the row.
Any Help is much appreciated. Thanks in advance.
IsNumericas you loop through your rows. You can check if it is a number or a range and act accordingly. If you get stuck, post your code, and someone will help you fix it. - Tim