smide
Board Regular
- Joined
- Dec 20, 2015
- Messages
- 164
- Office Version
- 2016
- Platform
- Windows
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!?
Any idea how to solve this?
Here is the code:
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!?
Any idea how to solve this?
Here is the code:
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