runtime error 91: object variable or with block variable not set

benefuexx

New Member
Joined
Mar 28, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm having trouble with a sub that i set up to create tables with specific sizes and inputs.

I get the error on this line: .ListColumns(1).DataBodyRange.Cells(1, 1).Value = "WR 1" and only if numRows is 1. if its higher than 1 everything runs fine and all tables get created and filled out the way they should.
at the point of error Table_b was created and has a total of 2 rows (1 being the title row). debug prints are numRows =1, and destTbl = Table_b at point of error in case that helps.

The number is being determined by searching a different sheet ("Layoutexport") for tables that end with a number. every found table should create 1 row in table_b and then fill out the first column (with WR +table number) before proceeding with the subroutines at the bottom. any ideas on how to solve this error? I can't figure out why it's only happening with numRows=1. at least not at this stage of the code.

VBA Code:
Sub GenerateTableB()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim destTbl As ListObject
    Dim srcTbl As ListObject
    Dim i As Integer
    Dim f As Integer
    Dim maxTableNum As Integer
    Dim nextRow As Long
    Dim numRows As Integer
    Dim numColumns As Integer
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Dokumentation")
    
    ' Delete existing "Table_b" if it exists
    For Each tbl In ws.ListObjects
        If tbl.name = "Table_b" Then
            tbl.Delete
            Exit For
        End If
    Next tbl
    
    ' Find the last row of Table_a
    nextRow = ws.ListObjects("Table_a").Range.Rows(ws.ListObjects("Table_a").Range.Rows.count).Row + 3
    
    ' Create Table_b starting from this row
    Set destTbl = ws.ListObjects.Add(xlSrcRange, ws.Range("A" & nextRow), , xlYes)
    
    ' Set the name of the table to "Table_b"
    destTbl.name = "Table_b"
    destTbl.ShowAutoFilter = False ' Disable autofilter
   
    ' Set the column titles for Table_b
    With destTbl
        .HeaderRowRange.Cells(1, 1).Value = "WR"
        .HeaderRowRange.Cells(1, 2).Value = "belegte MPP's"
        .HeaderRowRange.Cells(1, 3).Value = "Ausrichtungen Stränge"
        .HeaderRowRange.Cells(1, 4).Value = "Stranglängen"
        .HeaderRowRange.Cells(1, 5).Value = "Leistung Ist [kWp]"
        .HeaderRowRange.Cells(1, 6).Value = "Leistung Soll [kVA]"
        .HeaderRowRange.Cells(1, 7).Value = "AC/DC"
        .HeaderRowRange.Cells(1, 8).Value = "DC/AC"
        
        ' Determine the number of rows based on the number of tables in "Layoutexport"
        numRows = 0
        For Each tbl In ThisWorkbook.Sheets("Layoutexport").ListObjects
            If Left(tbl.name, 6) = "table_" And IsNumeric(Mid(tbl.name, 7)) Then
                numRows = numRows + 1
            End If
        Next tbl
        
        ' Set the number of columns for Table_b
        numColumns = 8 ' 8 columns in total
        
        ' Validate numRows
        If numRows > 0 Then
            
            If numRows = 1 Then
                Debug.Print numRows
                Debug.Print destTbl
                .Resize .Range.Resize(2, numColumns) ' 1 header row + 1 data row
                .ListColumns(1).DataBodyRange.Cells(1, 1).Value = "WR 1"
            ElseIf numRows > 1 Then
                .Resize .Range.Resize(numRows + 1, numColumns) ' +1 for the header row
                For f = 1 To numRows
                    .ListColumns(1).DataBodyRange.Cells(f, 1).Value = "WR " & f
                Next f
            End If
            
        Else
            MsgBox "No data found in Layoutexport tables.", vbExclamation
            Exit Sub
        End If
    End With
    
    ' Loop through all tables in "Layoutexport" with numbers at the end
    For i = 1 To numRows ' Loop up to the number of tables in "Layoutexport"
        On Error Resume Next ' Continue to next iteration if the table does not exist or is empty
        Set srcTbl = ThisWorkbook.Sheets("Layoutexport").ListObjects("table_" & i)
        On Error GoTo 0
        
        If Not srcTbl Is Nothing And srcTbl.ListRows.count > 0 Then
            PopulateMPPs destTbl, srcTbl, i
            PopulateStrings1 destTbl, srcTbl, i
            PopulateCombos destTbl, srcTbl, i
            Power2 destTbl, srcTbl, i
        End If
    Next i
    
    Power1 destTbl, srcTbl
    CalculateDivision1
    CalculateDivision2
    
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
The DataBodyRange object of a table is "Nothing" when the table is completely empty. You'll have to check for the tables' ListRows.Count being <> 0
 
Upvote 0
Solution
The DataBodyRange object of a table is "Nothing" when the table is completely empty. You'll have to check for the tables' ListRows.Count being <> 0
thank you for pointing me in the right direction. I had to additionally work with add rows and columns instead of my initial try. it seems to work fine now no matter what situation it starts with and the table no longer claims to be not set during that step. :)
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,125
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top