vba History Log of Data

MM91

Board Regular
Joined
Nov 29, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Hello! I am newish to vba and I am trying to take data from one worksheet (sheet1) to clear all data in cells and then save history of data to new sheet(sheet2) to a table (historytable). It just needs to take the final total and record it under the correct name. Not sure where to start thank you! If there is a better way to save the data open to any suggestions!

Sheet1

macro.png


Sheet 2
macro 2.png
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
hy mm91,

try the following code
VBA Code:
Sub abc()
Dim d_lr As Long
Dim d_lc As Long
Dim h_lr As Long
Dim h_lc As Long

Dim d_lookup_rng As Range
Dim d_srch_rng As Range

d_lr = Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
d_lc = Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column

h_lr = Sheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Row


If Sheets("sheet2").Cells(h_lr, 1) = "" Then
    h_lr = 2
Else
    h_lr = h_lr + 1
End If

Set d_lookup_rng = Sheets("sheet1").Range(Sheets("sheet1").Cells(1, 2), Sheets("sheet1").Cells(1, d_lc))
Set d_srch_rng = Sheets("sheet2").Range("b1:z1")

EDate = InputBox("Please input date for Data Logging", "Provide Date")
EDate = CDate(EDate)

Dim rfind As Range
h_lc = Sheets("sheet2").Cells(1, 1).End(xlToLeft).Column

Sheets("sheet2").Cells(h_lr, 1) = EDate
For Each cell In d_lookup_rng
   
    If IsError(Application.Match(cell, d_srch_rng, 0)) Then
        h_lc = Sheets("sheet2").Cells(1, Columns.Count).End(xlToLeft).Column
        Sheets("sheet2").Cells(1, h_lc + 1) = cell
        Set rfind = Sheets("sheet1").Range(d_lookup_rng.Address).Find(what:=cell.Value, Lookat:=xlWhole)
      
        Sheets("sheet2").Cells(h_lr, h_lc + 1) = WorksheetFunction.Sum(Sheets("sheet1").Range(Sheets("sheet1").Cells(2, rfind.Column), Sheets("sheet1").Cells(d_lr, rfind.Column)))
    Else
            Set rfind = Sheets("sheet1").Range(d_lookup_rng.Address).Find(what:=cell.Value, Lookat:=xlWhole)
            Set dfind = Sheets("sheet2").Range(d_srch_rng.Address).Find(what:=cell.Value)
            Sheets("sheet2").Cells(h_lr, dfind.Column) = WorksheetFunction.Sum(Sheets("sheet1").Range(Sheets("sheet1").Cells(2, rfind.Column), Sheets("sheet1").Cells(d_lr, rfind.Column)))
    End If
Next cell

   
End Sub

The code asks the user for the input date, then looks for all of the given names available in sheet1, one at a time, in sheet2. if found, it sums all figures against these names and place them in corresponding column in sheet2. if a given name is not found in sheet2 then it creates a new entry of that name and then performs the sum.



hope this helps.
 
Upvote 0
Solution
Thats great! perfect start for me and I can customize! Thank you so much!
 
Upvote 0
hy mm91,

try the following code
VBA Code:
Sub abc()
Dim d_lr As Long
Dim d_lc As Long
Dim h_lr As Long
Dim h_lc As Long

Dim d_lookup_rng As Range
Dim d_srch_rng As Range

d_lr = Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
d_lc = Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column

h_lr = Sheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Row


If Sheets("sheet2").Cells(h_lr, 1) = "" Then
    h_lr = 2
Else
    h_lr = h_lr + 1
End If

Set d_lookup_rng = Sheets("sheet1").Range(Sheets("sheet1").Cells(1, 2), Sheets("sheet1").Cells(1, d_lc))
Set d_srch_rng = Sheets("sheet2").Range("b1:z1")

EDate = InputBox("Please input date for Data Logging", "Provide Date")
EDate = CDate(EDate)

Dim rfind As Range
h_lc = Sheets("sheet2").Cells(1, 1).End(xlToLeft).Column

Sheets("sheet2").Cells(h_lr, 1) = EDate
For Each cell In d_lookup_rng
  
    If IsError(Application.Match(cell, d_srch_rng, 0)) Then
        h_lc = Sheets("sheet2").Cells(1, Columns.Count).End(xlToLeft).Column
        Sheets("sheet2").Cells(1, h_lc + 1) = cell
        Set rfind = Sheets("sheet1").Range(d_lookup_rng.Address).Find(what:=cell.Value, Lookat:=xlWhole)
     
        Sheets("sheet2").Cells(h_lr, h_lc + 1) = WorksheetFunction.Sum(Sheets("sheet1").Range(Sheets("sheet1").Cells(2, rfind.Column), Sheets("sheet1").Cells(d_lr, rfind.Column)))
    Else
            Set rfind = Sheets("sheet1").Range(d_lookup_rng.Address).Find(what:=cell.Value, Lookat:=xlWhole)
            Set dfind = Sheets("sheet2").Range(d_srch_rng.Address).Find(what:=cell.Value)
            Sheets("sheet2").Cells(h_lr, dfind.Column) = WorksheetFunction.Sum(Sheets("sheet1").Range(Sheets("sheet1").Cells(2, rfind.Column), Sheets("sheet1").Cells(d_lr, rfind.Column)))
    End If
Next cell

  
End Sub

The code asks the user for the input date, then looks for all of the given names available in sheet1, one at a time, in sheet2. if found, it sums all figures against these names and place them in corresponding column in sheet2. if a given name is not found in sheet2 then it creates a new entry of that name and then performs the sum.



hope this helps.
Hi I am trying to modify the sheet but the code does not seem to work as I need it to. It adds the new names to the top row instead of into the table. To make it more simple I added a row that totals the numbers so I just need the history table to record row 23. Any suggestions on how to modify the code or do it in another way? Thank you so much!!
 

Attachments

  • macro 3.png
    macro 3.png
    26.4 KB · Views: 9
  • macro 4.png
    macro 4.png
    9.7 KB · Views: 10
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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