VBA code to copy daily data to separate worksheets

sj84

New Member
Joined
Oct 17, 2020
Messages
2
Office Version
  1. 365
Hi,

Apologies for a repeated question - I've found a bunch of similar questions, but I don't have solid enough VBA skills to be able to adapt to my specific query.

I have a single row summary table (TABLE 1), populated after various values are input across a related table (TABLE 2). Column A on TABLE 1 is a manually input date and various values go in columns B-H.

There are then separate tabs for each month. Each tab has a table the same as TABLE 1, except is has a row for each day of the month.

I need a macro that will copy values from TABLE 1 to the row corresponding with the date currently on TABLE 1, on the relevant month tab, and then leave it unchanged when a new date is input on TABLE 1 and the macro is run again.

Appreciate any help, thanks.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Welcome to MrExcel

Table 1 and Table 2 assumed to be on separate sheets and that they are the only tables on those sheets
Monthly sheet names assumed to be Jan Feb Mar ...Dec
Run the code when cursor is on any cell in Table 1
Code assigns that row's values to a new row in the appropriate monthly sheet

Amend code below to match your monthly sheet names
VBA Code:
Sub CopyValues()
    Dim wsMonth As Worksheet, dateCell As Range, copyThis As Range, pasteHere As Range, Tbl As ListObject
    On Error Resume Next
        Debug.Print ActiveCell.ListObject.Name
        If Err.Number > 0 Then
            MsgBox "active cell must be located inside table"
            Exit Sub
        End If
    On Error GoTo 0
    Set copyThis = Intersect(ActiveCell.EntireRow, ActiveSheet.ListObjects(1).Range)
    Set dateCell = copyThis.Cells(1, 1)
    Set wsMonth = Sheets(Format(copyThis.Cells(1, 1), "MMM"))
    Set Tbl = wsMonth.ListObjects(1)
'add row for latest values at end of table
    Tbl.ListRows.Add AlwaysInsert:=True
    Set pasteHere = Tbl.ListRows(Tbl.ListRows.Count).Range
'test to see if date already exists
    If WorksheetFunction.CountIf(Tbl.ListColumns(1).Range, dateCell) = 0 Then
        pasteHere.Value = copyThis.Value
    Else
        MsgBox dateCell & " already exists in " & Tbl.Parent.Name
    End If
End Sub
 
Upvote 0
Welcome to MrExcel

Table 1 and Table 2 assumed to be on separate sheets and that they are the only tables on those sheets
Monthly sheet names assumed to be Jan Feb Mar ...Dec
Run the code when cursor is on any cell in Table 1
Code assigns that row's values to a new row in the appropriate monthly sheet

Amend code below to match your monthly sheet names
VBA Code:
Sub CopyValues()
    Dim wsMonth As Worksheet, dateCell As Range, copyThis As Range, pasteHere As Range, Tbl As ListObject
    On Error Resume Next
        Debug.Print ActiveCell.ListObject.Name
        If Err.Number > 0 Then
            MsgBox "active cell must be located inside table"
            Exit Sub
        End If
    On Error GoTo 0
    Set copyThis = Intersect(ActiveCell.EntireRow, ActiveSheet.ListObjects(1).Range)
    Set dateCell = copyThis.Cells(1, 1)
    Set wsMonth = Sheets(Format(copyThis.Cells(1, 1), "MMM"))
    Set Tbl = wsMonth.ListObjects(1)
'add row for latest values at end of table
    Tbl.ListRows.Add AlwaysInsert:=True
    Set pasteHere = Tbl.ListRows(Tbl.ListRows.Count).Range
'test to see if date already exists
    If WorksheetFunction.CountIf(Tbl.ListColumns(1).Range, dateCell) = 0 Then
        pasteHere.Value = copyThis.Value
    Else
        MsgBox dateCell & " already exists in " & Tbl.Parent.Name
    End If
