Hello.
I have a problem with code which runs periodically (every 30 seconds).
Basically, the code compares data from two Sheets (at the same Workbook) and then paste data (under certain conditions) from Sheet4 to Sheet1.
However, when I open some other Workbook (window on that "other" Workbook is active) and I start with my new project, the code from previous Workbook then paste results into that new Workbook also!?
So, this is what happened and what I actually want to do:
- I open Workbook 1 with the macro in it and it runs perfectly (great!)
- I open WB2 (Workbook1 is still open, macro still runs) and when WB2 is active then macro from WB1 start to paste data into WB2 (problem)
- I want macro from WB1 to keep working only in WB1 no matter the active workbook
Here is the code:
Sub Salary()
Dim Rng As Range, cell As Range, lr As Long, ws As Worksheet, sh As Worksheet, product_name As String
Set ws = ThisWorkbook.Sheets("Sheet1")
Set sh = ThisWorkbook.Sheets("Sheet4")
For Each Rng In sh.Range("B3:B" & sh.Range("B" & Rows.Count).End(xlUp).Row)
product_name = Rng.Value
For Each cell In ws.Range("A3:X3")
If InStr(1, cell, Rng.Value) > 0 Then
If Cells(ws.Cells(Rows.Count, cell.Column).End(xlUp).Row, cell.Column).Value <> Rng.Offset(, 2).Value Then Cells(ws.Cells(Rows.Count, cell.Column).End(xlUp).Row + 1, cell.Column).Value = Rng.Offset(, 2).Value
GoTo nextrng
End If
Next cell
nextrng:
Next Rng
Application.OnTime Now + TimeValue("00:00:30"), "Salary"
End Sub
Bookmarks