I have a report from our ERP (SAP) that is saved to a text file. That report is then opened in Excel and normalized with a macro and exported to an Access table via an ADO connection. (BTW I'm using Access 97.)
The problem is that there is a column that is sometimes there and other times it is not. So we have to modify the code from week to week. I'd like to add some inteligence to the code so it knows when the extra column is present.
Here is the code:
The problem is that there is a column that is sometimes there and other times it is not. So we have to modify the code from week to week. I'd like to add some inteligence to the code so it knows when the extra column is present.
Here is the code:
Code:
Sub FormatReport()
Dim r As Integer
Dim sOrder As String
With ShReport
.Range("A1:A2").EntireRow.Insert
r = 3
'Copy the order type to the column
sOrder = ""
Do While Trim(.Cells(r, 12)) <> "Orders not Shipped - Report Total"
If sOrder <> Trim(.Cells(r, 12)) And _
Trim(.Cells(r, 12)) <> "Orders Past Due Date" And _
Trim(.Cells(r, 12)) <> "" Then
sOrder = Trim(.Cells(r, 12))
End If
.Cells(r, 12) = sOrder
r = r + 1
Loop
'Delete the Last Page Summary report
.Range(.Cells(r, 1), .Cells(r + 65, 1)).EntireRow.Delete
'Assign Column Names to Row 1
.Cells(1, 1) = "Order #"
.Cells(1, 3) = "User Status"
.Cells(1, 6) = "Acct #"
.Cells(1, 7) = "Customer Name"
.Cells(1, 12) = "Order Type"
.Cells(1, 17) = "Cust Grp"
.Cells(1, 18) = "Sales Off"
.Cells(1, 19) = "Plant"
.Cells(1, 23) = "PO Number"
.Cells(1, 28) = "Sidemark"
.Cells(1, 30) = "Date Entered"
.Cells(1, 33) = "Date Booked"
.Cells(1, 36) = "Requested Ship Date"
.Cells(1, 39) = "Delivery CR/Hold"
.Cells(1, 43) = "Material Group"
.Cells(1, 44) = "Sales Amount"
.Cells(1, 46) = "Order Units"
.Cells(1, 48) = "Order Reason"
'Delete the blank rows
r = 3
Do While .Cells(r, 12) <> ""
If Trim(.Cells(r, 6)) = "" Or _
Trim(.Cells(r, 6)) = "Customer #" Then
.Range(.Cells(r, 1), .Cells(r, 1)).EntireRow.Delete
r = r - 1
End If
r = r + 1
Loop
'Delete Extra Columns
.Range(.Cells(1, 48), .Cells(1, 100)).EntireColumn.Delete
.Range("AS1:AS1").EntireColumn.Delete
.Range("An1:Ap1").EntireColumn.Delete
.Range("Ak1:Al1").EntireColumn.Delete
.Range("Ah1:Ai1").EntireColumn.Delete
.Range("Ae1:Af1").EntireColumn.Delete
.Range("Ac1:Ac1").EntireColumn.Delete
.Range("X1:AA1").EntireColumn.Delete
.Range("T1:V1").EntireColumn.Delete
.Range("M1:P1").EntireColumn.Delete
.Range("H1:K1").EntireColumn.Delete
.Range("D1:E1").EntireColumn.Delete
.Range("B1:B1").EntireColumn.Delete
End With
End Sub