【发布时间】:2018-03-15 13:11:10
【问题描述】:
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
给
运行时错误 1004 - “运行时错误‘1004’: 应用程序定义或对象定义的错误”
似乎无法理解。
【问题讨论】:
-
我看到的第一件事是您的 DIM 语句需要清理...只有
holder设置为整数,其他一切都是变体。其次,您可能不希望rownum成为整数,因为您很容易在大型电子表格中达到其限制。 -
您还需要将父工作表分配给每个范围对象:
Worksheets("Sheet1").Cells(... -
由于您没有正确地
Dim变量,我猜问题是rownum等改为填充String。修复变量,剩下的就OK了 -
非常感谢,这么简单的错误。我没有意识到它们是变体。
-
根据您的最后一个问题,我建议以“我如何……”或“如何……”的形式提出问题。 “请帮助我”的形式在这里通常被理解为“为我做我的工作”,即使这不是你想要的。