【问题标题】:Excel functions - values from input box inserted into free cellsExcel 函数 - 输入框中的值插入到空闲单元格中
【发布时间】: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

标签: vba excel


【解决方案1】:

Cell 不是 vba 中的对象。您应该改用范围对象。例如,如果要从 sheet1 中的 A1 单元格中选择一个值,则必须像 Worksheets("sheet1").Range ("A1") 一样引用它。您可以使用两个范围对象使用不同的方法,一个用于单元格,一个用于范围;例如在 A 列中的某些单元格中循环:

Dim lastrow As Integer
Dim eachcell as range
Dim total_range as range

lastrow = Worksheets("sheet name").Columns("A").Find("", Cells(Rows.Count, "A")).Row

set total_range = Worksheets("sheet name").Range("A1:A" & lastrow)

for each eachcell in total_range
   'do something
next eachcell 

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 2017-11-15
    • 2017-05-29
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多