Excel函数 - 插入自由单元格的输入框中的值

时间:2016-03-23 14:11:02

标签: excel vba excel-vba

VBA和Excel新手,多年来没有编程任何东西。快问。我们正在尝试在Excel中创建一个清单列表使用一个调用输入框的按钮可以插入数据。输入框中的数据应插入第一个可用行。每行有10列,每行代表清单中的一个新项目。这是我的代码,它给了我"运行时错误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

1 个答案:

答案 0 :(得分:0)

Cell不是vba中的对象。您应该使用范围对象。例如,如果要从sheet1中的A1单元格中选择一个值,则必须像Worksheets(“sheet1”)一样引用它。范围(“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