Vince
asked on
Read EXCEL records, parse, and copy to new EXCEL file
I have a EXCEL file w/ 3 fields: "Emp-Num", "Emp-Name", "City Coverage"
"City Coverage" has 1 to many names of cities covered by an employee. If more than 1 city, the cities are separated by a comma. I need to copy each record to another EXCEL file. If an employee covers more than city, then that record is copied N times; once for each city. The output file has an additional column; "City Total", which has the count of cities covered by the employee.
I am very familiar w/ VBA, but have not used VBA w/ EXCEL. Please help. Thank you, very much. Enclosed, is an EXCEL file w/ 2 tabs: "Original" shows the data. "Final" shows what the data needs to look like when completed.Test_Data1.xlsx
"City Coverage" has 1 to many names of cities covered by an employee. If more than 1 city, the cities are separated by a comma. I need to copy each record to another EXCEL file. If an employee covers more than city, then that record is copied N times; once for each city. The output file has an additional column; "City Total", which has the count of cities covered by the employee.
I am very familiar w/ VBA, but have not used VBA w/ EXCEL. Please help. Thank you, very much. Enclosed, is an EXCEL file w/ 2 tabs: "Original" shows the data. "Final" shows what the data needs to look like when completed.Test_Data1.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Neeraj: Thank you for your solution. I would like to put the button on a separate sheet, and have the input data on it's own sheet. I am trying to understand how to do that.
You're welcome vfinato!
In that case, please try this...
In that case, please try this...
Sub ReArrangeData()
Dim sws As Worksheet
Dim lr As Long, i As Long, ii As Long, j As Long, cnt As Long
Dim x, y()
Dim str() As String
Application.ScreenUpdating = False
Application.CopyObjectsWithCells = False
Set sws = Sheets("Original")
lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
sws.Range("D2:D" & lr).Formula = "=LEN(C2)-LEN(SUBSTITUTE(C2,"","",""""))+1"
cnt = Application.Sum(sws.Range("D2:D" & lr))
x = sws.Range("A1").CurrentRegion.Value
ReDim y(1 To cnt, 1 To 4)
For i = 2 To UBound(x, 1)
If x(i, 4) = 1 Then
j = j + 1
y(j, 1) = x(i, 1)
y(j, 2) = x(i, 2)
y(j, 3) = x(i, 3)
y(j, 4) = x(i, 4)
Else
str = Split(x(i, 3), ",")
For ii = 0 To UBound(str)
j = j + 1
y(j, 1) = x(i, 1)
y(j, 2) = x(i, 2)
y(j, 3) = Trim(str(ii))
y(j, 4) = x(i, 4)
Next ii
End If
Next i
sws.Range("A1").CurrentRegion.Offset(1).Clear
sws.Range("A2").Resize(UBound(y), 4).Value = y
sws.Range("D1").Value = "City Total"
sws.UsedRange.Columns.AutoFit
Application.CopyObjectsWithCells = True
Application.ScreenUpdating = True
End Sub
Test_Data-v2.xlsm
ASKER
Neeraj: I think I figured it out. I would like the button on its own separate sheet, the input data on its own separate sheet and the output going to its own excel file, like you did in the original version. It looks like all I need to do is move the button to its own sheet and point the button object to the ReArrangeData Macro.
If I could ask. I am trying to understand the code. It looks like swb and sws point to the "Original" workbook and worksheet. And, dwb and dws point to the new workbook and worksheet. The question is why do you ReDim cnt 1 to 4. Is that because there are 4 columns in the dws worksheet?
If I could ask. I am trying to understand the code. It looks like swb and sws point to the "Original" workbook and worksheet. And, dwb and dws point to the new workbook and worksheet. The question is why do you ReDim cnt 1 to 4. Is that because there are 4 columns in the dws worksheet?
That's correct. 1 to 4 because there would be 4 columns finally in the resultant data set.
Open in new window