Vba 添加行和添加列的按钮?
如图所示,我有一张桌子。当按下add row and add column按钮时,如果用户想要在生成的表的C中输入列,则表示用户输入;行按钮也是如此 如果我使用列按钮在C中添加了一列,并且使用行按钮在第5行添加了一行,请查看发生了什么: 注意C栏颜色是如何扭曲的 唯一不会发生这种情况的是,用户输入的值在表的末尾创建了行和列 添加行按钮代码:Vba 添加行和添加列的按钮?,vba,excel,Vba,Excel,如图所示,我有一张桌子。当按下add row and add column按钮时,如果用户想要在生成的表的C中输入列,则表示用户输入;行按钮也是如此 如果我使用列按钮在C中添加了一列,并且使用行按钮在第5行添加了一行,请查看发生了什么: 注意C栏颜色是如何扭曲的 唯一不会发生这种情况的是,用户输入的值在表的末尾创建了行和列 添加行按钮代码: Private Sub CommandButton21_Click() Dim varUserInput As Variant Dim inpt As
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
Private子命令按钮22_单击()
'将列添加到表中
Dim userinput作为字符串
Dim QuestionToMessageBox作为字符串
Dim colIndex作为变体
暗淡的StrtRow一样长,EndRow一样长,i一样长
作为ListObject的Dim oLo
userinput=MsgBox(“是否要在表的末尾添加列?”,vbYesNo+vbQuestion,“添加列选项”)”用户输入
如果userinput=vbNo,则选择“否”条件
出现错误时转到“已取消”
colIndex=Application.InputBox(“输入要添加的列:”,“什么列?”)
如果colIndex=“”,则退出Sub
使用此工作簿。工作表(“工作表1”)
.Columns(colIndex).Insert shift:=xlRight'因为您正在处理一个ListObject
格式设置应该可以正常工作,所以我删除了所有应该设置表格式的代码。我可能遗漏了什么,你需要把它补回来
您在列过程的开头有一个On Error Goto Cancelled
语句,它基本上是一个On Error Exit Sub
语句。除非您非常清楚您预期的错误,否则您的代码可能会在出现任何错误时退出,并产生意外或误解的结果
下面是我简化这两个例程的尝试。为了避免重复,我做了很多改变,去掉了一些变量,做了一些其他的改变
我还将InputBox
更改为Application.InputBox
,它允许您指定输入类型。这意味着空白响应将向用户抛出一条令人困惑的消息,因此我在InputBox
提示符周围放置了Application.DisplayAlerts
,以抑制该消息
我希望这一切按原样进行,我希望你所希望的会有所偏差。希望它能让你半途而废,给你展示一些新的技巧
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
你能不能创建一个简短的例子来展示这个问题而不需要额外的细节?好吧,我试着把它缩短,但是代码有点长,也是这个问题的例子,我不知道如何缩短它,因为这是我唯一的问题。道格,你在吗?我不知道怎么把它缩短?我来了。我来看看。