Excel VBA run-time error 1004 which I can't identify - excel

This is my code:
Sub VerticalLoop()
Dim i As Integer
Range("VerticalLoop").Activate
i = 1
Do While i <= Range("VerticalLoop").Rows.Count
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop
End Sub
For some reason I can't run this. When I press F8, the error pops up when I reach the line "i=1", so I don't know exactly what I did wrong.

This code will fail if the Sheet containing the Defined Name has not been activated first!

The error is in the line
Range("VerticalLoop").Activate
The error is probably a typo in the name of the range. Are you sure it isn't supposed to be "Vertical_Loop" (with the underscore)?
On the other hand, Gary's Student is correct that a '1004' error could be triggered by trying to activate a range in an inactive sheet. The error message itself (and not just the error number) can tell you more about the exact source of the error. Alternatively, you could break that line into two lines like thus:
Dim VLoop As Range
Set VLoop = Range("VericalLoop")
VLoop.Activate
If the Set VLoop line throws the error then the problem is with your name. If the second line throws the error then then problem is with what is active.
As an added benefit -- once you debug this error you now have a useful range variable allowing you to use expressions like VLoop.Rows.Count

Related

VBA Run-time error '424 on simple IF statement

I have the following short VBA code, what I want is for it to check a cell, B5 for whether it is a Y or a N, and if Y - go to one macro, and if N - go to another macro.
Sub LoadSetup()
Dim Master As Workbook
Set SingleLoad = Setup.Range("B5")
If SingleLoad = "Y" Then
Call LatestLoad
Else
Call TimeSeriesLoad
End If
End Sub
I am getting a Run-rime error '424' Object Required on this line. Any ideas please what could be causing this?
Set SingleLoad = Setup.Range("B5")

Intermittent "Run-time error '1004' PasteSpecial method of Range class failed

I have a macro that intermittently throws a run-time error when doing a PasteSpecial. Most times it runs through to completion without failing, but sometimes it doesn't. When it throws the error, if I click on "Debug" and then just let it continue, it runs through without a problem. This sounds like a timing thing, but I don't understand what's causing it. Here is a snippet of the code:
Dim SourceDataWB As Workbook
Dim RawDataWS As Worksheet
Dim LastDataRow As Long
Dim SrcRange As Range
<Lots of other code in here...>
SourceDataWB.Activate
Set SrcRange = Range("A1:A" & LastDataRow)
SrcRange.Copy
RawDataWS.Range("A:A").PasteSpecial xlPasteValues
The RawDataWS worksheet is in a different workbook than the SourceDataWB. The error occurs on the PasteSpecial line. And if I just press "Play" at that point, it continues without error. Any ideas?
For anyone else experiencing a random/intermittent runtime error in VBA, one key may be to take control of the error handling. Instead of having your code go to this onscreen error, have VBA do something else.
One idea is -- when the error occurs -- have VB sleep for a moment and then retry the offending action. In many cases, that may be all you need to resolve the issue.
Using the above example, you might code:
' Tell VB not to handle errors with onscreen/debug behavior (just for now)
On Error Resume Next
' This is the example line that intermittently throws the runtime error
RawDataWS.Range("A:A").PasteSpecial xlPasteValues
' Check if there was an error and -- if so -- do something about it
If Err Then
' Pause
Sleep (1000) ' Pause in milliseconds
' retry offending line...
RawDataWS.Range("A:A").PasteSpecial xlPasteValues
Err.Clear ' Clear Err object fields
End If
On Error GoTo 0 ' set VB error handling back to normal
... of course this code assumes that it runs OK the second time. If failure that time is unacceptable, you may need to add a condition for that...

Error 91 each time I run an Application.Caller Macro

I am getting an error when I use Application.Caller in Excel VBA.
I have a piece of code attached to a button/shape that I want to use to print a timestamp in the cell next to the button.
I need to use this button over and over so don't want to reference actual ranges of where to put the timestamp, so I thought this code would work.
However, each time the macro gets to the 2nd line, i.e. the Application.Caller row, it gives me an error:
ERROR 91 : 'Object variable or With block variable not set'
I would greatly appreciate if someone could assist with why this is happening as I can't fix it.
Thank you
I have tried activesheet.buttons as well as activesheet.shapes but neither have worked.
Dim x As Object
Set x = ActiveSheet.Shapes(Application.CALLER)
With x.TopLeftCell
Range(Cells(x.Row, x.Column), Cells(x.Row, x.Column)).Offset(0, 1).Select
End With

1004 No cells were found - Error Handling

Can anyone advise on how to handle a "no cells were found" error with the following code. This is a part of a larger sub that may often return no values, however handling the error as follows (which works for many of my other scenarios) still returns "Run-time error '1004': No cells were found". What am I doing wrong?
On Error GoTo Error_Exit_3
Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
Error_Exit_3:
What I Would do:
Dim RowsWithFormulas As Long
On Error Resume Next
RowsWithFormulas = Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).Rows.Count
On Error GoTo 0
If RowsWithFormulas > 0 Then
Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
End If
You can also set this as a Range:
Sub t()
Dim cellsWithErroringFormulas As Range
On Error Resume Next
Set cellsWithErroringFormulas = Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0
If cellsWithErroringFormulas Is Nothing Then
' Do whatever
MsgBox ("No formulas result in an error!")
Exit Sub
ElseIf cellsWithErroringFormulas.Rows.Count > 0 Then
cellsWithErroringFormulas.SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
' Now, if you call `cellsWithErroringFormulas` again, it will error since you removed all those references.
' So to be explicit, let's clear that variable.
Set cellsWithErroringFormulas = Nothing
End If
End Sub
I tweaked the variable name, just because you're not technically looking for rows with formulas, but rather cells with formulas that result in an error. It's a little clunky here, so rename as desired. Just wanted to point that out.
Also, since I don't know what you plan on doing next, I added the Set cellsWithErroringFormulas = Nothing, since we can't use that reference after you delete the erroring rows. You may not need that, but I just wanted to include to point that out also.
Please check the link below. its already answered in this forum.
Also, Please visit the rules for this forum.. :)
1004 Error: No cells were found, easy solution?

Find command giving error: "Run-time Error '91': Object variable or With block variable not set"

Trying to activate the cell in my column A which says "Generator loading".
I have been trying the 'With' and 'End With' commands and other suggested formats posted on the net. However, I keep getting the same error- Run-time Error 91.
From my various trials I am very sure that there is something wrong within the "Find" command, but I cannot figure out what... I have been filling it using the format on the MSDN page.
Do you have any suggestions?
Dim findstring As String
findstring = "Generator loading"
Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1)).Activate
The error commonly arises if the result of the .Find method is Nothing, because you can't do Nothing.Activate
First, you have to check for Nothingness
Dim rng as Range
Set rng = Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1))
If rng Is Nothing Then
MsgBox findString & " not found!!", vbCritical
Exit Sub
End If
rng.Activate
'the rest of your code goes here...
I don't like to work with columns, so instead of it, I used a range.
For me to work, I just replaced the reference of the sheet
Sheets(1).Range("A:A").Find(What:=findstring, After:=Cells(9, 1)).Activate
See if this works. If not, you could send me the sheet if you want :)

Resources