Loop finishing with error - excel

I need to look for each row that has the phrase " Total" in it, insert rows above and below it, format other cells in and around that same row, remove the phrase " Total" from the cell, and repeat the process for all other rows in the report.
The macro I've developed, once it's found and replaced all of the instances of " Total", I get the Run-time error '91': Object variable or With block variable not set.
I would like to finish the loop without ending up with the error as this has to be executed on multiple sheets. Here is the meat of the code:
'EmployerSummariesAddedForRegionsOnTabs Macro
Dim FoundCell As Range, LastCell As Range
Dim FirstAddr As String
With Range("D3:D3000")
Range("D3").Select
Set LastCell = .Cells(.Cells.Count)
End With
Set FoundCell = Range("D1:D3000").Find(What:=" Total", After:=LastCell, LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
Set FoundCell = Range("D1:D3000").FindNext(After:=FoundCell)
COLUMNS("D:D").Select
Selection.Find(What:=" Total", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Total", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
ActiveCell.Rows("1:1").EntireRow.Select
ActiveCell.Activate
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown
ActiveCell.Offset(3, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown
.
.
.
If FoundCell.Address = FirstAddr Then
Exit Do
End If
Loop
Range("A1").Select
End Sub

There are two steps to creating an object variable. First you must declare the object variable.
this error occurs because you didn't set a object correctly it is quite the same as null refrence exception if you have programming experience.
I ran your code and it seems that it doesn't do anything at all! Not even an error! so I assume that your error must because of somewhere else in your code.
any way this lines replace any total in active worksheet with nothing:
Cells.Replace What:=" Total", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Related

swap 2 values in excel selection

In a variable selection of cells in single column, I'm needing to swap "Buy" with "Sell", and vice-versa.
I tried something like this...but when first Selection.Replace changes "Sell" to "Buy", then 2nd one just changes "Buy" back to "Sell" -
Data sample
I havent found that I can nest a Selection.Replace..would need to loop down thru each cell somehow?
....
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("C4").Select
Selection.PasteSpecial
Selection.Replace What:="Bought", Replacement:="Sold", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="Sold", Replacement:="Bought", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
To achieve what you want, you can replace three times instead of just two times.
The solution shows how to do it, while avoiding the use of Copy, Paste and Select, to make sure we don't interfere with the user selection and the clipboard.
Sub Doit()
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng As Range
Dim Lst As Variant
' Get the range in column B from row 4 and down.
Set Rng1 = Range("B4")
Set Rng2 = Rng1.End(xlDown)
Set Rng = Range(Rng1, Rng2)
' Copy the range to column C
Lst = Rng
Range("C4").Resize(UBound(Lst, 1), UBound(Lst, 2)) = Lst
' Replace Bought with Sold and Sold with Bought
Rng.Replace What:="Bought", Replacement:="#B#o#u#g#h#t", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Rng.Replace What:="Sold", Replacement:="Bought", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Rng.Replace What:="#B#o#u#g#h#t", Replacement:="Sold", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
The solution assumes that your data does not contain the string "#B#o#u#g#h#t". You can use whatever string you want instead of "#B#o#u#g#h#t", as long as the string does not appear in your data.
As with your example, the solution does not test, if there is any valid data in Column B from row 4 and down.

ActiveCell Based Selection

Column A to H has data with some blanks in between. I want to find "ABC" in column A and then select 2 rows above - this will be my ActiveCell.
I want to delete rows in between ActiveCell to Row2 (Active Cell is Dynamic)
Sub format()
Cells.Find(What:="abc", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:= xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate
ActiveCell.Offset(-2, 0).Select
Range(Selection, ActiveCell, A2).Select
End Sub
The code will do the job for you:
Sub format()
Dim rng As Range
Set rng = Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
rng.Offset(-2, 0).Select
Range(Cells(Selection.Row, 1), Cells(2, 1)).Select
'Selection.EntireRow.Delete
End Sub
Currently I have commented out the last line which will delete the Rows you want. uncomment it, but first be sure that's what you want to delete.
For Range please try:
(ActiveCell, "A2").Select

Excel-VBA Prompt user to input what column is to be searched?

first post :)
I have the following code below, which selects all within a set column and clears the two text phrases, from row 12 down.
What i want is to have the user input the column instead? possibly via InputBox?
Sub ClearColumn()
Dim lastCell As Long
Dim myRange As Range
' Find lastCell in column Z
lastCell = Cells(Rows.Count, "Z").End(xlUp).Row
' Set range to look at
Set myRange = Range("Z12:Z" & lastCell)
' Replace All Pass
myRange.Replace What:="Go", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Replace All Fail
myRange.Replace What:="Stop", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Welcome to Stack Exchange. You seem to have answered your own question. Below code is untested, but should take a small amount of time to implement.
Sub ClearColumn()
Dim lastCell As Long
Dim chooseColumn As Variant
Dim myRange As Range
chooseColumn = InputBox("Which Column do you want to alter?")
' Find lastCell in column Z
lastCell = Cells(Rows.Count, chooseColumn ).End(xlUp).Row
' Set range to look at
Set myRange = Range(chooseColumn &"12:"&chooseColumn & lastCell)
' Replace All Pass
myRange.Replace What:="Go", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Replace All Fail
myRange.Replace What:="Stop", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Let us know how you go :)

Find method raise error 91 in excel Macro

I use a find method in the Excel macro for searching VLOOKUP in a cell, my goal is I need to know which formula that does not contain VLOOKUP, my method was running well, until in a cell there was
no VLOOKUP and macro kept debugging with the Run time error '91'
My question is how should I write the macro correctly so I wont get the debug until the activecell
contains *, below is my macro:
Sub findvlookup()
Do While Not ActiveCell.Value = "*"
If Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate Then
ActiveCell.Offset(1, 0).Select
End If
Loop
End Sub
Thanks for the help
You don't need a loop to find the first occurrence of your 'What:=" string
Dim fndVL as Range
fndVL = Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not fndVL is Nothing Then
MsgBox "Found it"
Else
NsgBox "Not found"
End If
If you want to find subsequent instances then have a look at FindNext and possibly use this within a loop.
Sub findvlookup()
Dim rngFind As Range
Do While Not ActiveCell.Value = "*"
Set rngFind = Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rngFind Is Nothing Then
ActiveCell.Offset(1, 0).Select
Else
Exit Sub
End If
Loop
End Sub
You are using Find method that technical details are Here.
You can find (in example) that:
Common Err.Numbers (9, 91) : the specified text wasn't in the target workbook.
And if you have a cell with an error value you will have another type of errors: like #Value

Excel - Copy adjacent data value to another sheet based on certain text, till end of sheet

So I have two excel documents.
One to take data from (RESULT.xlsm).
Another to insert data into (Summary.xls).
What I want is the adjacent cell values next to the hightlighted names to get inserted into Summary.xls under the respective columns. So I tried recording a macro but what happens is only the first record gets inserted.
Since only two links are allowed for me, i put it all in one picture:
http://i50.tinypic.com/9veihl.png
Note: There are multiple records in RESULT.xlsm and the screenshot shows just one.
I would like help on how I can extract data from all the set of records and insert in Summary.xlsx
Here's the recorded macro code:
Sub Summ()
Workbooks.Open Filename:="Summary.xlsx"
Windows.Arrange ArrangeStyle:=xlVertical
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Air System Name", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B10").Select
Selection.Copy
Windows("Summary.xlsx").Activate
Range("A5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Floor Area", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B14").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("B5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Total coil load", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B27").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("C5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Sensible coil load", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("B28").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("D5").Select
ActiveSheet.Paste
Windows("RESULT.xlsm").Activate
Cells.Find(What:="Max block L/s", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B30").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Summary.xlsx").Activate
Range("E5").Select
ActiveSheet.Paste
Range("A6").Select
End Sub
I've also attached the excel files at mediafire:
Excel files
Please do help.
Thanks alot:)
So I looked up at alot of resources and tried to follow what #Tim Williams told me to and stumbled across this page (the last part): https://sites.google.com/a/madrocketscientist.com/jerrybeaucaires-excelassistant/text-functions/column-sets-to-rows
They had a solution almost close to my problem, so I made a few modifications and I'm done:D
Note: This is within the same document, different sheets.
The code of it:
Dim LR As Long, NR As Long, Rw As Long
Dim wsData As Worksheet, wsOUT As Worksheet
Dim HdrCol As Range, Hdr As String, strRESET As String
Set wsData = Sheets("Sheet1") 'source data
Set wsOUT = Sheets("Sheet2") 'output sheet
strRESET = "    Air System Name " 'this value will cause the record row to increment
LR = wsData.Range("A" & Rows.Count).End(xlUp).Row
'end of incoming data
Set HdrCol = wsOUT.Range("1:1").Find(strRESET, _
LookIn:=xlValues, LookAt:=xlWhole) 'find the reset category column
If HdrCol Is Nothing Then
MsgBox "The key string '" & strRESET & _
"' could not be found on the output sheet."
Exit Sub
End If
NR = wsOUT.Cells(Rows.Count, HdrCol.Column) _
.End(xlUp).Row 'current output end of data
Set HdrCol = Nothing
On Error Resume Next
For Rw = 1 To LR
Hdr = wsData.Range("A" & Rw).Value
If (Hdr = "    Air System Name ") Then
NR = NR + 1
End If
If Hdr <> "" Then
Set HdrCol = wsOUT.Range("1:1").Find(Hdr, _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not HdrCol Is Nothing Then
wsOUT.Cells(NR, HdrCol.Column).Value _
= wsData.Range("B" & Rw).Value
Set HdrCol = Nothing
End If
End If
Next Rw
The only little problem is the space. In my excel document, my report has trailing and leading spaces, and this doesn't match with my sheet2 columns headers, I kind of temporarily fixed it, since I looked around and couldn't find a way to automatically trim all the space from the whole column.
So that's it:)

Resources