获取Excel Vba运行时错误1004
给予 运行时错误1004-“运行时错误“1004”: 应用程序定义或对象定义错误“获取Excel Vba运行时错误1004,vba,excel,Vba,Excel,给予 运行时错误1004-“运行时错误“1004”: 应用程序定义或对象定义错误“ 似乎无法理解。正确调暗VAR,并使用application.inputbox获取更多选项 Private Sub CommandButton3_Click() Dim rownum, startcol, endcol, holder As Integer rownum = InputBox("Enter the row that swapping process going to happen")
似乎无法理解。正确调暗VAR,并使用application.inputbox获取更多选项
Private Sub CommandButton3_Click()
Dim rownum, startcol, endcol, holder As Integer
rownum = InputBox("Enter the row that swapping process going to happen")
startcol = InputBox("Enter the column of the cell you want to swap")
endcol = InputBox("Enter the column of the cell that you wanted swap with")
holder = Cells(rownum, startcol).Value
Cells(rownum, startcol).Value = Cells(rownum, endcol).Value
Cells(rownum, endcol).Value = holder
End Sub
类型:=1
告诉application.inputbox需要一个数字type:=2
告诉application.inputbox需要一个字符串。我看到的第一件事是需要清理DIM语句……只有holder
被设置为整数,其他所有内容都是变量。其次,您可能不希望rownum
是一个整数,因为在一个大的电子表格中,您可能很容易达到它的限制。您还需要将父工作表分配给每个范围对象:工作表(“Sheet1”)。单元格(…
由于您没有正确地将变量设置为Dim
,我猜问题是rownum
等填充了字符串
。修复这些变量,其余的就可以了。非常感谢您,这是一个简单的错误。我没有意识到它们是可变的。同样,根据您的上一个问题,我建议您提问“我该怎么做…”或“该怎么做…”的形式。在这里,“请帮助我”的形式通常被理解为“为我做我的工作”,即使这不是你想要的。
'using column numbers
Private Sub CommandButton3_Click()
Dim rownum as long, startcol as long, endcol as long, holder As variant
rownum = application.InputBox("Enter the row that swapping process going to happen", type:=1)
startcol = application.InputBox("Enter the column number of the cell you want to swap", type:=1)
endcol = application.InputBox("Enter the column numbedr of the cell that you wanted swap with", type:=1)
holder = Cells(rownum, startcol).Value
Cells(rownum, startcol).Value = Cells(rownum, endcol).Value
Cells(rownum, endcol).Value = holder
End Sub
'using column letters
Private Sub CommandButton3_Click()
Dim rownum as long, startcol as string, endcol as string, holder As variant
rownum = application.InputBox("Enter the row that swapping process going to happen", type:=1)
startcol = application.InputBox("Enter the column letter of the cell you want to swap", type:=2)
endcol = application.InputBox("Enter the column letter of the cell that you wanted swap with", type:=2)
holder = Cells(rownum, startcol).Value
Cells(rownum, startcol).Value = Cells(rownum, endcol).Value
Cells(rownum, endcol).Value = holder
End Sub