2016-03-23 19 views
0

VBAとExcelの新機能で、何年も何もプログラムしていません。クイック質問。 Excelでインベントリリストを作成しようとしています。データを挿入できる入力ボックスを呼び出すボタンがあります。入力ボックスからのデータは最初に利用可能な行に挿入する必要があります。行ごとに10個の列があり、各行は在庫内の新しい項目を表します。ここに私のコードは、私に "実行時エラー424:オブジェクトが必要"を与える:Excel関数 - 入力ボックスからの値がフリーセルに挿入されました

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 
+0

私はVBAのページにコードを貼り付けて、一見すると、それがCell.Value = i日付(または他の選択肢の1つ)に貼り付けます。既にselectFirstBlankCellを呼び出しているため、iDateはまだ初期化されていません。他にも問題があるかもしれませんが、それが私が遭遇した最初の問題です。あなたの意図が何であるかはまだ分かりません。まだそれを修正する方法を提案するのは難しいです。 –

+0

私は 'Cell.Value = ...'の代わりに 'Selection.Value = ...'を使うと思っています。しかし、あなたは本当に[Selectを使うのを避ける方法](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)を読むべきです。 – Kyle

答えて

0

セルはvbaのオブジェクトではありません。代わりに範囲オブジェクトを使用する必要があります。たとえば、sheet1のA1セルから値を選択する場合は、ワークシート( "sheet1")Range( "A1")のように参照する必要があります。 2つの範囲オブジェクトを使用して異なるaproachを使用できます.1つはセル用、もう1つは範囲用です。列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 
関連する問題