Hi All
I have this large set of code below.
Note:
Any help would be greatly appreciated.
I have this large set of code below.
Note:
- That it is essentially the same code runs but with slight variations for each different type of sheet (determined by the Select Case) in the source file.
- It runs through each line, and then builds new lines in the "Lines" sheet based on "A" and combination of column headers.
- It runs very slowly, a full file run (4 sheets processed) is about 10 minutes.
- It also crashes Excel, as in full Excel crash / closes, no longer is running in task manager. It does this most times, but not every time.
- The code below, does exactly what I want it to do other than being slow and causing Excel to crash randomly.
VBA Code:
Public myFolder As String
Public strInvFile As String
Public strFile As String
Public strUnprocessedSheets As String
Public strInvoice As String
Public strSupplier As String
Public Function ImportSpreadsheet(strInvFile As String, strFile As String)
Dim Lastrow As Integer
Dim LastrowL As Integer
Dim lngInstr As Long
Dim strUnprocessedSheets As String
Dim strInvoice As String
Dim strSupplier As String
Dim intHeaderRow As Integer
Dim strStyle As String
Dim strType As String
Dim strGender As String
Dim strMaterial As String
Dim strDesc As String
Dim strOrigin As String
Dim dblUnitPrice As Double
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
strUnprocessedSheets = ""
strInvoice = ""
strSupplier = ""
intHeaderRow = 0
strStyle = ""
strType = ""
strGender = ""
strMaterial = ""
strDesc = ""
strOrigin = ""
dblUnitPrice = 0
ThisWorkbook.Sheets.Add.Name = "Data"
Workbooks.Open strInvFile, UpdateLinks:=False
For i = 1 To Workbooks(strFile).Sheets.Count
Select Case Workbooks(strFile).Sheets(i).Name
Case Is = "Supplier 1"
'Supplier 1
Workbooks(strFile).Sheets(i).Cells.Copy ThisWorkbook.Sheets("Data").Range("A1")
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
With ThisWorkbook.Sheets("Data")
strInvoice = .Range("B2").Value
strSupplier = "Supplier 1"
.Rows("1:9").EntireRow.Delete
Lastrow = .Range("W" & .Rows.Count).End(xlUp).Row
.Rows(Lastrow + 1 & ":65536").EntireRow.Delete
.Columns("A:A").Delete
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = Lastrow To 1 Step -1
If IsEmpty(.Range("A" & r)) = True Then
.Rows(r & ":" & r).EntireRow.Delete
Else
End If
Next r
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = 1 To Lastrow
If .Range("A" & r).Value = "Style" Then
intHeaderRow = r
Else
strStyle = .Range("A" & r).Value
strType = .Range("C" & r).Value
strGender = .Range("E" & r).Value
strMaterial = .Range("G" & r).Value
strDesc = strGender & " " & strType & " " & strMaterial
strOrigin = .Range("D" & r).Value
dblUnitPrice = .Range("W" & r).Value
For c = 8 To 21
If IsEmpty(.Cells(intHeaderRow, c)) = False Then
If .Cells(r, c).Value = 0 Then
'No QTY so skip
Else
ThisWorkbook.Sheets("Lines").Range("A" & LastrowL + 1).Value = strSupplier 'Supplier
ThisWorkbook.Sheets("Lines").Range("B" & LastrowL + 1).Value = strInvoice 'Invoice
ThisWorkbook.Sheets("Lines").Range("C" & LastrowL + 1).Value = strStyle & "-" & .Cells(intHeaderRow, c).Value 'Product
ThisWorkbook.Sheets("Lines").Range("D" & LastrowL + 1).Value = strDesc 'Spreadsheet Description
ThisWorkbook.Sheets("Lines").Range("H" & LastrowL + 1).Value = .Cells(r, c).Value 'QTY
ThisWorkbook.Sheets("Lines").Range("I" & LastrowL + 1).Value = "UNT" 'UNT
ThisWorkbook.Sheets("Lines").Range("J" & LastrowL + 1).Value = .Cells(r, c).Value * dblUnitPrice 'PRICE
ThisWorkbook.Sheets("Lines").Range("K" & LastrowL + 1).Value = strOrigin 'Origin
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
End If
Else
'No size so skip
End If
Next c
End If
Next r
End With
'Delete DATA and recreate
ThisWorkbook.Sheets("Data").Delete
ThisWorkbook.Sheets.Add.Name = "Data"
Case Is = "Supplier 2"
'Supplier 2
Workbooks(strFile).Sheets(i).Cells.Copy ThisWorkbook.Sheets("Data").Range("A1")
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
With ThisWorkbook.Sheets("Data")
strInvoice = .Range("B2").Value
strSupplier = "Supplier 2"
.Rows("1:9").EntireRow.Delete
Lastrow = .Range("W" & .Rows.Count).End(xlUp).Row
.Rows(Lastrow + 1 & ":65536").EntireRow.Delete
.Columns("A:A").Delete
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = Lastrow To 1 Step -1
If IsEmpty(.Range("A" & r)) = True Then
.Rows(r & ":" & r).EntireRow.Delete
Else
End If
Next r
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = 1 To Lastrow
If .Range("A" & r).Value = "Style" Then
intHeaderRow = r
Else
strStyle = .Range("A" & r).Value
strType = .Range("C" & r).Value
strGender = .Range("E" & r).Value
strMaterial = .Range("G" & r).Value
strDesc = strGender & " " & strType & " " & strMaterial
strOrigin = .Range("D" & r).Value
dblUnitPrice = .Range("W" & r).Value
For c = 8 To 21
If IsEmpty(.Cells(intHeaderRow, c)) = False Then
If .Cells(r, c).Value = 0 Then
'No QTY so skip
Else
ThisWorkbook.Sheets("Lines").Range("A" & LastrowL + 1).Value = strSupplier 'Supplier
ThisWorkbook.Sheets("Lines").Range("B" & LastrowL + 1).Value = strInvoice 'Invoice
ThisWorkbook.Sheets("Lines").Range("C" & LastrowL + 1).Value = strStyle & "-" & .Cells(intHeaderRow, c).Value 'Product
ThisWorkbook.Sheets("Lines").Range("D" & LastrowL + 1).Value = strDesc 'Spreadsheet Description
ThisWorkbook.Sheets("Lines").Range("H" & LastrowL + 1).Value = .Cells(r, c).Value 'QTY
ThisWorkbook.Sheets("Lines").Range("I" & LastrowL + 1).Value = "UNT" 'UNT
ThisWorkbook.Sheets("Lines").Range("J" & LastrowL + 1).Value = .Cells(r, c).Value * dblUnitPrice 'PRICE
ThisWorkbook.Sheets("Lines").Range("K" & LastrowL + 1).Value = strOrigin 'Origin
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
End If
Else
'No size so skip
End If
Next c
End If
Next r
End With
'Delete DATA and recreate
ThisWorkbook.Sheets("Data").Delete
ThisWorkbook.Sheets.Add.Name = "Data"
Case Is = "Supplier 3"
'Supplier 3
Workbooks(strFile).Sheets(i).Cells.Copy ThisWorkbook.Sheets("Data").Range("A1")
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
With ThisWorkbook.Sheets("Data")
strInvoice = .Range("B2").Value
strSupplier = "Supplier 3"
.Rows("1:9").EntireRow.Delete
Lastrow = .Range("V" & .Rows.Count).End(xlUp).Row
.Rows(Lastrow + 1 & ":65536").EntireRow.Delete
.Columns("A:A").Delete
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = Lastrow To 1 Step -1
If IsEmpty(.Range("A" & r)) = True Then
.Rows(r & ":" & r).EntireRow.Delete
Else
End If
Next r
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = 1 To Lastrow
If .Range("A" & r).Value = "Style" Then
intHeaderRow = r
Else
strStyle = .Range("A" & r).Value
strType = .Range("C" & r).Value
strGender = .Range("E" & r).Value
strMaterial = .Range("G" & r).Value
strDesc = strGender & " " & strType & " " & strMaterial
strOrigin = .Range("D" & r).Value
dblUnitPrice = .Range("V" & r).Value
For c = 8 To 20
If IsEmpty(.Cells(intHeaderRow, c)) = False Then
If .Cells(r, c).Value = 0 Then
'No QTY so skip
Else
ThisWorkbook.Sheets("Lines").Range("A" & LastrowL + 1).Value = strSupplier 'Supplier
ThisWorkbook.Sheets("Lines").Range("B" & LastrowL + 1).Value = strInvoice 'Invoice
ThisWorkbook.Sheets("Lines").Range("C" & LastrowL + 1).Value = strStyle & "-" & .Cells(intHeaderRow, c).Value 'Product
ThisWorkbook.Sheets("Lines").Range("D" & LastrowL + 1).Value = strDesc 'Spreadsheet Description
ThisWorkbook.Sheets("Lines").Range("H" & LastrowL + 1).Value = .Cells(r, c).Value 'QTY
ThisWorkbook.Sheets("Lines").Range("I" & LastrowL + 1).Value = "UNT" 'UNT
ThisWorkbook.Sheets("Lines").Range("J" & LastrowL + 1).Value = .Cells(r, c).Value * dblUnitPrice 'PRICE
ThisWorkbook.Sheets("Lines").Range("K" & LastrowL + 1).Value = strOrigin 'Origin
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
End If
Else
'No size so skip
End If
Next c
End If
Next r
End With
'Delete DATA and recreate
ThisWorkbook.Sheets("Data").Delete
ThisWorkbook.Sheets.Add.Name = "Data"
Case Is = "Supplier 4"
'Supplier 4
Workbooks(strFile).Sheets(i).Cells.Copy ThisWorkbook.Sheets("Data").Range("A1")
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
With ThisWorkbook.Sheets("Data")
strInvoice = .Range("B2").Value
strSupplier = "Supplier 4"
.Rows("1:9").EntireRow.Delete
Lastrow = .Range("R" & .Rows.Count).End(xlUp).Row
.Rows(Lastrow + 1 & ":65536").EntireRow.Delete
.Columns("A:A").Delete
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = Lastrow To 1 Step -1
If IsEmpty(.Range("A" & r)) = True Then
.Rows(r & ":" & r).EntireRow.Delete
Else
End If
Next r
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
For r = 1 To Lastrow
If .Range("A" & r).Value = "Style" Then
intHeaderRow = r
Else
strStyle = .Range("A" & r).Value
strType = .Range("C" & r).Value
strGender = .Range("E" & r).Value
strMaterial = .Range("G" & r).Value
strDesc = strGender & " " & strType & " " & strMaterial
strOrigin = .Range("D" & r).Value
dblUnitPrice = .Range("R" & r).Value
For c = 8 To 16
If IsEmpty(.Cells(intHeaderRow, c)) = False Then
If .Cells(r, c).Value = 0 Then
'No QTY so skip
Else
ThisWorkbook.Sheets("Lines").Range("A" & LastrowL + 1).Value = strSupplier 'Supplier
ThisWorkbook.Sheets("Lines").Range("B" & LastrowL + 1).Value = strInvoice 'Invoice
ThisWorkbook.Sheets("Lines").Range("C" & LastrowL + 1).Value = strStyle & "-" & .Cells(intHeaderRow, c).Value 'Product
ThisWorkbook.Sheets("Lines").Range("D" & LastrowL + 1).Value = strDesc 'Spreadsheet Description
ThisWorkbook.Sheets("Lines").Range("H" & LastrowL + 1).Value = .Cells(r, c).Value 'QTY
ThisWorkbook.Sheets("Lines").Range("I" & LastrowL + 1).Value = "UNT" 'UNT
ThisWorkbook.Sheets("Lines").Range("J" & LastrowL + 1).Value = .Cells(r, c).Value * dblUnitPrice 'PRICE
ThisWorkbook.Sheets("Lines").Range("K" & LastrowL + 1).Value = strOrigin 'Origin
LastrowL = ThisWorkbook.Sheets("Lines").Range("C" & ThisWorkbook.Sheets("Lines").Rows.Count).End(xlUp).Row
End If
Else
'No size so skip
End If
Next c
End If
Next r
End With
'Delete DATA and recreate
ThisWorkbook.Sheets("Data").Delete
ThisWorkbook.Sheets.Add.Name = "Data"
Case Else
If strUnprocessedSheets = "" Then
strUnprocessedSheets = Workbooks(strFile).Sheets(i).Name
Else
strUnprocessedSheets = strUnprocessedSheets & " / " & Workbooks(strFile).Sheets(i).Name & " / "
End If
End Select
strInvoice = ""
strSupplier = ""
intHeaderRow = 0
strStyle = ""
strType = ""
strGender = ""
strMaterial = ""
strDesc = ""
strOrigin = ""
dblUnitPrice = 0
Next i
Workbooks(strFile).Saved = True
Workbooks(strFile).Close
ThisWorkbook.Sheets("Data").Delete
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
If strUnprocessedSheets = "" Then
'Empty
Else
lngInstr = InStr(1, strUnprocessedSheets, " / ")
If lngInstr = 0 Then
'No " / "
Else
strUnprocessedSheets = Left(strUnprocessedSheets, Len(strUnprocessedSheets) - 3)
End If
MsgBox "Spreadsheet imported successfully." & vbNewLine & vbNewLine & "However the below sheets were not processed:" & vbNewLine & vbNewLine & strUnprocessedSheets, vbInformation, "Process Compelted"
End If
End Function
Any help would be greatly appreciated.