查找下一个空白行,然后添加公式

时间:2019-05-22 14:26:49

标签: excel vba excel-formula

我有一个用户窗体,该窗体会将combobox1值粘贴到B列中的下一个可用行。右边的3列(C,D和E)具有vlookup公式,我想将它们填充为combobox1值粘贴到B列。

这是vlookup公式,B123将是该特定行的combobox1值 = IFERROR(VLOOKUP(B123,'Sheet1'!$ A $ 3:$ C $ 370,2,0),0)/ 1000000

这是我到目前为止尝试过的

  Dim nextrow As Long 
  Dim nextrow1 As Long
  Dim nextrow2 As Long
  nextrow = Cells(Rows.Count, "C").End(xlUp).Row + 1
        .Range(rows.count, nextrow - 1).FillDown
  nextrow1 = Cells(Rows.Count, "D").End(xlUp).Row + 1
        .Range(rows.count, nextrow - 1).FillDown
  nextrow2 = Cells(Rows.Count, "E").End(xlUp).Row + 1
        .Range(rows.count, nextrow - 1).FillDown
  End Sub

此代码会将组合框值粘贴到B列中的下一个可用行

Private sub CommandButton1_click()

    With Worksheets("sheet")
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = ComboBox1.Value 
        .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = ComboBox2.Value 
    End With
ActiveWorkbook.RefreshAll
Unload Me

1 个答案:

答案 0 :(得分:1)

我在您的代码中添加了一行,每次您单击按钮时该代码都会自动填充。

Private sub CommandButton1_click()

With Worksheets("sheet")
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = ComboBox1.Value 
    .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = ComboBox2.Value
    .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).AutoFill .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)

    With .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)
        .Borders.LineStyle = xlContinuous
    End With                   

End With
ActiveWorkbook.RefreshAll
Unload Me
相关问题