How to duplicate option explicit VBA code excel 2010

tayris

New Member
Joined
Jun 19, 2014
Messages
2
I have used the information from another post but this is a new type of enquiry.

quote_icon.png
Originally Posted by Ruddles
Create the "timestamp info" worksheet by hand, make the top row bold and freeze it. Paste the following code into the code module for your main worksheet - the one where B321 changes. Change the bit in red to match the name of your main worksheet.
Code:


Code:
[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT] [FONT=Courier New][SIZE=1]Private Sub Worksheet_Calculate()[/SIZE][/FONT] 

[FONT=Courier New][SIZE=1]Dim iLastRow As Long[/SIZE][/FONT][SIZE=1][FONT=Courier New]
Dim tsi As Worksheet[/FONT][/SIZE][SIZE=1][FONT=Courier New]
Dim mws As Worksheet[/FONT][/SIZE] 
[SIZE=1][FONT=Courier New]
Set tsi = ThisWorkbook.Sheets("timestamp info")
[/FONT][/SIZE][SIZE=1][FONT=Courier New]tsi.Range("A1") = "New Value"
[/FONT][/SIZE][SIZE=1][FONT=Courier New]tsi.Range("B1") = "Date/Time Changed"
[/FONT][/SIZE][SIZE=1][FONT=Courier New]iLastRow = tsi.Cells(tsi.Rows.Count, 1).End(xlUp).Row[/FONT][/SIZE] [SIZE=1][FONT=Courier New]Set

mws = ThisWorkbook.Sheets("[COLOR=red][B]Sheet1[/B][/COLOR]")[/FONT][/SIZE] [SIZE=1][FONT=Courier New]

If mws.Range("B321").Value <> tsi.Cells(iLastRow, 1).Value Then[/FONT][/SIZE][SIZE=1][FONT=Courier New] 
tsi.Cells(iLastRow + 1, 1) = mws.Range("B321").Value[/FONT][/SIZE][SIZE=1][FONT=Courier New] 
tsi.Cells(iLastRow + 1, 2) = Format(Now(), "dd/mm/yyyy hh:nn:ss")[/FONT][/SIZE][SIZE=1][FONT=Courier New]

End If[/FONT][/SIZE] [FONT=Courier New][SIZE=1]
End Sub[/SIZE][/FONT]

</PRE>



I have used the above and it works perfectly!
My issue is I am creating a coin book and would like to run the same code for more than one cell being updated (different denominations) and with a chosen description as well.
I've used the following modification for the singular cell to achieve this for 5 cent bags,

Code:
Option Explicit

Private Sub Worksheet_Calculate()

Dim iLastRow As Long
Dim tsi As Worksheet
Dim mws As Worksheet

Set tsi = ThisWorkbook.Sheets("Timestamp Info")
tsi.Range("A1") = "Description"
tsi.Range("B1") = "New Value"
tsi.Range("C1") = "Date/Time Changed"

iLastRow = tsi.Cells(tsi.Rows.Count, 1).End(xlUp).Row

Set mws = ThisWorkbook.Sheets("Sheet1")

If mws.Range("B1").Value <> tsi.Cells(iLastRow, 1).Value Then
tsi.Cells(iLastRow + 1, 1) = "changed total 5 cent bags"
tsi.Cells(iLastRow + 1, 3) = Format(Now(), "dd/mm/yyyy hh:nn:ss")
tsi.Cells(iLastRow + 1, 2) = mws.Range("B1").Value
End If

End Sub

This runs perfectly for what I wish to do however when I try to duplicate the code, only the second code runs (10 cent bags) even when the 1st codes cell is the one that has been altered.



Code:
Option Explicit

Private Sub Worksheet_Calculate()

Dim iLastRow As Long
Dim tsi As Worksheet
Dim mws As Worksheet

Set tsi = ThisWorkbook.Sheets("Timestamp Info")
tsi.Range("A1") = "Description"
tsi.Range("B1") = "New Value"
tsi.Range("C1") = "Date/Time Changed"

iLastRow = tsi.Cells(tsi.Rows.Count, 1).End(xlUp).Row

Set mws = ThisWorkbook.Sheets("Sheet1")

If mws.Range("B1").Value <> tsi.Cells(iLastRow, 1).Value Then
tsi.Cells(iLastRow + 1, 1) = "changed total 5 cent bags"
tsi.Cells(iLastRow + 1, 3) = Format(Now(), "dd/mm/yyyy hh:nn:ss")
tsi.Cells(iLastRow + 1, 2) = mws.Range("B1").Value
End If
If mws.Range("B2").Value <> tsi.Cells(iLastRow, 1).Value Then
tsi.Cells(iLastRow2 + 1, 1) = "changed total 10 cent bags"
tsi.Cells(iLastRow2 + 1, 3) = Format(Now(), "dd/mm/yyyy hh:nn:ss")
tsi.Cells(iLastRow2 + 1, 2) = mws.Range("B2").Value
End If

End Sub
Any help at all would be appreciated, happy to give more info if needed.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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