按钮事件Excel

时间:2017-03-14 11:47:06

标签: excel-vba vba excel

我的目的是将从行中获取的数据表的基础填充到压力(位于“BUTTON”的按钮)和I“No”的位置。自动增加。

到目前为止我使用了这段代码,但它没有复制到下一个空闲行。我怎么能这样做?

Sub Range_Copy()
    Range("D15:F15").Copy Range("D20:F20")
    Range("D15:F15").Value = ""
End Sub

My excel WorkSheet

2 个答案:

答案 0 :(得分:0)

这是你如何做到的。您只需要找到该列中最后使用的行。

我为你添加了一些评论,以了解为什么我改变了一些东西。我还使用了With Worksheets(...)来确保在特定工作表上进行所有更改。请参阅here about the with block及其使用方法。

YOUR-SHEET-NAME-HERE更改为工作表的名称。

Sub Range_Copy()
    Dim LastUsedRow As Long
    Dim InputRange As Range

    With Worksheets("YOUR-SHEET-NAME-HERE")
        LastUsedRow = .Cells(.Rows.Count, "D").End(xlUp).Row
            ' find last used (non empty) row in column D

        Set InputRange = .Range("D15:F15")
            ' set the input range to a variable so in case we
            ' need to change that range later there is only
            ' one position that we need to change

        InputRange.Copy
            ' copy the input range

        .Range("D" & LastUsedRow + 1).PasteSpecial xlPasteValues
            ' Paste in column D (LastUsedRow + 1 = next empty row)
            ' PasteSpecial xlPasteValues only pastes the values no format
            ' (so the input range can be formated differently)

        InputRange.ClearContents
            ' using .value = "" dosen't really clear the cell.
            ' It remains an empty string which can cause issues later
            ' Therefore we use .ClearContents
    End With
End Sub

答案 1 :(得分:0)

我找到了解决所有问题的方法:

复制值:

Sub Range_Copy()
    Dim x As Long
    x = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row + 1
    With Range("E15:G15")
        .Copy Destination:=Range("D" & x)
        .ClearContents
    End With
End Sub

复制工作表并重命名副本:

Option Explicit
Sub NewSheets()
    Dim i As Integer
    Dim ws As Worksheet
    Dim sh As Worksheet
    Set ws = Sheets("Template")
    Set sh = Sheets("Dati")
    Application.ScreenUpdating = 0

    For i = 5 To Range("K" & Rows.Count).End(xlUp).Row
        If sh.Range("K" & i).Value <> "" Then
            Sheets("Template").Copy After:=sh
            ActiveSheet.Name = sh.Range("K" & i).Value
            ActiveSheet.Range("E11").Value = sh.Range("L" & i).Value
        End If
        If i = 23 Then Exit For
    Next i

End Sub