I'm having trouble with a macro that I'm trying to build to do many steps.
Our financial system exports budget data in terribly unhelpful excel files (thus necessitating the continued use of the pdf reports within the program). However, I have been asked to create a budget planning tool that will work from these poorly delineated excel exports.
Data exports look like: UglyExport.xlsx
(actual data has been altered for security reasons)
My macro thus far:
Option Explicit
Sub Delete_Based_on_Page()
' This macro will delete an entire row based on the presence of a
'predefined word or set of words. If that word or set of words is
'found in a cell, in a specified column, the entire row will be deleted
Dim x As Long
Dim Z As Long
Dim lastrow As Long
Dim FoundRowToDelete As Boolean
Dim OriginalCalculationMode As Long
Dim RowsToDelete As Range
Dim SearchItems() As Variant
Dim DataStartRow As Long
Dim SearchColumn As String
Dim SheetName As String
' Choose the row you want the search and delete to start on
' Choose the column to search and delete to use for deletion
' Choose the sheet in the workbook you want this macro to be run on
DataStartRow = 4
SearchColumn = "A"
SheetName = "Sheet1"
' Enter the terms you want to be used for criteria for deletion
' All terms entered below are CASE SENSITIVE and need to be
'seperated by a comma
SearchItems = Array("Page", "Services", "Supplies", "Capital", "Costs", "TOTAL", "Salaries")
On Error GoTo Whoops
OriginalCalculationMode = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
With Worksheets(SheetName)
lastrow = .Cells(.Rows.Count, SearchColumn).End(xlUp).Row
For x = lastrow To DataStartRow Step -1
FoundRowToDelete = False
For Z = 0 To UBound(SearchItems)
If InStr(.Cells(x, SearchColumn).Value, SearchItems(Z)) Then
FoundRowToDelete = True
Exit For
End If
Next
If FoundRowToDelete Then
If RowsToDelete Is Nothing Then
Set RowsToDelete = .Cells(x, SearchColumn)
Else
Set RowsToDelete = Union(RowsToDelete, .Cells(x, SearchColumn))
End If
If RowsToDelete.Areas.Count > 100 Then
RowsToDelete.EntireRow.Delete
Set RowsToDelete = Nothing
End If
End If
Next
End With
If Not RowsToDelete Is Nothing Then
RowsToDelete.EntireRow.Delete
End If
Whoops:
Application.Calculation = OriginalCalculationMode
Application.ScreenUpdating = True
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Dim rngFound As Range
Dim strSearch As Variant
strSearch = Array("11", "12", "13", "21", "31", "32", "33", "34", "35", "36", "41", "51", "52", "53", "61", "81")
Set rngFound = Cells.Find(What:=strSearch, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
rngFound.Cut Destination:=rngFound.Offset(1, -1)
Set rngFound = Nothing
End Sub
So far I have found various posts that have gotten me to where I am now. I have copied code from several places, hacked it until I made it do what I wanted, and now I'm stuck.
Goals for the macro:- Delete rows with subtotals (those with words like Salaries and TOTAL) to leave only data per line item
- Insert a column
- Find and move specific strings (11, 12, 13, etc) back one column and down one row
- Copy the found string (11, 12, 13, etc) down to the next empty row
- delete the empty rows
- format the data as a table
So far - I can get the first two objectives to work perfectly as I want them.
I can get part of the third objective to work, but not all. I need it to search for each instance of the selected strings, but it only finds the first instance of 11 and then stops.
specific code for that part:
Dim rngFound As Range
Dim strSearch As Variant
strSearch = Array("11", "12", "13", "21", "31", "32", "33", "34", "35", "36", "41", "51", "52", "53", "61", "81")
Set rngFound = Cells.Find(What:=strSearch, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
rngFound.Cut Destination:=rngFound.Offset(1, -1)
Set rngFound = Nothing
Since I copied all of this from other places, I don't really know what I'm doing here, so I don't know how to fix it.
I'm fairly certain I can create the other parts of the macro by myself, but this part is causing me troubles I can't seem to fix.
Any and all help is greatly appreciated!!!
Bookmarks