+ Reply to Thread
Results 1 to 4 of 4

VBA Error 91: Object variable or With block variable not set

  1. #1
    Registered User
    Join Date
    02-06-2013
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA Error 91: Object variable or With block variable not set

    Hi there

    I am very new to VBA and have the following error and cannot identify exactly where the problem lies: Please could someone help?
    Thank you very much
    Krista



    Option Explicit
    Option Base 1

    '-------------------------------------------------------------------------------------
    ' Macro to create report for individual builder using autofilter
    '-------------------------------------------------------------------------------------
    Public Sub CreateReportBuilder()

    Dim strBuilder As String
    Dim varAllData As Variant
    Dim varFindData As Variant
    Dim lngLastRow As Long
    Dim lngLoop As Long
    Dim lngRecordCount As Long
    Dim intFind As Integer
    Dim intFindCount As Integer
    Dim intField As Integer
    Dim intFieldCount As Integer
    Dim intHeaderRow As Integer
    Dim intPasteFirstRow As Integer
    Dim intPasteLastCol As Integer
    Dim intLastUsedRow As Integer
    Dim intPasteBuilderCol As Integer
    Dim intBuilderCol As Integer

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    On Error GoTo ErrorHandler

    With wsReportBuilder
    intHeaderRow = .Range("ClaimHeader").Row
    intPasteFirstRow = intHeaderRow + 1
    intPasteLastCol = .Rows(intHeaderRow).Find(What:="Additional Notes", LookIn:=xlValues, LookAt:=xlWhole).Column
    intLastUsedRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row - 1
    If intLastUsedRow < intPasteFirstRow Then intLastUsedRow = intPasteFirstRow
    .Range(.Cells(intPasteFirstRow, 1), .Cells(intLastUsedRow, intPasteLastCol)).ClearContents

    End With


    strBuilder = wsReportBuilder.cboBuilder.Value

    With wsDatabase
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row - 1
    varAllData = .Range(.Cells(2, 1), .Cells(lngLastRow, intPasteLastCol))
    intBuilderCol = .Rows(1).Find(What:="Builder", LookIn:=xlValues, LookAt:=xlWhole).Column

    'Loop through all data to find selected builder to determine array size for redim
    lngRecordCount = UBound(varAllData, 1)
    intFieldCount = UBound(varAllData, 2)
    For lngLoop = 1 To lngRecordCount
    If varAllData(lngLoop, intBuilderCol) = strBuilder Or varAllData(lngLoop, intBuilderCol + 1) = strBuilder Then
    intFindCount = intFindCount + 1
    End If
    Next lngLoop

    'Check if no records found
    If intFindCount = 0 Then
    ' MsgBox prompt:="No records found for " & strBuilder, Title:="No data"
    With wsReportBuilder
    intPasteBuilderCol = .Rows(intHeaderRow).Find(What:="Builder", LookIn:=xlValues, LookAt:=xlWhole).Column
    .Cells(intPasteFirstRow, 1) = "No data for " & strBuilder
    .Cells(intPasteFirstRow, intPasteBuilderCol) = strBuilder
    .Calculate
    End With

    GoTo ExitReport
    End If

    ReDim varFindData(intFindCount, intFieldCount)
    For lngLoop = 1 To lngRecordCount
    If varAllData(lngLoop, intBuilderCol) = strBuilder Or varAllData(lngLoop, intBuilderCol + 1) = strBuilder Then
    intFind = intFind + 1
    For intField = 1 To intFieldCount
    varFindData(intFind, intField) = varAllData(lngLoop, intField)
    Next intField
    End If

    Next lngLoop

    End With

    With wsReportBuilder
    .Range(.Cells(intPasteFirstRow, 1), .Cells(intFindCount + intPasteFirstRow - 1, intFieldCount)) = varFindData
    .Calculate
    End With


    ExitReport:
    ' Application.Calculation = xlCalculationAutomatic
    Exit Sub

    ErrorHandler:
    MsgBox prompt:="VBA Error " & Err.Number & vbCrLf & Err.Description, _
    Buttons:=vbOKOnly + vbCritical, Title:="VBA Error"
    Application.Calculation = xlCalculationAutomatic

    End Sub

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,884

    Re: VBA Error 91: Object variable or With block variable not set

    When you compile the code, which line(s) of code are highlighted.

    Look at the link in my signature on debugging code. Once you have analyzed your code through debugging, post back with your findings for help.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    02-06-2013
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA Error 91: Object variable or With block variable not set

    Thank you.

    I have de-bugged and the error occurs on this line of code...

    With wsReportBuilder
    intHeaderRow = .Range("ClaimHeader").Row
    intPasteFirstRow = intHeaderRow + 1
    intPasteLastCol = .Rows(intHeaderRow).Find(What:="Additional Notes", LookIn:=xlValues, LookAt:=xlWhole).Column
    intLastUsedRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row - 1
    If intLastUsedRow < intPasteFirstRow Then intLastUsedRow = intPasteFirstRow
    .Range(.Cells(intPasteFirstRow, 1), .Cells(intLastUsedRow, intPasteLastCol)).ClearContents

    End With

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,884

    Re: VBA Error 91: Object variable or With block variable not set

    for starters, in the code you presented, you have a variable wsReportBuilder that is not defined. Need to do that first. I tried to compile your code and that was the first error I got.

    Alan

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1