"Object variable or With block variable not set" in VBA

pApaAPPApapapa

I am looking to calculate a column (in wsOut) of averages using VBA. The input is in another sheet (wsRefor).

I use the following code, where I use the worksheet function to calculate the average

Dim Avg As Double
Dim AvgRange As Range
Set Reformulering = ActiveSheet

For i = 1 To lastCol
    AvgRange = Range(wsRefor.Cells(1 + i, 4), wsRefor.Cells(1 + i, lastCol))
    wsOut.Cells(i + 1, 4).Value = Application.WorksheetFunction.Average(AvgRange)
Next

Yet, I get the mistake, from the second line inside of the for-loop:

"Object variable or With block variable not set"

I am not sure I understand the error from videos I have watched and other forum discussion, so I am hoping anyone can explain or potentially point of the mistake

Shai Rado

Assuming you defined Dim wsRefor As Worksheet, and set it to the right Sheet, then modify your line:

AvgRange = Range(wsRefor.Cells(1 + i, 4), wsRefor.Cells(1 + i, lastCol))

to:

Set AvgRange = wsRefor.Range(Cells(1 + i, 4), Cells(1 + i, lastCol))

or, on the safe side:

With wsRefor
     Set AvgRange = .Range(.Cells(1 + i, 4), .Cells(1 + i, lastCol))
End With

Edit 1: full code which I've tested (also has error handling for the Average Function)

Option Explicit

Sub DynamicAvgRange()

Dim wsRefor             As Worksheet
Dim wsOut               As Worksheet
Dim Avg                 As Double
Dim AvgRange            As Range
Dim lastCol             As Long
Dim i                   As Long

Set wsRefor = ThisWorkbook.Sheets("Refor")
Set wsOut = ThisWorkbook.Sheets("Out")

' just for simulating the tests
lastCol = 6

For i = 1 To lastCol
    With wsRefor
        Set AvgRange = .Range(.Cells(1 + i, 4), .Cells(1 + i, lastCol))
    End With
    If Not IsError(Application.Average(AvgRange)) Then
        wsOut.Cells(i + 1, 4).Value = Application.Average(AvgRange)
    Else
        ' Average value returned an error (no values in the searched range)
        wsOut.Cells(i + 1, 4).Value = "" ' put a blank value >> modify to your needs
    End If
Next i

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Object Variable or With Block Variable not set getelementsbyname vba

Object variable or With block variable not set error in vba

Excel VBA - Error: Object Variable or With Block Variable not set

Excel vba Object variable or With block variable not set error

VBA error 91: Object variable or With block variable not set (Excel 2013)

VBA Excel , 91 object variable or with block variable not set

VBA For Each and With Block Object Variable or With Block Variable not set Error 91

Range variable errors in Excel VBA ; "Object variable or With block variable not set"

VBA Excel SQL object variable or with block variable not set

Object Variable or With Block Variable Not Set for Simple VBA For loop

VBA: I get the "object variable or with block variable not set"

check if getelementsbyclassname exists in excel vba. runtime error 91 object variable or with block variable not set

rg.NumberFormat = "0" showing object variable or with block variable not set error VBA

VBA: Run-time error 91: Object variable or With block variable not set

New to Excel-VBA: Run Time Error "91": Object Variable or With block variable not set

VBA: An object in a loop is going to "Nothing" and is giving me an "Object variable or With block variable not set" Error

Getting Object Variable, Block Variable not set error trying to use VBA to pull SQL Server stored procedure

MS Excel 2007 VBA function error (Object variable or with block variable not set)

Run-time Error 91 - Object Variable or with Block variable not set - VBA

All of the sudden getting Excel VBA error code 91: "Object variable or With block variable not set" after code had been working and no edits made

Object variable or With block variable not set

91 object variable or with block variable not set

dbOpenDynaset - 'Object variable or With block variable not set'

EXCEL - object variable or with block variable not set

It says Object variable or With block variable is not set

run time error 91 - object variable or with block variable not set

Object variable or block variable not set when processing HTMLSelectElement

How to solve an error object variable or with block variable not set?

Assigning an object to a collection: error '91': Object variable or With block variable not set