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.

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.
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Watch MrExcel Video

Forum statistics

Threads
1,099,337
Messages
5,468,019
Members
406,563
Latest member
kdorClintR

This Week's Hot Topics

Top