I am getting "run time error '91': Object variable or With block variable not set" on the following code and can't seem to work out what is wrong.
I have set the following variables:
Dim EngineAncillariesProjectsVBAList As Range
Dim EngineAncillariesProjectsLastRow As Long
Dim ProjectList As Range
Which are defined by:
EngineAncillariesProjectsLastRow = Worksheets("VBA_Data").Cells(Rows.Count, 8).End(xlUp).Row
Set EngineAncillariesProjectsVBAList = Worksheets("VBA_Data").Range("H2:H" & EngineAncillariesProjectsLastRow)
The error is coming at the following stage, see below. On Debug it is saying that ProjectList = Nothing
If ActiveSheet.Name = "Engine Ancillaries" Then ProjectList = EngineAncillariesProjectsVBAList
Related
I am attempting to determine if a range has a name and keep getting an error message.
My code looks like this:
Dim rCell As Range
Dim sName As String
Set rCell = Range("A1")
If Not rCell.Name Is Nothing Then sName = rCell.Name
And produces the following error message:
Run-time error '1004':
Application-defined or object-defined error
I have also tried replacing the
If Not rCell.Name Is Nothing Then sName = rCell.Name line with sName = rCell.Name and even Debug.Print rCell.Name but keep getting the same error.
What am I doing wrong?
Range.Name throws a RTE 1004 if the range is not named. One alternative is to try to assign to a Name object.
Note that you probably want the Name.Name property.
Set rCell = Range("A1")
On Error Resume Next
Dim n As Name
Set n = Nothing ' only including in case you are planning to use this approach in a loop.
Set n = rCell.Name
On Error GoTo 0
If not n Is Nothing Then
sName = n.Name ' Name.Name, not Range.Name
End If
I am at my wits end trying to decode this error I am getting.
In the syntax below, the line which throws up the error is NumColumnsTarget = .Range("A1").End(xlToRight).Column
As far as I can tell, I have assigned all the objects correctly. One thing I have noticed is that changing the order in which the Range variables are declared in (Ie: NumRowsTarget, NumColumnsTarget, NumColumnsLOP, NumRowsLOP as opposed to NumRowsLOP, NumColumnsLOP, NumRowsTarget, NumColumnsTarget) will change which variable throws the Run-Time error up and I am lost for a reason why. Any thoughts?
Option Explicit
Sub MergeRecords()
Application.ScreenUpdating = False
Dim TargetRecordArray(), LOPRecordArray() As Variant
Dim ws As Worksheet
Dim i, j As Integer
Dim LOPSerialNo, TargetSerialNo As String
Dim NumRowsLOP, NumColumnsLOP, NumRowsTarget, NumColumnsTarget As Range
Set ws = ThisWorkbook.Sheets("LOP Records")
With ws
NumRowsLOP = .Range("A1").End(xlDown).Row
NumColumnsLOP = .Range("A1").End(xlToRight).Column
End With
Set ws = ThisWorkbook.Sheets("Target Records")
With ws
NumRowsTarget = .Range("A1").End(xlDown).Row
NumColumnsTarget = .Range("A1").End(xlToRight).Column
End With
End Sub
Edit: I am a fool. Thanks to all.
I need to use the "find" function to get the row of a value, where that value is defined from another worksheet, but I keep getting object-variable not defined errors even when I try defining everything.
I was initially able to get the code below to work. (I paste the "test" version of the code that only uses a small range of 20 cells. The result is placed in cell "A1" to make sure it works correctly in testing.)
X = Range("D6").Value
Range("A1").Value = Range("D2:D20").Find(X).Row
But when I use this line in a large data set, I keep getting error 91 ("Object variable or With block variable not set").
So instead, I try code that defines everything more explicitly, but for the life of me, I can't get this code to work even on the small test set. (I also include a few lines that I've tried but that also produce errors.)
Sub Test3()
Sheets("2017").Select
Dim X As Long
X = Range("D6").Value
Dim ws As Worksheet
Set ws = Worksheets("2017")
Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = ws.Range("D2", ws.Range("D20").End(xlUp))
Set FindRow = SearchRange.Find(X, LookIn:=xlValues, lookat:=xlWhole)
If Not FindRow Is Nothing Then ReturnRowNumber = FindRow.Row
'Range("A1").Value = FindRow 'Running this row gives application-defined error 1004
Dim i_2017 As Long
i_2017 = FindRow.Rows(1) 'All of these rows give object variable not set error 91
'i_2017 = FindRow
'i_2017 = FindRow.Rows.Item(1)
Range("A1").Value = i_2017 'Check that the sub returns 6
End Sub
If I try to return "FindRow" in cell "A1", I get error 1004 ("Application-defined or object-defined error")
If I try to store "FindRow" as another variable or use the "Item" property, I get error 91 ("Object variable or With block variable not set")
You need to enclose all of your code that assumes FindRow has actually been found inside your If Not FindRow is Nothing statement.
Sub Test3()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("2017")
Dim Found As Range, ReturnRowNumber As Long
Set Found = ws.Range("D:D").Find (ws.Range("D6"), LookIn:=xlValues, LookAt:=xlxWhole)
If Not Found Is Nothing Then
'All code that assumes your value is found goes HERE
ReturnRowNumber = Found.Row
Range("A1").Value = Found
Else
'All code that assumed your value is NOT found goes HERE
MsgBox ws.Range("2017") & ": Not Found in Range"
End If
End Sub
When passing a Worksheet to a Class Object Method, I receive:
Error 91: Object Variable or With Block variable not set.
The problem is, I do not know where it is I have not set an object.
Dim WS As Worksheet
Set WS = oExport.ExportSheet(oExport.WB)
Dim testint As Integer
Dim oAsset As clsAsset
testint = oAsset.AssetIDCol(WS)
I have checked, and the worksheet object is correctly set in Line 2 of the code. The error occurs when assigning a value to the 'testint' variable. Here is the .AssetIDCol() property from my clsAsset property:
Option Explicit
Private priv_asset_id_col As Integer
Public Static Property Get AssetIDCol(WS As Worksheet) As Integer
If Not priv_asset_id_col = Null Then
AssetIDCol = priv_asset_id_col
Exit Property
End If
Dim rngX As Range
Dim Val As Variant
Dim i As Integer
Set rngX = WS.UsedRange '.Rows(1) '.Find(SearchVal, LookAt:=xlWhole)
For i = 1 To rngX.Columns.Count
If InStr(priv_asset_id_col_name, rngX.Cells(1, i).Value) > 0 Then
AssetIDCol = i
priv_asset_id_col = i
Exit Property
End If
Next i
AssetIDCol = 0
End Property
How do I fix this error? The Get property returns an integer, so I am not sure where I am failing to use Set in creating an instance.
The error appears, because oAsset is declared, but not initialized.
The easiest way to fix it is to write Dim oAsset As New clsAsset instead of Dim oAsset As clsAsset. This way you are referring to a new object upon declaration.
Another way is to set the new object explicitly with a new line, after declaring it:
Dim oAsset As clsAsset
Set oAsset = New clsAsset
If the workbook isn't saved and closed and reopened,
I get the following error
"Run-time error '91': Object variable or With block not set"
I have the exact same code (only the name of the string is different) on other places, and sometimes it gives me the same error until I save close and reopen and re-run. Afterwards, the code runs smoothly.
Any ideas on how to avoid the bug? Have you had this problem before?
Dim fal As Excel.Worksheet
Set fal = wb.Sheets("Falancs")
Dim x As String
x = "F_1 ="
Dim cc As Integer ' The column as an integer (cc = 1,2,3...)
cc = fal.UsedRange.Find(x).Column
Error is on the last line, where the "(x)" is ...
.Find returns Nothing if value of x not found, so you should check it:
Dim fal As Excel.Worksheet
Set fal = wb.Sheets("Falancs")
Dim x As String
x = "F_1 ="
Dim cc As Integer ' The column as an integer (cc = 1,2,3...)
Dim res As Range
Set res = fal.UsedRange.Find(x)
If Not res Is Nothing Then
cc = res.Column
Else
MsgBox "Value " & x & " not found"
Exit Sub
End If