复制粘贴Excel VBA代码的说明

复制粘贴Excel VBA代码的说明,vba,excel,Vba,Excel,这是我在互联网上找到的代码,但我不想使用它,因为我不知道它是如何工作的,所以想问问是否有人能为我解释一下 Sub CopyPaste() Dim sh As Worksheet, Src As Range, Dst As Range For Each sh In Application.Worksheets If sh.Index <> Worksheets.Count Then Set Src = sh.Range("A1:L34") Set

这是我在互联网上找到的代码,但我不想使用它,因为我不知道它是如何工作的,所以想问问是否有人能为我解释一下

Sub CopyPaste()
Dim sh As Worksheet, Src As Range, Dst As Range
For Each sh In Application.Worksheets
    If sh.Index <> Worksheets.Count Then
        Set Src = sh.Range("A1:L34")
        Set Dst = Worksheets(3).Range("A500").End(xlUp).Offset(1, 0).Resize(Src.Rows.Count, Src.Columns.Count)
        Dst.Value = Src.Value
    End If
Next sh
End Sub
子复制粘贴()
尺寸sh作为工作表,Src作为范围,Dst作为范围
对于应用程序中的每个sh。工作表
如果sh.索引工作表,则计数
设置Src=sh.Range(“A1:L34”)
设置Dst=工作表(3).范围(“A500”).结束(xlUp).偏移量(1,0).调整大小(Src.Rows.Count,Src.Columns.Count)
Dst.Value=Src.Value
如果结束
下一个sh
端接头

使用此代码所做的只是将目标范围的值设置为源范围的值。您没有按照单词的正常含义“复制”单元格,因为您不会像正常复制和粘贴(即通过执行Crtl+C和Crtl+V)那样保留源单元格的任何格式

然而,这种方法有它的优点,因为它比编码拷贝和粘贴要快得多,所以如果它只是你想要的值,它会更有效

最后,您还可以使用类似的方法将范围“复制”到可以处理的变量。例如,在示例中使用预定义的范围:

Dim vVar as Variant

vVar = Src.value

for ii = lBound(vVar, 1) to uBound(vVar, 1)
    for jj = lBound(vVar, 2) to uBound(vVar, 2)
        vVar(ii, jj) = vVar(ii, jj) + 1
    next jj
next ii

Dst.Value = vVar

使用此代码所做的只是将目标范围的值设置为源范围的值。您没有按照单词的正常含义“复制”单元格,因为您不会像正常复制和粘贴(即通过执行Crtl+C和Crtl+V)那样保留源单元格的任何格式

然而,这种方法有它的优点,因为它比编码拷贝和粘贴要快得多,所以如果它只是你想要的值,它会更有效

最后,您还可以使用类似的方法将范围“复制”到可以处理的变量。例如,在示例中使用预定义的范围:

Dim vVar as Variant

vVar = Src.value

for ii = lBound(vVar, 1) to uBound(vVar, 1)
    for jj = lBound(vVar, 2) to uBound(vVar, 2)
        vVar(ii, jj) = vVar(ii, jj) + 1
    next jj
next ii

Dst.Value = vVar

请注意,张贴的代码将从每张图纸复制特定范围,然后从最后一张复制到指定的图纸

' Basically it is copying the VALUE (There are other things to copy, e.g. format, color)
' from the Source Range from all worksheet(s) to the Destination Range on another worksheet
Sub CopyPaste() 'The beginning of subroutine, you cannot return value in SubRoutine, and you can't call subroutine directly from Excel worksheet formula
Dim sh As Worksheet, Src As Range, Dst As Range 'declaring each variable
'You can look up each object in Object Explorer by F2 in VB Editor
For Each sh In Application.Worksheets 'iterate though each worksheet in all workbooks
    If sh.Index <> Worksheets.Count Then 'if this sheet isn't the last sheet
        Set Src = sh.Range("A1:L34") 'set Src variable to the src range
        Set Dst = Worksheets(3).Range("A500").End(xlUp).Offset(1, 0).Resize(Src.Rows.Count, Src.Columns.Count)
        'Worksheets(3) <-- a specific target worksheet
        '.Range("A500").End(xlUp).Offset(1, 0) <-- trying to find the last empty cell bottom up from cell A500
        '.Resize(Src.Rows.Count, Src.Columns.Count) <-- Resizing the range so that it fits with src range
        Dst.Value = Src.Value   'Assign the value of all cells in the dst range from src range
    End If
