将表格的值插入最后一行

时间:2018-09-30 02:19:46

标签: excel vba

我希望我的表单将其值添加到表中最后使用的行正下方的新行中。这就是我得到的:

Dim sh As Worksheet, lastrow As Long

    If MiemCombx.Value = "General" Then
    Sh = Sheets("General")
    lastrow = sh.UsedRange.Row - 1 + sh.UsedRange.Rows.Count

        Sh.Cells(lastrow, 1) = Nume
        Sh.Cells(lastrow, 2) = Date
        Sh.Cells(lastrow, 3) = MiemCombx
        Sh.Cells(lastrow, 4) = ClasCombx
        Sh.Cells(lastrow, 5) = ConcTxt
        Sh.Cells(lastrow, 6) = JustCombx
        Sh.Cells(lastrow, 7) = Impo

    End If

1 个答案:

答案 0 :(得分:0)

有很多方法可以执行此操作,这是基于A列中值的最后出现的方法:

Dim sh as Worksheet
Dim c as Range

Set sh = sheets("General")
Set c = sh.Range("A1048576").End(xlUp)

c.Offset(1, 1) = Nume
c.Offset(1, 2) = Date
c.Offset(1, 3) = MiemCombx
c.Offset(1, 5) = ConcTxt
c.Offset(1, 6) = JustCombx
c.Offset(1, 7) = Impo

End Sub

或者,如果您更喜欢使用“ lastrow”,那么

Dim sh as Worksheet
Dim lastrow as Long

Set sh = sheets("General")
Set lastrow = sh.Range("A1048576").End(xlUp).Row + 1

sh.Cells(lastrow , 1) = Nume
sh.Cells(lastrow , 2) = Date
sh.Cells(lastrow , 3) = MiemCombx
sh.Cells(lastrow , 5) = ConcTxt
sh.Cells(lastrow , 6) = JustCombx
sh.Cells(lastrow , 7) = Impo

End Sub

希望一个对你有用。