Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hi Everyone, I am wrapping up a project and need a little help with adding an if statement to my code, not sure the best way to do this so was hoping for some help.
Background
Using the find method the code loops thru a list of values in sheet "DataSource" and when a value is matched on Sheet "Manual Adjustments" it enter a $amount from the "Datasource" sheet.
What I need to add to this working code is an If Statement for the find method, that in the event a value is not found from the DataSource tab, the code needs to go to Sheets "Manual Adjustments", find the LastRow+1 in Column A and add the New Value.
Background
Using the find method the code loops thru a list of values in sheet "DataSource" and when a value is matched on Sheet "Manual Adjustments" it enter a $amount from the "Datasource" sheet.
What I need to add to this working code is an If Statement for the find method, that in the event a value is not found from the DataSource tab, the code needs to go to Sheets "Manual Adjustments", find the LastRow+1 in Column A and add the New Value.
Code:
Option Explicit
Sub ManlAdjustments()
Dim results As Range, CurrCell As Range, currValue As Range, rCell As Range
Dim ws1 As Worksheet, ws2 As Worksheet
Dim LastRow As Long, lrow As Long
Set ws2 = Sheets("DataSource") 'DataSource
Set ws1 = Sheets("Manual Adjustment") 'Sheet to insert new values if found
LastRow = ws2.Cells(Rows.Count, "A").End(xlUp).Row 'datasource Lastrow
lrow = ws1.Cells(Rows.Count, "A").End(xlUp).Row 'Adjustments Lastrow
Set results = ws2.Range("A11:A" & LastRow & "")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call Clear_Highlighting ' Clears sheet of any highlighted cells
For Each rCell In results
If rCell <> "" Then
Set CurrCell = rCell
Set currValue = CurrCell.Offset(0, 4)
With ws1 'This block searches for the value that is defined in the column A from the "Datasource" Tab
.Activate
.Cells.Find(What:=CurrCell.Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 7).Value = -currValue.Value ' "-" makes the values negative, remove to go back to original value
ActiveCell.EntireRow.Interior.ColorIndex = 6 ' Highlights entire row when it adds an entry
End With
'Add code to enter new value if not found
End If
Next rCell
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub