I have one workbook, with two sheets (sheet 1 & sheet 2). Sheet 1 has columns A:E (TSR = Target Salary Range)
Job Code
job title
TSR min
TSR mid
TSR max
10001
job 1
55k
60k
65K
10003
job 3
65k
70k
75K
10005
job 3
75k
80k
85K
on my Sheet 2, I have updated figures for only two of the jobs:
Job Code
job title
TSR min
TSR mid
TSR max
10001
job 1
65k
70k
75K
10005
job 3
85k
90k
95K
based on Sheet2, I want to only update the numbers in columns C:E of jobs 1 and 3, and do not change job 2’s salary bands.
Here is code I’ve tried but I’m not getting a subscript error on this line: Set varSheetB = wbkA.Sheets(2)
, and I in general don’t think I’m quite there (why Im reaching out here).
Option Explicit
Sub test()
Dim wbkA As Workbook
Dim varSheetA As Variant, varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long 'I'll want to check for differing values between columns C:E
Set wbkA = ThisWorkbook
Set varSheetA = wbkA.Sheets(1)
Set varSheetB = wbkA.Sheets(2)
strRangeToCheck = "A2:E3000"
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet2").Range(strRangeToCheck)
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) <> varSheetB(iRow, iCol) Then '
varSheetA(iRow, iCol) = varSheetB(iRow, iCol)
Else
'do nothing
End If
Next iCol
Next iRow
End Sub
I’ll link screenshots if necessary, but just imagine both takes starting in A1 (with the headers)
The sheet shows the OG stuff your code updates, but then it has iterations of slightly higher salaries that’ll need to also be updated too.
I made a few changes to your code to clean it up and make it more efficient. First, since Sheet2 contains all of the changes, we will be looping over that sheet and searching for the values in sheet 1. This is more efficient because it doesn’t waste operations looping through the cells in sheet1 that don’t have any changes.
Additionally, I’ve made the Ranges dynamic so you’re not constrained to range “A2:E3000” in all cases. If I understand correctly, sheet2 will be a subset of sheet1, so there are cases where it will have less rows. No reason to loop over blank rows.
Finally, I use the Match
function to search for matches. Keep in mind that the function returns the position relative to the range. So, if you are searching for a match in Range(“A5:A10”), and the match is found in cell A5
, the function will return 1 (not 5). That’s why I offset the fndRow
variable.
Let me know if this works for you or if you have additional questions.
Sub Test()
Dim wbk As Workbook
Dim wsA As Worksheet
Dim wsB As Worksheet
Dim rngA As Range
Dim rngB As Range
Dim rIterator As Range
Dim fndRow As Long
Set wbk = ThisWorkbook
Set wsA = wbk.Sheets("Sheet1")
Set wsB = wbk.Sheets("Sheet2")
'CurrentRegion should work to bring in the entire
'necessary range so long as it is contiguous data
Set rngA = wsA.Range(wsA.Range("A2"), wsA.Range("A2").End(xlDown))
Set rngB = wsB.Range(wsB.Range("A2"), wsB.Range("A2").End(xlDown))
For Each rIterator In rngB
On Error Resume Next
fndRow = Application.Match(rIterator.Value, rngA, 0) + _
rngA.Range("A1").Row - 1
If Err.Number <> 0 Then
'What happens if it isn't found?
Else
wsA.Cells(fndRow, 3).Value = rIterator.Offset(, 2).Value
wsA.Cells(fndRow, 4).Value = rIterator.Offset(, 3).Value
wsA.Cells(fndRow, 5).Value = rIterator.Offset(, 4).Value
wsA.Cells(fndRow + 1, 3).Value = rIterator.Offset(, 2).Value * 1.1
wsA.Cells(fndRow + 1, 4).Value = rIterator.Offset(, 3).Value * 1.1
wsA.Cells(fndRow + 1, 5).Value = rIterator.Offset(, 4).Value * 1.1
wsA.Cells(fndRow + 2, 3).Value = rIterator.Offset(, 2).Value * 1.2
wsA.Cells(fndRow + 2, 4).Value = rIterator.Offset(, 3).Value * 1.2
wsA.Cells(fndRow + 2, 5).Value = rIterator.Offset(, 4).Value * 1.2
'.... Repeat for all rows
End If
Err.Clear
Next rIterator
End Sub
Tags: date, excel, excelarrays