• 示例代码将复制到名称为Sheet2的数据库表中。

  • 每次您运行一个子程序时,单元格都会被放置在最后一行包含数据的下方,或者放在最后一列之后的sheet2中的数据之后。

  • 对于每个示例,都有一个宏进行普通复制,而宏仅复制值。

  • Example子代码使用以下功能(如果没有这些功能,宏将无法使用)。

Sub CopyRow()

Dim sourceRange As Range

Dim destrange As Range

Dim Lr As Long

Lr = LastRow(Sheets("Sheet2")) + 1

Set sourceRange = Sheets("Sheet1").Rows("1:1")

Set destrange = Sheets("Sheet2").Rows(Lr)

sourceRange.Copy destrange

End Sub

Sub CopyRowValues()

Dim sourceRange As Range

Dim destrange As Range

Dim Lr As Long

Lr = LastRow(Sheets("Sheet2")) + 1

Set sourceRange = Sheets("Sheet1").Rows("1:1")

Set destrange = Sheets("Sheet2").Rows(Lr). _

Resize(sourceRange.Rows.Count)

destrange.Value = sourceRange.Value

End Sub

Function LastRow(sh As Worksheet)

On Error Resume Next

LastRow = sh.Cells.Find(What:="*", _

After:=sh.Range("A1"), _

Lookat:=xlPart, _

LookIn:=xlFormulas, _

SearchOrder:=xlByRows, _

SearchDirection:=xlPrevious, _

MatchCase:=False).Row

On Error GoTo 0

End Function

Function Lastcol(sh As Worksheet)

On Error Resume Next

Lastcol = sh.Cells.Find(What:="*", _

After:=sh.Range("A1"), _

Lookat:=xlPart, _

LookIn:=xlFormulas, _

SearchOrder:=xlByColumns, _

SearchDirection:=xlPrevious, _

MatchCase:=False).Column

On Error GoTo 0

End Function