[VBA] Copy data as new row under the corresponding category

CurlyQ12391

New Member
Joined
Jan 16, 2023
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Hello! I am quite familiar with Excel, but new to VBA and would love any assistance that could be provided. I did find a similar post for my situation, but I wasn't able to leverage the code in a way that would work for me (probably my fault).

I am wanting to automate my monthly financial tracker. I currently download my account activity on a routine basis, manually categorize it, and then cut it into categorized sections so I can track the amount spent for each category. It seems like I could potentially download my account activity, still manually categorize it, and then have VBA run to copy the categorized data from one tab into another (as a new row).

Hopefully the example image gives a better idea of what I want to do.
Sheet4 = Where I would paste Account Activity data (manually remove duplicates and categorize)
Sheet 3 = Where I want VBA to paste new rows of data (probably to the top of the corresponding section)

Example: Hulu charge is categorized in Sheet4, column D as "TV Streaming", so data from columns A-C would be pasted under the TV Streaming section of Sheet 3.

Additionally, if you have any suggestions on a better way to build this I am open!
 

Attachments

  • VBA Transaction Example.PNG
    VBA Transaction Example.PNG
    100.7 KB · Views: 21

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello!

I've been working on this all day since posting and have made some headway, but now two more hurdles with the code I leveraged from here.

VBA Code:
Sub CopyRows()
    
    ' 1. Reference the workbook ('wb').
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' 2. Source
     ' Calculate the source last row ('slRow'),
    ' the row of the last non-empty cell in the column.
    Dim slRow As Long: slRow = Sheet4.Cells(Sheet4.Rows.Count, "D").End(xlUp).Row
    ' Reference the source columns range ('scrg') whose rows will be copied.
    Dim scrg As Range: Set scrg = Sheet4.Columns("A:C")
    
    ' 3. Destination
    
    ' Reference the first destination row range by resizing the first
    ' destination cell by the number of columns of the source columns range.
    Dim drrg As Range: Set drrg = Sheet3.Range("B3").Resize(, scrg.Columns.Count)
    ' Write the lookup string value to a string variable ('dlString').
    Dim dlString As String: dlString = CStr(Sheet3.Range("A2").Value)
    
    ' 4. The Loop
    
    ' Declare additional variables.
    Dim srrg As Range ' Current Source Row Range
    Dim sr As Long ' Current Row in the Source Worksheet
    Dim slString As String ' Current String Lookup String
    
    ' Loop through the designated rows of the source worksheet.
    For sr = 3 To slRow
        ' Write the source string value in the current row to a variable.
        slString = CStr(Sheet4.Cells(sr, "D").Value)
        ' Compare the string in the current row against the lookup string.
        ' The comparison is case-insensitive i.e. 'dog = DOG'
        ' due to the 'vbTextCompare' parameter.
        If StrComp(slString, dlString, vbTextCompare) = 0 Then ' is equal
            ' Reference the source row range.
            Set srrg = scrg.Rows(sr)
            ' Write the values from the source row range
            ' to the destination row range ('copy by assignment').
            drrg.Value = srrg.Value
            ' Reference the next destination row range (one row below).
            Set drrg = drrg.Offset(1)
            'Else ' is not equal; do nothing
        End If
    Next sr
    
    ' 5. Inform to not wonder if the code has run or not.
    MsgBox "Rows copied.", vbInformation
        
End Sub

With some minor adjustments I was able to get the macro to successfully run and transfer some data from the source tab (Sheet4). Unfortunately, the data...
  1. Is only searching for the Rx/Dr category, probably because I specified cell "A2" in Line 19. Is there a way for that to be a variable search for any category in column A?

  2. The rows copied over in the order I wanted, but they didn't insert themselves so they were pasted into other categories (see screenshot). What can I add to the code that would tell it to insert the copied cells and not just paste them?
 

Attachments

  • VBA Transaction Results.PNG
    VBA Transaction Results.PNG
    13.9 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,215,863
Messages
6,127,394
Members
449,382
Latest member
DonnaRisso

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