I have an excel file with many tabs, each with many lines of data, some of which need to be manually changed and I am trying to automate the process a bit.
I have created a tab which shows me the lines that need to be changed and I can input the new value.
This is what I have so far, but it's not working quite right. The correct lines/cells are being changed, but they are all being changed to what is in E3, rather than E3, E4, E5 and so on.
Private Sub CommandButton1_Click()
Dim r As Long
Dim g As Long
Dim lrow As Long
Dim grow As Long
Dim ysheet As String
Dim ws As Worksheet
ysheet = Sheets("Group Code Correction").Range("B3").Value 'define ysheet by cell B3
Set ws = Sheets(ysheet) 'Define ws by ysheet
grow = Sheets("Group Code Correction").Range("D" & Rows.Count).End(xlUp).row 'find last row in sheet group code correction
lrow = ws.Range("D" & Rows.Count).End(xlUp).row 'Find last row in sheet ws
For g = 3 To grow 'loop from row 3 to last row
If Sheets("Group Code Correction").Range("E3") = "" Then 'if E3 is blank do nothing
Else
For r = 2 To lrow 'Loop from row 2 to last row
If ws.Range("D" & r) = Sheets("Group Code Correction").Range("D" & g).Value And ws.Range("Z" & r) = Sheets("Group Code Correction").Range("C" & g).Value Then 'if Dr matches Dg and Zr matches Cg
ws.Range("Z" & r) = Sheets("Group Code Correction").Range("E" & g).Value 'replace Zr with Eg
End If
Next r
End If
Next g
End Sub
Anyone able to point me on the right direction?
You need to fix this line:
If Sheets("Group Code Correction").Range("E3") = "" Then
Try this out:
Private Sub CommandButton1_Click()
Dim r As Long, g As Long, lrow As Long, grow As Long, ysheet As String
Dim ws As Worksheet, wsGCC As Worksheet, vD, vC
Set wsGCC = ThisWorkbook.Worksheets("Group Code Correction") '<<use explicit workbook scope
ysheet = wsGCC.Range("B3").Value 'define ysheet by cell B3
Set ws = ThisWorkbook.Worksheets(ysheet) 'Define ws by ysheet
grow = wsGCC.Range("D" & Rows.count).End(xlUp).Row 'find last row in sheet group code correction
lrow = ws.Range("D" & Rows.count).End(xlUp).Row 'Find last row in sheet ws
For g = 3 To grow 'loop from row 3 to last row
If Len(wsGCC.Cells(g, "E").Value) > 0 Then '<< if E cell has a value
vD = wsGCC.Cells(g, "D").Value '<< only need to read these once...
vC = wsGCC.Cells(g, "C").Value
For r = 2 To lrow 'Loop from row 2 to last row
If ws.Cells(r, "D") = vD And ws.Cells(r, "Z") = vC Then 'if Dr matches Dg and Zr matches Cg
ws.Cells(r, "Z") = wsGCC.Cells(g, "E").Value 'replace Zr with Eg
'Exit For '<< uncomment if you only expect one update in the target sheet
End If
Next r
End If
Next g
End Sub