Visual Basic Loop - excel

I want the loop to query the ticker in the cell right below it and loop until it has pulled that data for all of tickers in the column.
Summary:
I am attempting to pull data from for ticker symbols in Column A
This is the code I am using.
Sub URL_Static_Query()
''Pull Data from Profile
With Sheet2.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/pm?s=" & Sheet1.Range("A2").Value & "+Performance", _
Destination:=Sheet2.Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
''Pull Data from Performance
With Sheet3.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/pr?s=" & Sheet1.Range("A2").Value & "+Profile", _
Destination:=Sheet3.Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
'Grab and Paste 3-month
Sheets("Sheet2").Select
Range("A1").Select
Cells.Find(What:="3-month", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
'Grab and Paste 1-Year
Sheets("Sheet2").Select
Range("A1").Select
Cells.Find(What:="1-Year", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 2).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Sheet3").Select
Range("A1").Select
Cells.Find(What:="Prospectus Net Expense Ratio:", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Sheet1").Select
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheet2.Cells.Clear
Sheet3.Cells.Clear
End Sub

You can wrap this code in a loop that goes down each cell in the column, one by one.
For example, if you're using column A,
Dim row_counter As Long, last_row As Long
row_counter = 1
'last_row = whatever your last row is
Do While row_counter < last_row
'... put looping code here
row_counter = row_counter + 1
Loop

Related

Increase Decrease Values from One Sheet to Another Sheet VBA

I have one sheet named Sheet3 and another are Sheet4. Sheet3: Column A Header is Product type, and Column B is their Quantity, Sheet4 has same column Header. Product Type and Quantity. But when I Run Below Macro in Sheet3, Sometime they add values correctly to
Sheet4 and sometime they doesn't work properly.
Sub Increase_Value()
Sheets("Sheet3").Select
Cells.Find(What:=Sheet4.Range("A2").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet4").Select
Range("A2").Select
ActiveCell.Offset(0, 1).Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(0, 1).Activate
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("E10").Select
Sheets("Sheet3").Select
Cells.Find(What:=Sheet4.Range("A3").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet4").Select
Range("A3").Select
ActiveCell.Offset(0, 1).Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(0, 1).Activate
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("E10").Select
Sheets("Sheet3").Select
Cells.Find(What:=Sheet4.Range("A4").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet4").Select
Range("A4").Select
ActiveCell.Offset(0, 1).Activate
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(0, 1).Activate
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("E10").Select
Sheets("Sheet4").Select
Range("A2:B4").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A2").Select
ActiveWorkbook.Save
End Sub
For Example: If Sheet3 Range A3 = **Coca Cola**, And B3 = **20**, And When I rum the macro Increase_Value(), The VBA should find the value Coca Cola in Sheet4 Column A and if value found in row 10 (A10) then add value 20 from sheet3 Range B3 to sheet4 Range B10.
If B10 is 47 then after running the macro it should be 67.
Looks like you are trying to do something that a vlookup could solve easily? You can do vlookups in VBA as well
Below is the exact formula -
'''Application.WorksheetFunction.vlookup(lookup_value, table_array, col_index_num, range_lookup)'''
You can find more information on this here:
https://excelmacromastery.com/vba-vlookup/
Thanks,

VBA Last Row errors

I'm using this code, but columns with last row (A,B,K,L) fill in with 0 beyond the defined last row. Additionally, my transaction and type occasionally stop working, but if anyone sees what i'm doing wrong i'd love to learn so i don't have this issue again.
Sub test()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 10).End(xlUp).Row
'delete blank columns
Range("W:W,U:U,S:S,Q:Q,O:O,M:M,K:K,I:I,G:G,E:E,C:C,A:A").Select
Range("A1").Activate
Selection.Delete Shift:=xlToLeft
'filter for blanks
Range("A:L").CurrentRegion.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$L$1").AutoFilter Field:=10, Criteria1:="="
ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
Selection.AutoFilter
'Trans
Columns("A:A").Select
Selection.NumberFormat = "General"
Range("A2:A" & lastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
Columns("A:A").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Type
Columns("B:B").Select
Selection.NumberFormat = "General"
Range("B2:B" & lastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
Columns("B:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Debit
Range("K2:K" & lastRow).Select
'Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'credit
Range("L2:L" & lastRow).Select
'Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
So sorry for my English, just get you wrong 😆
Try to use cycle instead of replace.
Use Dim EmptCell as range once.
Dim EmptCell as range
For Each EmptCell in Range("youRange").Cells
If EmptCell.value = "" Then EmptCell.value = 0
Next EmptCell
And try to not to use selection. Work directly with a ranges, or use variables like you did with LastRow.
Small example below.
' Trans
Columns("A:A").NumberFormat = "General"
Range("A2:A" & lastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
Columns("A:A").value = Columns("A:A").value
And I'm definitely recommend to change LastRow algorithm to this bulletproof one.
© https://stackoverflow.com/a/11169920/12882709
With Sheets("Sheet1")
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastrow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lastrow = 1
End If

Selection.Replace in VBA macro

I need to change the code so that
LMX220MA (KIT) becomes X220MA,
LMX220MA becomes X220MA,
LMX220 (KIT) becomes X220MB,
LMX220 becomes X220MB.
Tried removing LMX22 Selection.Replace line and then adding:
Range("H2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],6)"
Selection.Copy
Range("G1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(, 1).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
Selection.Replace What:="LMX220", Replacement:="X220MB", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("H2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],8)"
Selection.Copy
Range("G1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(, 1).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks
Selection.Replace What:="LMX220MA", Replacement:="X220MA",
LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
and so on.
Original code:
' Insert Model Number_Carrier column
Sheets("Data_Upload").Select
Columns("H:H").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("H1").Select
ActiveCell.FormulaR1C1 = "Model Number_Carrier"
' Fill Model Number_Carrier field
Range("H2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],5)"
Selection.Copy
Range("G1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(, 1).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("H:H").Select
Selection.Replace What:="LMX21", Replacement:="X210MA", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="MW41M", Replacement:="_", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Q710M", Replacement:="Q710MS", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="LMQ61", Replacement:="Q610MA", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="LMQ71", Replacement:="_", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="X410M", Replacement:="X410MK", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="LMX22", Replacement:="X220MB", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
It will fill ModelNumber_Carrier cells with what is in the cell in Model column (LMX220 becomes LMX220) and "ModelNumber_Carrier" column becomes "Model" even though ModelNumber_Carrier column coding was left alone.
Returning compile error: end sub error when I change it to this:
Sub MPCSWeeklyReturnReason()
'
' MPCS_Return_Reason Macro
'
' Prevents screen refreshing.
Application.ScreenUpdating = False
' Check if procedure has already run
Dim rCell As String
rCell = ActiveSheet.Range("H1").Text
If InStr(1, rCell, "Model Number_Carrier") Then
Application.ScreenUpdating = True
MsgBox "Macro already run."
Exit Sub
Else
' Combine all worksheets to one for upload
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Data_Upload"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
' Insert Model Number_Carrier column
Sheets("Data_Upload").Select
Columns("H:H").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("H1").Select
ActiveCell.FormulaR1C1 = "Model Number_Carrier"
' Fill Model Number_Carrier field
Sub FindReplaceAll()
' This will find and replace text in all sheets
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
Dim fnd1 As Variant
Dim rplc1 As Variant
Dim fnd2 As Variant
Dim rplc2 As Variant
Dim fnd3 As Variant
Dim rplc3 As Variant
'Set the criteria to change here
fnd = "LMX220MA (KIT)"
rplc = "X220MA"
fnd1 = "LMX220MA"
rplc1 = "X220MA"
fnd2 = "LMX220 (KIT)"
rplc2 = "X220MB"
fnd3 = "LMX220"
rplc3 = "X220MB"
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
sht.Cells.Replace what:=fnd1, Replacement:=rplc1, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
sht.Cells.Replace what:=fnd2, Replacement:=rplc2, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
sht.Cells.Replace what:=fnd3, Replacement:=rplc3, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
End Sub
' ESN Concantenate Fix
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=TEXT(,RC[-11])"
Selection.Copy
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 16).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("R2").Select
ActiveCell.FormulaR1C1 = "=IF(ISERROR(RC[-1]), RC[-12], RC[-1])"
Selection.Copy
Range("Q2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range("F2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("Q:R").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
' TRIM Reason and SUBReason spaces
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-4])"
Selection.Copy
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 16).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("Q:Q").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
' Enables screen refreshing.
Application.ScreenUpdating = True
' Save the Workbook
ActiveWorkbook.Save
End If
End Sub
Here try this. It will go through all of the sheets in your workbook and find and replace all cases with the text you specified. I was unsure if you wanted to have the "(KIT)" included so I left it in, but feel free to adjust as necessary.
Sub FindReplaceAll()
' This will find and replace text in all sheets
Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
Dim fnd1 As Variant
Dim rplc1 As Variant
Dim fnd2 As Variant
Dim rplc2 As Variant
Dim fnd3 As Variant
Dim rplc3 As Variant
'Set the criteria to change here
fnd = "LMX220MA (KIT)"
rplc = "X220MA"
fnd1 = "LMX220MA"
rplc1 = "X220MA"
fnd2 = "LMX220 (KIT)"
rplc2 = "X220MB"
fnd3 = "LMX220"
rplc3 = "X220MB"
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
sht.Cells.Replace what:=fnd1, Replacement:=rplc1, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
sht.Cells.Replace what:=fnd2, Replacement:=rplc2, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
sht.Cells.Replace what:=fnd3, Replacement:=rplc3, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
End Sub
Took another look at this and you can also do it this way by using arrays. Similar to my other answer with this one if I left in the "(KIT)" that shouldn't have been there or anything just adjust as necessary but the syntax is there.
Sub FindReplaceAll()
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
'Set the criteria to change here
fndList = Array("LMX220MA (KIT)", "LMX220MA", "LMX220 (KIT)", "LMX220")
rplcList = Array("X220MA", "X220MA", "X220MB", "X220MB")
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
End Sub

Skip all Errors While Iteration in Excel VBA

I am trying to Data from one workbook and paste it in other while doing so there are some values not searchable, an error is shown. On handling the error it skips the iteration but when the second-time error occurs it is not skipping the iteration but giving the error message.
For i = 1 To lrow
On Error GoTo InvalidValue
mWkb.Activate
Sheets("Sheet2").Select
s_value = ActiveCell.Offset(i, 0).Value
Sheets("LocDB").Select
Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Find(What:=s_value, After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
wkb.Activate
Range("G9").Select
ActiveCell.Offset(i, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
InvalidValue:
Next i

Find function not working when searching for a word through macro

I am unable to search a word which I know is there in an excel sheet, when searching using macro. I have checked all the options but could not find why is this happening. var2 is 1082591.html and 1082592.html and so on. When I am finding Total in the excel it is not finding it. Thanks
Sub DataPull()
Dim Website
Application.DisplayAlerts = False
var1 = 2
Do Until Range("A" & var1) = ""
var2 = Range("A" & var1).Value
Hostteam = Range("C" & var1).Value
Hosts = Hostteam & " innings"
Visitorteam = Range("D" & var1).Value
Visitors = Visitorteam & " innings"
Website = "http://www.espncricinfo.com/indian-premier-league-2017/engine/match/" & var2
Set myIE = CreateObject("InternetExplorer.Application")
myIE.Navigate Website
myIE.Visible = True
Application.Wait Now + TimeSerial(0, 0, 5)
SendKeys "^a"
Application.Wait Now + TimeSerial(0, 0, 1)
SendKeys "^c"
Application.Wait Now + TimeSerial(0, 0, 1)
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=var2
ActiveSheet.Paste
Application.Wait Now + TimeSerial(0, 0, 10)
Cells.Find(What:=Hosts, After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Total", After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 2).Copy
ThisWorkbook.Activate
Range("J" & var1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks(var2).Activate
Cells.Find(What:=Visitors, After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Total", After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 2).Copy
ThisWorkbook.Activate
Range("L" & var1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
myIE.Quit
Set myIE = Nothing
Err.Clear
Workbooks(var2).Activate
ActiveWorkbook.Close
ThisWorkbook.Activate
Range("O" & var1).Value = "Done"
var1 = var1 + 1
Loop
End Sub
Write these 2 lines before the search for "Totals" to see what is your active cell and in which sheet:
debug.print activecell.address
debug.print activecell.parent.name
Cells.Find(What:="Total", After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 2).Copy
That could be a reason for not finding it. If this is the case, consider not searching for After:ActiveCell, but use a variable to set it.

Resources