我在图片中看到了一张桌子。按下时添加行和添加列按钮会获取用户输入,这意味着如果用户想要列,则让它说出生成的表的C;对于行按钮也一样。
如果我使用列按钮在C 中添加了一列,并使用行按钮添加第5行,请查看发生的情况:
注意列C颜色是如何扭曲的?
唯一不会发生这种情况的方法是,用户输入了在表格末尾创建行和列的值。
添加行按钮代码:
Private Sub CommandButton21_Click()
Dim varUserInput As Variant
Dim inpt As String
Dim oLo As ListObject
Dim RowNum
inpt = MsgBox("Do You Want To Add A Row At The END Of The Table?", vbYesNo + vbQuestion, "Add Row Choice") 'user input
If inpt = vbNo Then
' add row to table 'runs if condition is user selected no
varUserInput = InputBox("Enter The Row Number You Want To Generate:", _
"What Row?")
If varUserInput = "" Then Exit Sub
RowNum = varUserInput 'adds row based on user input
Rows(RowNum & ":" & RowNum).Insert shift:=xlDown
Rows(RowNum - 1 & ":" & RowNum - 1).Copy Range("A" & RowNum)
Range(RowNum & ":" & RowNum).ClearContents
Else
Set oLo = ActiveSheet.ListObjects(1) 'first table on sheet
With oLo
.ListRows.Add AlwaysInsert:=True 'adds row to end of table
.Range.Rows(.Range.Rows.Count).RowHeight = 30
End With
End If
End Sub
添加列按钮:
Private Sub CommandButton22_Click()
' add column to table
Dim userinput As String
Dim QuestionToMessageBox As String
Dim colIndex As Variant
Dim StrtRow As Long, EndRow As Long, i As Long
Dim oLo As ListObject
userinput = MsgBox("Do you want to add the column at the END of the table?", vbYesNo + vbQuestion, "Add Column Choice") 'user input
If userinput = vbNo Then 'condition if no is selected
On Error GoTo Canceled '
colIndex = Application.InputBox("Enter a column that you want to add: ", "What column?")
If colIndex = "" Then Exit Sub
With ThisWorkbook.Sheets("Sheet1")
.Columns(colIndex).Insert shift:=xlRight '<--| reference column you want to insert
'sheet row numbers from table rows
Set oLo = .ListObjects(1) '<~~ first table on sheet
With oLo
StrtRow = .ListRows(1).Range.Row
EndRow = .ListRows.Count + StrtRow - 1
End With
For i = StrtRow To EndRow
.Cells(i, colIndex).Interior.Color = .Cells(i, 1).DisplayFormat.Interior.Color
Next i
End With
Else 'condition if yes is selected
Set oLo = ActiveSheet.ListObjects(1) 'first table on sheet
With oLo
.ListColumns.Add
.ListColumns(.ListColumns.Count).Range.ColumnWidth = 25
End With
'macro loops through to end of table to generate the proper around column lines
Range("Table1[[#Headers],[Stages]]").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 1).Activate
Loop
ActiveCell.Offset(0, -1).Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End If
Canceled:
End Sub
答案 0 :(得分:0)
因为您正在处理ListObject
格式应该正常工作,所以我删除了应该格式化表格的所有代码。我可能错过了一些东西,你需要把它添加回来。
您在列过程的开头有一个On Error Goto Cancelled
语句,基本上是On Error Exit Sub
语句。除非您非常清楚自己的错误,否则您的代码可能会因意外或误解的结果而退出任何错误。
以下是我尝试简化这两个例程。我动了很多东西试图避免重复,摆脱一些Variant变量并进行其他更改。
我还将InputBox
更改为Application.InputBox
,这允许您指定输入类型。这意味着空白响应会向用户抛出一个令人困惑的消息,因此我将Application.DisplayAlerts
放在InputBox
提示符周围以禁止显示该消息。
我希望这样的工作原理,我希望你的想法会有所改变。希望它会让你在那里,并向你展示一些新的技巧!
Private Sub CommandButton21_Click()
'add row to table
Dim InputPosition As Long
Dim InputEndOfTable As VbMsgBoxResult
Dim oLo As ListObject
Set oLo = ActiveSheet.ListObjects(1) '
With oLo
InputEndOfTable = MsgBox("Do You Want To Add A Row At The END Of The Table?", vbYesNo + vbQuestion, "Add Row Choice")
If InputEndOfTable = vbNo Then
Application.DisplayAlerts = False
InputPosition = Application.InputBox(Prompt:="Enter The Row Number You Want To Add:", Title:="What Row?", Type:=1)
Application.DisplayAlerts = True
Else
InputPosition = .Range.Rows.Count + 1
End If
If InputPosition = 0 Then Exit Sub
If InputEndOfTable = vbYes Then
.ListRows.Add
Else
.ListRows.Add InputPosition
End If
.Range.Rows(InputPosition).RowHeight = 30
End With
End Sub
Private Sub CommandButton22_Click()
'add column to table
Dim InputPosition As Long
Dim InputEndOfTable As VbMsgBoxResult
Dim oLo As ListObject
Set oLo = ActiveSheet.ListObjects(1) '
With oLo
InputEndOfTable = MsgBox("Do You Want To Add A Column At The END Of The Table?", vbYesNo + vbQuestion, "Add column Choice")
If InputEndOfTable = vbNo Then
Application.DisplayAlerts = False
InputPosition = Application.InputBox(Prompt:="Enter The Column Number You Want To Add:", Title:="What Column?", Type:=1)
Application.DisplayAlerts = True
Else
InputPosition = .Range.Columns.Count + 1
End If
If InputPosition = 0 Then Exit Sub
If InputEndOfTable = vbYes Then
.ListColumns.Add
Else
.ListColumns.Add InputPosition
End If
.ListColumns(InputPosition).Range.ColumnWidth = 25
End With
End Sub