I am trying to prep an Excel file for import.
I used the macro recorder to get the code to remove the header and shift the rows up for my column headers for my temp table in Access.
I am trying to find a specific value in a column, then delete everything below it.
Public Function FormatSPExcel()
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim strfile As String
Dim r As Range ' range variable
Dim d As String ' rows to delete variable
Set fd = Application.FileDialog(3)
With fd
.allowmultiselect = False
.Filters.Clear
.Filters.Add "Excel Files", "*.xls*"
If .Show Then
strfile = .selecteditems(1)
End If
End With
Set xl = New Excel.Application
Set wb = xl.Workbooks.Open(strfile)
Set ws = wb.Sheets("Sheet0")
'deletes and cleans up header
ws.Shapes.Range(Array("Picture 1")).Select
Selection.Delete
Rows("1:21").Select
Selection.Delete Shift:=xlUp
'delete footer up to and including total
Set r = ws.Range("A:K").Find("Total", lookin:=xlValues)
Do While Not r Is Nothing
d = "A:K" & r.Row + 30
ws.Range(d).Delete xlShiftUp
Set r = ws.Range("A:K").Find("Total", lookin:=xlValues)
Loop
wb.Save
xl.Quit
Set xl = Nothing
End Function
This fails at ActiveSheet.Range(d).Delete xlShiftUp
and says
Application-defined or object-defined error
I cobbled this together from various things across StackOverflow and other sites. It is finding the row but I think I did the delete part wrong.
I want to find the row with a specific value, in this case "Total" then store as string to delete that row and a large chunk of rows below it.