VBA - Filter columns, cut/paste rows new sheet, pivot table

outis_

New Member
Joined
Sep 28, 2024
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, newbie here. I can record a macro and use it, but don't understand the jargon to edit, and started looking into VBA only last week - totally clueless yet totally hooked.
Sorry if similar questions have been asked before. I learn best when I learn by doing, so I hope someone can help me with this.

Below is a sample dataset that needs to be manipulated, and the steps to be taken.
Would it be possible to streamline this, accommodating dynamic datasets?

Steps to sort data:
  • In ‘Prep’ tab, Filter column D ‘DonationType’ for ‘blanks’
  • Copy and paste rows into ‘Gift aid’ tab and delete visible rows in ‘Prep’ tab
  • In ‘Gift aid’ tab, create a pivot table with GiftAidAmount by Gift date
  • In ‘Prep’ tab, filter column D ‘DonationType’ for ‘Account Donation’, ‘Account Voucher’ or ‘Other Matched Giving’
  • Copy and paste rows into ‘Data entry’ tab and delete visible rows in ‘Prep’ tab
  • In ‘Prep’ tab, filter Column A ‘DonorName’ for text that contains ‘S/O ANON’ or ‘SO ANON’, copy and paste into ‘Data entry’ tab and delete visible rows in ‘Prep’ tab
  • Sort ‘Prep’ tab by ‘Surname’ (Column B)
In case it affects the choice of coding, the ordering does not matter, as long as the Prep tab is sorted by surname with relevant rows deleted, and the pivot table is created from the Gift aid tab.

I hope I have managed to explain everything well, but please let me know if I am missing anything.
Any help would be greatly appreciated, thanks.



VBA test.xlsx
ABCDEF
1DonorNameSurnameDonationAmountDonationTypeGiftAidAmountGift date
2LEAHYLEAHY100S/O001/07/2020
3Foundation500Account Voucher003/07/2020
4CHAPMANCHAPMAN10D/D003/07/2020
5BROWNBROWN5D/D003/07/2020
6ROBINSONROBINSON25D/D003/07/2020
7DAVIESDAVIES5D/D003/07/2020
8FrankFrank25Account Regular Donation004/07/2020
9HAIGHHaigh5Account Regular Donation004/07/2020
10SmithSmith10Account Regular Donation004/07/2020
11M J AY5000Account Voucher004/07/2020
12MOONEYMOONEY0304/07/2020
13HendersonHenderson10Account Regular Donation007/07/2020
14S/O ANON - xxxS/O ANON - xxx50S/O007/07/2020
15GARDNERGARDNER10S/O007/07/2020
16S/O ANON - yyyS/O ANON - yyy15S/O007/07/2020
17SO ANON - J C GSO ANON - J C G15S/O007/07/2020
18STEWARTSTEWART10S/O009/07/2020
19OWENOWEN5S/O015/07/2020
20ColleyColley5Account Regular Donation016/07/2020
21GRUGRUNDY02.517/07/2020
22PELLPELL01.2517/07/2020
23DAVIESDAVIES0417/07/2020
24PurserPurser01.2517/07/2020
25ELLISELLIS00.517/07/2020
26TalbotTalbot0318/07/2020
27DaveyDavey01.2518/07/2020
28MOONEYMOONEY2S/O021/07/2020
29BALDWINBALDWIN3D/D025/07/2020
30PELLPELL5D/D025/07/2020
31DAVIESDAVIES5D/D025/07/2020
32Gilson CGilson60Account Regular Donation027/07/2020
33SO ANON RSO ANON R10S/O028/07/2020
34JacksonJackson20Other Matched Giving031/07/2020
Prep





VBA test.xlsx
ABCDEF
1DonorNameSurnameDonationAmountDonationTypeGiftAidAmountGift date
2
3
4
Data entry



VBA test.xlsx
ABCDEF
1DonorNameSurnameDonationAmountDonationTypeGiftAidAmountGift date
2
3
4
Gift aid
 
Anyway, feel free to message me with any questions. This is very close to what you want I think. Place it in the Prep Sheet Code Module. One thing I still have to work on is not copying the first row a second time in Date Entry.
VBA Code:
Option Explicit

