VBA code to specify sheets depending on date

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a spreadsheet that transfers rows from a excel table to sheets depending on the date. The reporting month of where I work goes from the 26th of the previous month to the 25th of the current month. I had it all working when the table in my home sheet had one row but now it has multiple rows. I extracted the month from the current date and checking the date using these functions:

Column W in my table:
Code:
=TEXT(DATE(YEAR(A5),IF(DAY(A5)<26,MONTH(A5),MONTH(A5)+1),1), "mmmm")

Column X in my table:
Code:
=TEXT(DATE(YEAR(A5),IF(DAY(A5)<26,MONTH(A5),MONTH(A5)+1),1), "yyyy")

column Y in my table:
Code:
=CONCATENATE(W5," ",X5)




With one row, I used the following code to get combo:
Code:
 Combo = Worksheets("Home").Range("Y5")

With the columns W, X and Y, the same for each row in the table.


Norie helped me with my code to copy rows to relevant sheets and suggested I just use the date column to extract the name of the monthly sheet to transfer the row to. I forgot about needing to move the rows to months, not just depending on the month, but depending on being between the 25th and 26th.

Code:
Sub cmdCopyo()
Dim wsDst As Worksheet
Dim wsSrc As Worksheet
Dim tblrow As ListRow
Dim Combo As String
Dim sht As Worksheet
Dim tbl As ListObject

    Application.ScreenUpdating = False
    
    'assign values to variables
    Set sht = Worksheets("Home")

    With sht

        Set tbl = .ListObjects("tblCosting")
        
        
        
        For Each tblrow In tbl.ListRows
           [COLOR=#ff0000] Combo = Format(tblrow.Range.Cells(1, 1), "mmmm yyyy")[/COLOR]
            Set wsDst = Sheets(Combo)
            
            With wsDst
                'This copies the first 10 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
                tblrow.Range.Resize(, 10).copy
                .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
                'This should go to the 15th column in the current row, i.e. column O, and copy that column and the next 2 columns, i.e. O:Q, to column K on the destination sheet.
                tblrow.Range.Offset(, 14).Resize(, 3).copy
                .Range("K" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
                'Similarly this should copy columns AD:AF from the table to column N on the destination sheet.
                tblrow.Range.Offset(, 29).Resize(, 3).copy
                .Range("N" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
            End With
            
        Next tblrow
        
        Call SortDates
        
    End With
    
    Application.CutCopyMode = False

    Application.ScreenUpdating = True

I am not very good at writing vba and this is my copy code above. I need to change the highlighted line so that combo refers to the value in column Y for each row in the table, not just hard coded in the be cell Y5.

Can I have some help please?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
This should do it
Code:
Combo = Format(tblrow.Range.Cells(1, [COLOR=#ff0000]25[/COLOR]), "mmmm yyyy")
 
Last edited:
Upvote 0
Do you need columns W an X ?

Could get column Y data with :
=TEXT(DATE(YEAR(A5),MONTH(A5)+(DAY(A5)>26),1), "mmmm yyyy")
 
Upvote 0
That's just how I did it. I could change it now but there are too many references to cells in vba to bother doing that.
 
Upvote 0

Forum statistics

Threads
1,215,568
Messages
6,125,599
Members
449,238
Latest member
wcbyers

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