Next sh
End Sub ' The end of subroutine
'基本上是复制值(还有其他东西需要复制,例如格式、颜色)
'从所有工作表的源范围到另一工作表的目标范围
Sub CopyPaste()'子例程的开头,不能在子例程中返回值,也不能直接从Excel工作表公式调用子例程
Dim sh作为工作表,Src作为范围,Dst作为范围“声明每个变量
'您可以通过VB编辑器中的F2在对象资源管理器中查找每个对象
对于应用程序中的每个sh。工作表在所有工作簿中迭代每个工作表
如果sh.Index Worksheets.Count,则“如果此工作表不是最后一张工作表”
设置Src=sh.Range(“A1:L34”)'将Src变量设置为Src范围
设置Dst=工作表(3).范围(“A500”).结束(xlUp).偏移量(1,0).调整大小(Src.Rows.Count,Src.Columns.Count)

'工作表(3)请注意,发布的代码将从每张工作表复制特定范围,然后从最后一张复制到指定的工作表

' Basically it is copying the VALUE (There are other things to copy, e.g. format, color)
' from the Source Range from all worksheet(s) to the Destination Range on another worksheet
Sub CopyPaste() 'The beginning of subroutine, you cannot return value in SubRoutine, and you can't call subroutine directly from Excel worksheet formula
Dim sh As Worksheet, Src As Range, Dst As Range 'declaring each variable
'You can look up each object in Object Explorer by F2 in VB Editor
For Each sh In Application.Worksheets 'iterate though each worksheet in all workbooks
    If sh.Index <> Worksheets.Count Then 'if this sheet isn't the last sheet
        Set Src = sh.Range("A1:L34") 'set Src variable to the src range
        Set Dst = Worksheets(3).Range("A500").End(xlUp).Offset(1, 0).Resize(Src.Rows.Count, Src.Columns.Count)
        'Worksheets(3) <-- a specific target worksheet
        '.Range("A500").End(xlUp).Offset(1, 0) <-- trying to find the last empty cell bottom up from cell A500
        '.Resize(Src.Rows.Count, Src.Columns.Count) <-- Resizing the range so that it fits with src range
        Dst.Value = Src.Value   'Assign the value of all cells in the dst range from src range
    End If
Next sh
End Sub ' The end of subroutine
'基本上是复制值(还有其他东西需要复制,例如格式、颜色)
'从所有工作表的源范围到另一工作表的目标范围
Sub CopyPaste()'子例程的开头,不能在子例程中返回值,也不能直接从Excel工作表公式调用子例程
Dim sh作为工作表,Src作为范围,Dst作为范围“声明每个变量
'您可以通过VB编辑器中的F2在对象资源管理器中查找每个对象
对于应用程序中的每个sh。工作表在所有工作簿中迭代每个工作表
如果sh.Index Worksheets.Count,则“如果此工作表不是最后一张工作表”
设置Src=sh.Range(“A1:L34”)'将Src变量设置为Src范围
设置Dst=工作表(3).范围(“A500”).结束(xlUp).偏移量(1,0).调整大小(Src.Rows.Count,Src.Columns.Count)

'带有此代码的工作表(3)可以将A1:L34范围从所有工作表复制到第三个工作表(不复制的最后一个工作表除外)

最重要的部分是这个:

Set Dst = Worksheets(3).Range("A500").End(xlUp).Offset(1, 0).Resize(Src.Rows.Count, Src.Columns.Count)

此处设置了目的地范围。对于每个复制的图纸,目标范围为偏移量,因此粘贴后,复制的数据不会重叠

使用此代码,u可以将A1:L34范围从所有图纸复制到第三张图纸(不复制的最后一张图纸除外)

最重要的部分是这个:

Set Dst = Worksheets(3).Range("A500").End(xlUp).Offset(1, 0).Resize(Src.Rows.Count, Src.Columns.Count)

此处设置了目的地范围。对于每个复制的图纸,目标范围为偏移量,因此粘贴后,复制的数据不会重叠

我想知道在哪里可以找到学习循环编码的材料,我很困惑。这似乎很有用,我想知道在哪里可以找到学习循环编码的材料,我很困惑。这似乎很有用