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.
 

Forum statistics

Threads
1,082,082
Messages
5,363,057
Members
400,711
Latest member
rscd20

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top