MoonDancer
New Member
- Joined
- May 4, 2024
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
Hi all,
Ok, so I feel a little guilty coming here to ask for help. I'm an absolute novice with anything Macro or VBA related so I'm pretty much going to be need leading by the hand (so sorry). That is what seems to have come from having big ideas with little to no idea of how to execute what I want.
Some background info on what data I need to cleanup:
Using Power BI, I export a report in Excel format. This data set always has Columns A - Z but the number of rows can vary.
At the very bottom of the sheet, when I use Ctrl + Down, is a Totals Row, followed by a blank row and another row with data in. All 3 of these rows need to be deleted.
Column A usually contains multiple duplicates. All rows with a duplicate number in column A need to be removed.
Next, filters are inserted across the header row (1). In Column J, there will be the following entries:
Approved
Awaiting Service Approval
Awaiting Value Approval
Cancelled
Created
Rejected
I need to remove all rows on the spreadsheet except the ones with "Approved" in column J.
I have tried using Record Macro, but it is only recording the rows I need to execute the Macro on from this data set. If I run it on another import, I am guessing the range will stay the same, whereas, I need it to be variable depending on the total number of rows on any export.
I've tried and tried to attach an image I took (on phone camera) of the steps done after recording the Macro, but it will not attach no matter what I do (file too big all the time).
I've had to try copy and pasting the coding below instead and putting in gaps between each bit (I did keep returning to A1 to ensure I could see where my active cell was at any point).
Sub Test Cleanup Approvals()
Test_Cleanup_Approvals
' Test Cleanup Approvals Macro ' Clean up the data -
Selection.End(xlDown) .Select
Rows ("5423:5425") .Select
Selection.Delete Shift:=xlUp
Rows ("l:1").Select
Selection.AutoFilter
Cells.Select
ActiveSheet.Range ("$A$l:$Z$5425").RemoveDuplicates Columns:=1, Header:= xlYes
Range ("Al") .Select
ActiveWorkbook.Worksheets ("Export") .AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Export").AutoFilter.Sort.SortFields.Add2 Key:= Range ("J2:J4040"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption :=xlSortNormal
With ActiveWorkbook.Worksheets("Export").AutoFilter.Sort .Header = xlYes .MatchCase = False . Orientation - xlTopToBottom . SortMethod = xlPinYin . Apply
End With
ActiveSheet.Range ("$A$l:$Z$4040").AutoFilter Field:=10, Criterial:=Arrayc "Awaiting Service Approval", "Awaiting Value Approval", "Cancelled", "Created" "Rejected"), OperatorlFiltervalues
Rows ("3856:3856").Select
Range (Selection, Selection.End(xlDown)).Select
Selection.Delete ShiftlUp
Range ("Al") .Select
ActiveSheet.Range ("$A$l:$Z$3855") .AutoFilter Field:=10
End Sub
Can anyone please help me with amending the code to allow for variable rows of data? I'm guessing I can go in and manually change the recorded Macro, or would it be better to type it all in the VBA as a new module?
There is another column that I need to remove lots of unwanted data from but I think perhaps there are too many variables? In this one, the field is populates in the original program with a name. When an action has been taken, an "R" is then usually inserted in the front. Unfortunately the format this is put in can vary wildly from colleague to colleague who happens to be doing it. Some input R - (name), others do R (name), R- (name) and so on. Removing duplicates won't work because entries without that "R" would also be affected if there are multiple with the same name, I.e. "Joe Bloggs". Filtering is an absolute pain as not only are all the "R's" put in with variable formats, but also there could be any one of hundreds of different names in the field too, making it an absolute nightmare and takes ages to delete all of them. There are some others to remove too, but the "R" prefix ones are by far the worst and can be in their 1000s line wise. Not sure if anything can be done or if this has to be done manually?
So sorry for the huge message! Posting in the hope someone will save me! I've tried to code in the VBA but I've not a clue what I'm doing.
Faye
EDIT: Sorry, random angry faces in there. Think it should have been : = x (without spaces)
Ok, so I feel a little guilty coming here to ask for help. I'm an absolute novice with anything Macro or VBA related so I'm pretty much going to be need leading by the hand (so sorry). That is what seems to have come from having big ideas with little to no idea of how to execute what I want.
Some background info on what data I need to cleanup:
Using Power BI, I export a report in Excel format. This data set always has Columns A - Z but the number of rows can vary.
At the very bottom of the sheet, when I use Ctrl + Down, is a Totals Row, followed by a blank row and another row with data in. All 3 of these rows need to be deleted.
Column A usually contains multiple duplicates. All rows with a duplicate number in column A need to be removed.
Next, filters are inserted across the header row (1). In Column J, there will be the following entries:
Approved
Awaiting Service Approval
Awaiting Value Approval
Cancelled
Created
Rejected
I need to remove all rows on the spreadsheet except the ones with "Approved" in column J.
I have tried using Record Macro, but it is only recording the rows I need to execute the Macro on from this data set. If I run it on another import, I am guessing the range will stay the same, whereas, I need it to be variable depending on the total number of rows on any export.
I've tried and tried to attach an image I took (on phone camera) of the steps done after recording the Macro, but it will not attach no matter what I do (file too big all the time).
I've had to try copy and pasting the coding below instead and putting in gaps between each bit (I did keep returning to A1 to ensure I could see where my active cell was at any point).
Sub Test Cleanup Approvals()
Test_Cleanup_Approvals
' Test Cleanup Approvals Macro ' Clean up the data -
Selection.End(xlDown) .Select
Rows ("5423:5425") .Select
Selection.Delete Shift:=xlUp
Rows ("l:1").Select
Selection.AutoFilter
Cells.Select
ActiveSheet.Range ("$A$l:$Z$5425").RemoveDuplicates Columns:=1, Header:= xlYes
Range ("Al") .Select
ActiveWorkbook.Worksheets ("Export") .AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Export").AutoFilter.Sort.SortFields.Add2 Key:= Range ("J2:J4040"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption :=xlSortNormal
With ActiveWorkbook.Worksheets("Export").AutoFilter.Sort .Header = xlYes .MatchCase = False . Orientation - xlTopToBottom . SortMethod = xlPinYin . Apply
End With
ActiveSheet.Range ("$A$l:$Z$4040").AutoFilter Field:=10, Criterial:=Arrayc "Awaiting Service Approval", "Awaiting Value Approval", "Cancelled", "Created" "Rejected"), OperatorlFiltervalues
Rows ("3856:3856").Select
Range (Selection, Selection.End(xlDown)).Select
Selection.Delete ShiftlUp
Range ("Al") .Select
ActiveSheet.Range ("$A$l:$Z$3855") .AutoFilter Field:=10
End Sub
Can anyone please help me with amending the code to allow for variable rows of data? I'm guessing I can go in and manually change the recorded Macro, or would it be better to type it all in the VBA as a new module?
There is another column that I need to remove lots of unwanted data from but I think perhaps there are too many variables? In this one, the field is populates in the original program with a name. When an action has been taken, an "R" is then usually inserted in the front. Unfortunately the format this is put in can vary wildly from colleague to colleague who happens to be doing it. Some input R - (name), others do R (name), R- (name) and so on. Removing duplicates won't work because entries without that "R" would also be affected if there are multiple with the same name, I.e. "Joe Bloggs". Filtering is an absolute pain as not only are all the "R's" put in with variable formats, but also there could be any one of hundreds of different names in the field too, making it an absolute nightmare and takes ages to delete all of them. There are some others to remove too, but the "R" prefix ones are by far the worst and can be in their 1000s line wise. Not sure if anything can be done or if this has to be done manually?
So sorry for the huge message! Posting in the hope someone will save me! I've tried to code in the VBA but I've not a clue what I'm doing.
Faye
EDIT: Sorry, random angry faces in there. Think it should have been : = x (without spaces)
Last edited by a moderator: