VB Code not being consistent - Excel 2010 - Search Function

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. 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?


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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top