在excel 2013中,我希望是否有人可以在单击此按钮(即activex控件)后,根据用户输入创建一个代码,用于向电子表格添加1个空白列。该列将根据我的表中有多少行结束,这意味着如果有10行,我不应该在电子表格的第11行看到一列。
我输入要添加的列后,我一直收到错误说应用程序定义或对象错误,我甚至尝试将两个大写字母小写并出现相同的错误。
Private Sub CommandButton2_Click()
Dim x As String
x = InputBox("Enter a column that you want to add: ", "What column?")
If x = "" Then Exit Sub
ColumnNum = x
Columns(ColumnNum & ":" & ColumnNum).Insert shift:=xlShiftRight
Columns(ColumnNum - 1 & ":" & ColumnNum - 1).Copy Range("A1" & ColumnNum)
Columns(ColumnNum & ":" & ColumnNum).ClearContents
End Sub
答案 0 :(得分:1)
这将帮助您入门:
Dim x As Variant
Dim ColumnNum%
x = InputBox("Enter a column that you want to add: ", "What column?")
If x = "" Then Exit Sub
ColumnNum = x
ThisWorkbook.Sheets("Sheet1").Columns(ColumnNum).Insert shift:=xlRight
ThisWorkbook.Sheets("Sheet1").Columns(ColumnNum - 1).Copy
'THe line above doesnt make any sense whatsoever.
'Im not going to try and trouble shoot it but it seems like you dont understand how to
' properly scuplt things. Youll notice i changed how you strucutred the .copy part.
'THe part that doesnt make sense to me is the Range section.
ThisWorkbook.Sheets("Sheet1").Columns(ColumnNum).ClearContents
您的代码存在一些问题。
我建议您花一些时间阅读基础知识:)