How to keep a log of "Usage of a macro"

rbysetty

New Member
Joined
Jul 3, 2012
Messages
22
Hello, I have a rather silly problem. I have a macro (linked to a button) which copies cells A1:A2 from one worksheet (namedFP) to another worksheet (Log). I intend to copy these 2 cells on the log sheet every time i hit the macro button. The problem I am facing right now is that when I use the button multiple times, these cells are getting copied over each other instead of using the next available row to paste the cells.
this is what i have now and I tried changing the 'Rowcount+1' to 'rowcount+2' but that did not work. Any help is appreciated.

DHRSheet.Select
Range("A1:A2").Select
Selection.Copy

LogSheet.Select
RowCount = LogSheet.UsedRange.Rows.Count
Dim r As Integer
r = RowCount + 2
Dim infocell As Range
Set infocell = Cells(r, 1)
infocell.Select
ActiveSheet.Paste
infocell.Value = DHRSheet.Name & "$" & infocell.Value

DHRSheet.Select
ActiveWorkbook.Save
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Instead of all this selecting, you could do something like:

Code:
Dim SourceRange as Range
Dim DestinationRange as Range

Set SourceRange = DHRSheet.Range("A1:A2")
Set DestinationRange = LogSheet.Cells(Rows.Count, "A").End(xlup).Offset(1,0)

With DestinationRange
    .Resize(2,1).Value = SourceRange.Value
    .Cells(1,1).Value = DHRSheet.Name & "$" & .Cells(1,1).Value
End With

The .End(xlUp) will get the last filled cell in LogSheet!A:A and .Offset(1,0) is the cell below that.
 
Upvote 0
For example:

Code:
Sub test()

    With LogSheet
        With .Range("A" & .UsedRange.Rows.Count + 1)
            .Value = DHRSheet.Name & "$" & DHRSheet.Range("A1").Value
            .Offset(1).Value = DHRSheet.Range("A2").Value
        End With
        .Parent.Save
    End With


End Sub
 
Upvote 0
Hello, try:
Code:
Sub test()
    Dim lLr&
    lLr = Worksheets("LogSheet").Cells(Rows.Count, "A").End(xlUp).Row
    Worksheets("LogSheet").Range("A" & lLr + 1 & ":A" & lLr + 2 & "") = _
    Worksheets("DHRSheet").Range("A1:A2").Value
End Sub
 
Upvote 0
Hello, I have a rather silly problem. I have a macro (linked to a button) which copies cells A1:A2 from one worksheet (namedFP) to another worksheet (Log). I intend to copy these 2 cells on the log sheet every time i hit the macro button. The problem I am facing right now is that when I use the button multiple times, these cells are getting copied over each other instead of using the next available row to paste the cells.
this is what i have now and I tried changing the 'Rowcount+1' to 'rowcount+2' but that did not work. Any help is appreciated.

DHRSheet.Select
Range("A1:A2").Select
Selection.Copy

LogSheet.Select
RowCount = LogSheet.UsedRange.Rows.Count
Dim r As Integer
r = RowCount + 2
Dim infocell As Range
Set infocell = Cells(r, 1)
infocell.Select
ActiveSheet.Paste
infocell.Value = DHRSheet.Name & "$" & infocell.Value

DHRSheet.Select
ActiveWorkbook.Save

I would replace what you have there with the code below:

Code:
Dim RowCount As Long
RowCount = LogSheet.Cells(Rows.Count, 1).End(xlUp).Row
DHRSheet.Range("A1:A2").Copy LogSheet.Range("A" & RowCount + 1)
Range("A" & RowCount + 1) = DHRSheet.Name & "$" & Range("A" & RowCount + 1).Value
ActiveWorkbook.Save
Code:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

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