I'm a complete newbie in Excel VBA and was forced to learn it within days to complete a university project, and now I'm stuck. The idea was to select between 0 to 8 from a drop-down list to generate a table with number of rows similar to the picked choice and 2 columns. Selecting 0 would clear away the table, select 1 would generate a table with 1 row, 2 for 2 rows...
The problem is that no matter what I try, it always says 'overlapping table' with the exception of 0 (There are other kind of errors as well specific to the methods I try). I do not know the proper way to write the code for adding or deleting rows or columns, and have tried and tested very hard but I still can't make it work. Also, is it possible that I could predefine the column title of the generated table? Please help! I've also attached a picture for better understanding.
Private Sub Workbook_Open()
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Select Case Range("B21")
Case "0"
Range("$A$24:$B$32").Clear
Case "1"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$25"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
ActiveSheet.ListObjects("CLOTable").Range("A24").Select
Case "2"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$26"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
Case "3"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$27"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
Case "4"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$28"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
Case "5"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$29"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
Case "6"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$30"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
Case "7"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$31"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
Case "8"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$24:$B$32"), , xlYes).Name = "CLOTable"
ActiveSheet.ListObjects("CLOTable").TableStyle = "TableStyleLight2"
End Select
End Sub
Bookmarks