根据搜索条件在特定列中填充数据

时间:2019-07-10 11:39:53

标签: excel vba

我正在尝试通过用户窗体输入数据以查找特定的库存物品,然后找到最后一个空行以输入数据。发生的情况是先前的数据被覆盖,然后新的数据填充在其下。它没有找到特定列的最后一行。

Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim iRow As Long
Dim Lastrow As Long
Dim Found As Range

Set ws = Worksheets("Inventory Log")

Lastrow = ws.Range("C:WD").Find("*", , , , xlByRows, xlPrevious).Row


If Me.itemnumber.Value = "" Then
    MsgBox "select item number please"
    Else
        Set Found = ws.Range("C:WD").Find(What:=Me.itemnumber.Value, _
                                                   LookIn:=xlValues, _
                                                   LookAt:=xlWhole, _
                                                  SearchOrder:=xlByRows,_
                                                SearchDirection:=xlNext,_                                                     
                                                 MatchCase:=False)

    If Found Is Nothing Then
               MsgBox "Not found"
    Else
       For iRow = 15 To Lastrow
           ws.Cells(iRow, Found.Column).Value = Me.ponumber.Value
           ws.Cells(iRow, Found.Column).Offset(1, 0).Value = Me.quantity.Value
       Next iRow
    End If
End If


With ws
    .Cells(iRow, Found.Column).Value = "PO#:" & "" & Me.ponumber.Value
    .Cells(iRow, Found.Column).Offset(1, 0).Value = Me.quantity.Value
End With

End Sub

第一个条目可以正常工作,但是在第二个条目之后,先前的数据将被覆盖,然后如果它在同一列中,则将其填充到单元格中。如果它的列不同,则正确的数据将填充在第一行下方和上方(请参见下图)。

enter image description here

1 个答案:

答案 0 :(得分:0)

好吧,在玩耍之后,我终于按照我想要的方式工作了。代码是

Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim iRow As Long
Dim Lastrow As Long
Dim Found As Range
Dim rangeheader As Range
Dim col As Long

Set rangeheader = Range("8:8")
Set ws = Worksheets("Inventory Log")

With ws
    Set Found = rangeheader.Find(What:=Me.itemnumber.Value, _
                                                   LookIn:=xlValues, _
                                                   LookAt:=xlWhole, _
                                                   SearchOrder:=xlByColumns, _
                                                   SearchDirection:=xlNext, _
                                                   MatchCase:=False)

  If Not Found Is Nothing Then
      col = Found.Column
      Lastrow = Cells(Rows.Count, col).End(xlUp).Row
      For iRow = 15 To Lastrow
          If ws.Cells(iRow, col).Offset(0, 0).Value = "" Then
            ws.Cells(iRow, col).Offset(0, 0).Value = "-------"
            ws.Cells(iRow, col).Offset(1, 0).Value = Me.todaysdate.Value
            ws.Cells(iRow, col).Offset(2, 0).Value = Me.ponumber.Value
            ws.Cells(iRow, col).Offset(3, 0).Value = Me.quantity.Value
          End If
      Next iRow

    Else
      MsgBox "not found"
  End If
End With

With ws
  .Cells(iRow, Found.Column).Offset(0, 0).Value = "-------"
  .Cells(iRow, Found.Column).Offset(1, 0).Value = Me.todaysdate.Value
  .Cells(iRow, Found.Column).Offset(2, 0).Value = "PO#:" & "" & Me.ponumber.Value
  .Cells(iRow, Found.Column).Offset(3, 0).Value = Me.quantity.Value
End With

End Sub