End Sub
Thanks very much for the reply. A couple of things I should have made clearer.

1. I shouldn't have used the word "TABLE", as while this is data presented as a table, none of it is formatted as an actual Excel table.

2. The month tabs contain other data / formatting where adding a row to the table may cause issues. Is there anything that can be done using a lookup so if the dates are already populated on the month tabs, then the corresponding data from what I originally labelled as "TABLE 1" can be copied across in the columns next to it?
 
Upvote 0
Ok - will post amended code when back at PC later today

Is this what you want?
- copy the values in chosen row (columns B:H) to B:H in month's sheet
- date lookup in column A determines row
 
Upvote 0
Data
Book1
ABCDEFGH
1DateVal1Val2Val3Val4Val5Val6Val7
201/01/2020B2C2D2E2F2G2H2
302/01/2020B3C3D3E3F3G3H3
403/01/2020B4C4D4E4F4G4H4
504/01/2020B5C5D5E5F5G5H5
605/01/2020B6C6D6E6F6G6H6
706/01/2020B7C7D7E7F7G7H7
807/01/2020B8C8D8E8F8G8H8
908/01/2020B9C9D9E9F9G9H9
1009/01/2020B10C10D10E10F10G10H10
11
Sheet1


Monthly Sheet - BEFORE
Book1
ABCDEFGH
1DateVal1Val2Val3Val4Val5Val6Val7
201/01/2020B2C2D2E2F2G2H2
302/01/2020B3C3D3E3F3G3H3
403/01/2020B4C4D4E4F4G4H4
504/01/2020B5C5D5E5F5G5H5
605/01/2020B6C6D6E6F6G6H6
706/01/2020B7C7D7E7F7G7H7
807/01/2020B8C8D8E8F8G8H8
908/01/2020B9C9D9E9F9G9H9
1009/01/2020
1110/01/2020
1211/01/2020
1312/01/2020
1413/01/2020
1514/01/2020
1615/01/2020
1716/01/2020
1817/01/2020
Jan


Monthly Sheet - AFTER
Book1
ABCDEFGH
1DateVal1Val2Val3Val4Val5Val6Val7
201/01/2020B2C2D2E2F2G2H2
302/01/2020B3C3D3E3F3G3H3
403/01/2020B4C4D4E4F4G4H4
504/01/2020B5C5D5E5F5G5H5
605/01/2020B6C6D6E6F6G6H6
706/01/2020B7C7D7E7F7G7H7
807/01/2020B8C8D8E8F8G8H8
908/01/2020B9C9D9E9F9G9H9
1009/01/2020B10C10D10E10F10G10H10
1110/01/2020
1211/01/2020
1312/01/2020
1413/01/2020
1514/01/2020
1615/01/2020
1716/01/2020
Jan


With active cell anywhere in correct row, run this code
VBA Code:
Sub CopyValues()
    Dim WF As WorksheetFunction, wsMonth As Worksheet, Msg As String
    Dim dateCell As Range, lookHere As Range, pasteHere As Range, B_H As Range
    
    Set WF = Application.WorksheetFunction
    Set dateCell = Range("A" & ActiveCell.Row)
    Set B_H = dateCell.Offset(, 1).Resize(, 7)
    Set wsMonth = Sheets(Format(dateCell, "MMM"))
    Set lookHere = wsMonth.Range("A:A")

    If WF.CountIf(lookHere, dateCell) = 0 Then
        Msg = "OOPS!" & vbCr & dateCell & " Not Found"
    Else
        Set pasteHere = lookHere.Find(dateCell).Offset(, 1).Resize(, 7)
        If WF.CountBlank(pasteHere) = 7 Then
            pasteHere.Value = B_H.Value
            Msg = "OK"
        Else
            Msg = "OOPS!" & vbCr & dateCell & " already contains values"
        End If
    End If
    MsgBox Msg
    
End Sub
 
Upvote 0
I am no longer watching this thread
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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