We had a macro written 4 or 5 years ago that took the attached xlsx and changed the data around into a more readable format.
Where the script seems to get stuck on is for example Column A has a part number without letters in it. For example A:226 - A:255 (Theres more if you page down)
If I remove these cells the script works but seems to only get stuck on these types of part numbers.
Could anyone help me find the problem?
Sub test()
Dim strTargetFile As String
Application.DisplayAlerts = False
strTargetFile = "*.xml"
Workbooks.OpenXML Filename:=strTargetFile, LoadOption:=xlXmlLoadImportToList
Application.DisplayAlerts = True
Columns(2).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(4).EntireColumn.Delete
Dim ws, ws2, ws3 As Worksheet, lr%, celval$, destcol%, destrow%
Set ws = ActiveSheet
Set ws2 = Worksheets.Add
Set ws3 = Worksheets.Add
ws.Cells.Copy
ws2.Range("A1").PasteSpecial xlPasteValues, xlNone
Application.CutCopyMode = False
lr = ws2.Range("C65536").End(xlUp).Row
ws2.Sort.SortFields.Add Key:=Range("B2:B" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws2.Sort
.SetRange Range("A1:C" & lr)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For Each cell In ws2.Range("B2:B" & lr)
If cell.Value <> celval Then
celval = cell.Value
ws3.Cells(1, ws3.Columns.Count).End(xlToLeft).Offset(0, 1).Value = celval
End If
If WorksheetFunction.CountIf(ws3.Range("A:A"), cell.Offset(0, -1).Value) = 0 Then ws3.Range("A65536").End(xlUp).Offset(1, 0).Value = cell.Offset(0, -1).Value
destrow = WorksheetFunction.Match(cell.Offset(0, -1).Value, ws3.Range("A:A"), 0)
destcol = ws3.Rows("1:1").Find(What:=cell.Value, After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False).Column
Cells(destrow, destcol).Value = cell.Offset(0, 1).Value
Next cell
ws3.Rows("1:1").NumberFormat = "Mmm/dd/yyyy"
Application.DisplayAlerts = False
ws2.Delete
Application.DisplayAlerts = True
Application.DisplayAlerts = False
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
ActiveSheet.Name = "830 Data"
End Sub
Bookmarks