如何使用vba

时间:2019-06-24 15:39:11

标签: excel vba

我有一个表单,我希望当我单击按钮时,这些值被添加到另一个工作表中的表中,我尝试使用UsedRange.Row检测最后一行,但它位于创建的表下方。我该怎么解决?

Sub agregar()
    Dim fecha As String
    Dim nombre As String
    Dim dia As String
    Dim cant_1 As Integer
    Dim cant_2 As Integer
    Dim cant_3 As Integer
    Dim ped_1 As String
    Dim ped_2 As String
    Dim ped_3 As String
    Dim pre_1 As Double
    Dim pre_2 As Double
    Dim pre_3 As Double
    Dim procede As String
    Dim ultimafila As Double


    fecha = Cells(4, 3).Value
    nombre = Cells(5, 3).Value
    dia = Cells(4, 5).Value
    cant_1 = Cells(6, 4).Value
    cant_2 = Cells(7, 4).Value
    cant_3 = Cells(8, 4).Value
    ped_1 = Cells(6, 3).Value
    ped_2 = Cells(7, 3).Value
    ped_3 = Cells(8, 3).Value
    pre_1 = Cells(6, 7).Value
    pre_2 = Cells(7, 7).Value
    pre_3 = Cells(8, 7).Value
    procede = Cells(6, 9).Value

    ultimafila = Worksheets("Control semanal").UsedRange.Row - 1 + Worksheets("Control semanal").UsedRange.Rows.Count

    Worksheets("Control semanal").Cells(ultimafila + 5, 2).Value = dia
    Worksheets("Control semanal").Cells(ultimafila + 5, 3) = fecha
    Worksheets("Control semanal").Cells(ultimafila + 5, 4) = nombre
    Worksheets("Control semanal").Cells(ultimafila + 5, 7) = ped_1
    Worksheets("Control semanal").Cells(ultimafila + 6, 7) = ped_2
    Worksheets("Control semanal").Cells(ultimafila + 7, 7) = ped_3
    Worksheets("Control semanal").Cells(ultimafila + 5, 8) = cant_1
    Worksheets("Control semanal").Cells(ultimafila + 6, 8) = cant_2
    Worksheets("Control semanal").Cells(ultimafila + 7, 8) = cant_3
    Worksheets("Control semanal").Cells(ultimafila + 5, 9) = pre_1
    Worksheets("Control semanal").Cells(ultimafila + 6, 9) = pre_2
    Worksheets("Control semanal").Cells(ultimafila + 7, 9) = pre_3

End Sub

我希望将其添加到表格中

1 个答案:

答案 0 :(得分:-1)

您可以选择在宏中使用组合键,方法是按住Control键并按方向键(如下例所示),您将位于该行或列中具有值的最后一个元素中(插入此后的值)

Sub InsertMacro()
    'select a row rank and copy'
    Range("A3:C3").Select
    Selection.Copy
    Sheets("weekly_control").Select
    'select the first element of the table header'
    'note: table created by the insert table method'
    Range("Tabla1[[#Headers],[NAME]]").Select
    'End(xlDown) = equivalent to Control and direction key (below)'
    'Offset(1, 0) = insert value one row below the last added value'
    Selection.End(xlDown).Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'return to previous page and clean range of copied cells'
    Sheets("form").Select
    Range("A3:C3").ClearContents
End Sub

可以通过录制创建宏来选择键的组合代码(此代码通过录制是optenido,然后更改为方便)