Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hi Guys,
Was hoping someone could look over my code and see if I am doing something weird to cause an issue with my search Function.
My code is looking on a data sheet and searching for text I have entered in a named range CYEAR & " " & Rperiod2 = "2018 Budget"
For some reason the search function is not being consistent, sometimes it will find the cell that has the "2018 Budget" but sometimes it returns the cell that has "2018 Actuals"? I am not sure why though? Does the code retain the prior search value without removing it?
Was hoping someone could look over my code and see if I am doing something weird to cause an issue with my search Function.
My code is looking on a data sheet and searching for text I have entered in a named range CYEAR & " " & Rperiod2 = "2018 Budget"
For some reason the search function is not being consistent, sometimes it will find the cell that has the "2018 Budget" but sometimes it returns the cell that has "2018 Actuals"? I am not sure why though? Does the code retain the prior search value without removing it?
Code:
Sub FindColumn()
Dim ws As Worksheet
Dim FindCol1 As Range, FindCol2 As Range, FindCol3 As Range, FindCol4 As Range
Dim Fd As String, fd2 As String, fd3 As String, fd4 As String
Set ws = Sheets("By-Account")
With ws
Set FindCol1 = .Range("9:9").Find(What:=Range("cyear").Value & " " & Range("RPeriod3").Value, LookIn:=xlValues)
Set FindCol2 = .Range("9:9").Find(What:=Range("cyear").Value & " " & Range("RPeriod1").Value, LookIn:=xlValues)
Set FindCol3 = .Range("9:9").Find(What:=Range("pyear").Value & " Actuals", LookIn:=xlValues)
Set FindCol4 = .Range("9:9").Find(What:=Range("cyear").Value & " " & Range("RPeriod2").Value, LookIn:=xlValues) ' This line is having issues
End With
With Sheets("WBEI Consolidated")
Fd = Chr(FindCol1.Column + 64)
fd2 = Chr(FindCol2.Column + 64)
fd3 = Chr(FindCol3.Column + 64)
fd4 = Chr(FindCol3.Column + 64)
'Actuals Validation
.Range("D114").Formula = "=ROUND(D112,1)-ROUND(INDEX('By-Account'!" & Fd & ":" & Fd & ",MATCH(3E+100,'By-Account'!" & Fd & ":" & Fd & ",1)),-2)/1000"
'Forecast Validation
.Range("M114").Formula = "=ROUND(M112,1)-ROUND(INDEX('By-Account'!" & fd2 & ":" & fd2 & ",MATCH(3E+100,'By-Account'!" & fd2 & ":" & fd2 & ",1)),-2)/1000"
'Prior Year Actuals Validation
.Range("N114").Formula = "=ROUND(N112,1)-ROUND(INDEX('By-Account'!" & fd3 & ":" & fd3 & ",MATCH(3E+100,'By-Account'!" & fd3 & ":" & fd3 & ",1)),-2)/1000"
'Budget Validation
.Range("L114").Formula = "=ROUND(L112,1)-ROUND(INDEX('By-Account'!" & fd4 & ":" & fd4 & ",MATCH(3E+100,'By-Account'!" & fd4 & ":" & fd4 & ",1)),-2)/1000"
'Budget Validation
.Range("F114").Formula = "=ROUND(F112,1)-ROUND(INDEX('By-Account'!" & fd4 & ":" & fd4 & ",MATCH(3E+100,'By-Account'!" & fd4 & ":" & fd4 & ",1)),-2)/1000"
End With
End Sub