Sub filterDonation()
Application.ScreenUpdating = False
Const ard As String = "Account Regular Donation"
Const av As String = "Account Voucher"
Const omg As String = "Other Matched Giving"
Dim rng As Range, r As Range, i As Integer, lRow As Integer
Dim dSht As Worksheet, gSht As Worksheet, delRows() As Range
Set dSht = ThisWorkbook.Sheets("Data Entry"): Set gSht = ThisWorkbook.Sheets("Gift Aid")
Set rng = Me.UsedRange
rng.AutoFilter 4, "="
i = 1
For Each r In rng.Rows
    If r.Hidden = False Then
        r.Copy gSht.Rows(i)
        ReDim Preserve delRows(i - 1)
        Set delRows(i - 1) = r
        i = i + 1
    End If
Next r
rng.AutoFilter
For i = UBound(delRows) To 1 Step -1
    delRows(i).Delete
Next i
i = 1
rng.AutoFilter 4, Array(ard, av, omg), xlFilterValues
For Each r In rng.Rows
    If r.Hidden = False Then
        r.Copy dSht.Rows(i)
        ReDim Preserve delRows(i - 1)
        Set delRows(i - 1) = r
        i = i + 1
    End If
Next r
rng.AutoFilter
For i = UBound(delRows) To 1 Step -1
    delRows(i).Delete
Next i
i = 1
rng.AutoFilter 1, "SO*", xlOr, "S/O*"
rng.Offset(1, 0).Sort Cells(1, 3), xlAscending
lRow = dSht.UsedRange.Rows.Count + 1
For Each r In rng.Rows
    If r.Hidden = False Then
        r.Copy dSht.Rows(lRow)
        ReDim Preserve delRows(i - 1)
        Set delRows(i - 1) = r
        i = i + 1: lRow = lRow + 1
    End If
Next r
rng.AutoFilter
For i = UBound(delRows) To 1 Step -1
    delRows(i).Delete
Next i
rng.Offset(1, 0).Sort (Cells(1, 2))
rng.AutoFilter
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello @Skyybot , thank you for your replies and for explaining DIM/SET etc. I sort of get it, but I guess understanding also comes with practice and familiarity.

I tried your code on the sample dataset I posted. It came up with an error for me - 'Invalid use of Me Keyword'.
So, I changed it to this, and it worked - not that I fully understand what I'm doing, mind you!

Set rng = ThisWorkbook.Sheets("Prep").UsedRange

1727690295839.png


To fully test it, I edited your code for the complete dataset and.... now I have more questions! (I wasn't sure if you meant message you privately - I did have a look on this forum to see if it is possible, but couldn't find out how, so I'll just continue this thread. But please feel free to stop replying at any point. You have already helped me enormously - I also really enjoyed the little tasks that you asked me to try, they were fun - thanks for that!)

Just to explain what I've done -
I've saved your code as a separate Macro (this is the second macro I am trying to run on this data)
When I work on my dataset, I run the first macro. Then when I try to run your one, I get 'Run-time error '9': Subscript out of range' and this is the line highlighted:

1727691709756.png


However! If, in the dataset I run the first macro, then open VBE, insert Module, and copy and paste the code, it works.
So, my question is: how can I make your code run as a macro without opening VBE and inserting Module? The coding is the same in both instances, so it seems to me that it has something to do with where I am applying the code from...

Hope this makes sense, but let me know if my question is not very clear. but again, please don't feel you need to keep replying. The code you posted above is already of great help, and I will use it as a model to learn from (and hopefully adapt it for other datasets I have). I'm actually going back to university on Wednesday, so that's why I've been trying to learn VBA in attempt to automate my job (as much as I can), because I really don't know how I am going to manage the workload!

Thanks again @Skyybot , and greetings from rainy England.
 
Upvote 0
@outis_ , I imagine the keywords are throwing an error because of code location. If the code is in a standard module Me and ThisWorkbook won't work. You will have to declare explicitly.
 
Upvote 0
Solution

Forum statistics

Threads
1,222,905
Messages
6,168,948
Members
452,227
Latest member
sam1121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top