【发布时间】:2016-03-23 14:11:01
【问题描述】:
VBA 和 Excel 新手,多年未编写任何程序。快速提问。我们正在尝试在 Excel 中创建一个库存列表,其中一个按钮调用一个可以插入数据的输入框。输入框中的数据应该插入到第一个可用行中。每行有 10 列,每行代表 Inventory 中的一个新项目。这是我的代码,它给了我“运行时错误 424:需要对象”:
Dim iDate, iVessel, iOrder, iCourier, iWaybill, iSender, iPcs, iWeight, iRemark As Variant
Dim col As Integer
Public Function selectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
col = 2
sourceCol = col
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
While col < 11
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
If sourceCol = 2 Then
Cell.Value = iDate
ElseIf sourceCol = 3 Then
Cell.Value = iVessel
ElseIf sourceCol = 4 Then
Cell.Value = iOrder
ElseIf sourceCol = 5 Then
Cell.Value = iCourier
ElseIf sourceCol = 6 Then
Cell.Value = iWaybill
ElseIf sourceCol = 7 Then
Cell.Value = iSender
ElseIf sourceCol = 8 Then
Cell.Value = iPcs
ElseIf sourceCol = 9 Then
Cell.Value = iWeight
ElseIf sourceCol = 10 Then
Cell.Value = iRemark
End If
col = col + 1
End If
Next
Wend
End Function
Public Sub newParcel_Click()
Call selectFirstBlankCell
iDate = InputBox("Tast inn dato, format dd.mm.yyy")
iVessel = InputBox("Tast inn båtens navn")
iOrder = InputBox("Tast inn eventuell P.O.")
iCourier = InputBox("Tast inn courier")
iWaybill = InputBox("Tast inn waybillnummer")
iSender = InputBox("Tast inn avsender")
iPcs = InputBox("Tast inn antall kolli")
iWeight = InputBox("Tast inn vekt")
iRemark = InputBox("Tast inn eventuelle anmerkninger")
End Sub
【问题讨论】:
-
我将您的代码粘贴到 VBA 页面中,乍一看,它卡在 Cell.Value = iDate(或其他选择之一)上。 iDate 尚未初始化,因为您已经调用了 selectFirstBlankCell。可能还有其他问题,但这是我遇到的第一个问题。不太确定您的意图是什么,因此很难提出解决方法。
-
我认为您应该使用
Selection.Value =...而不是Cell.Value =...。但你真的应该阅读How to avoid using Select。