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.
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