This is my first ever S.O. question, so apologies if I go overboard with Wall of Text/Lack of Relevant Info'.
I maintain Macro Excels built by a former employee for P&L by Market and Sales Rep. They pull in data from other packages which now vary in format due to different versions in use. There is also a French-language market. Everything works for English-only (barely) but now they want the French-language market included.
I'm pulling a value from a cell in multiple external files that are not fixed except by it's accompanying labeling text. Using Cells.Find
, I am searching for an English phrase and activating the adjacent cell to copy that value into the main Excel. This works.
Now they want the French language version included in this process, and I was attempting a parallel search for the English and French phrases to find which one it is and to take one OR the other, but I am getting successive and cyclic errors upon attempting the solutions found in StackOverflow.
What is wrong with this code?
Sub FindMonthlyValues(ByVal varFile As String, ByRef MonthlyProfitLine As String, ByRef MonthlyTonnageLine As String, ByRef MonthlyTripNumberRange As String)
Dim CurrentPnL, CurrentPath As String
Dim k, FirstRow, LastRow As Integer
Dim ProfitEn, ProfitFr As Range
'loop through excel files only
Application.ScreenUpdating = False
CurrentPnL = varFile
k = InStr(CurrentPnL, "[") 'find the end position of the default link
CurrentPath = Left(CurrentPnL, k - 1)
CurrentPnL = Replace(CurrentPnL, "[", "")
CurrentPnL = Replace(CurrentPnL, "]", "")
ChDir _
CurrentPath
Workbooks.Open FileName:= _
CurrentPnL
' Find Monthly Profit Line in Initial sheet
Sheets("Initial").Select
Range("A1:AK1").Select
Set ProfitFr = Cells.Find(What:="PROFIT MENSUEL:", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Value
If ProfitFr Is Nothing Then
Set ProfitEn = Cells.Find(What:="Monthly Profit:", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Value
End If
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(, 1).Select
MonthlyProfitLine = ActiveCell.Address
I've tried three different StackOverflow solutions but they lead to a circle of different errors as defined in the header.
I don't see an Option Explicit
in the code section I'm modifying.
I've tried setting a Dim for new variables to hold the phrase found.
I've tried Set [DimName] = Cells.Find(BLAH).Value
from the original (and functional) Cells.Find(BLAH).Activate
.
I've tried an If [DimName] Is Nothing Then
but it makes no difference to the errors, which happen at the first Cells.Find
.
I've tried changing the order of the English or French Cells.Find
, but it makes no difference to the errors, which happen at the first Cells.Find
.
I've tried changing the Dim from Range
to String
but only get:
"Object undefined."