gpt4 book ai didi

vba - 复制粘贴 Excel VBA 代码说明

转载 作者:行者123 更新时间:2023-12-04 21:17:16 27 4
gpt4 key购买 nike

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

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

最佳答案

请注意,发布的代码会将特定范围从除最后一个以外的每个表复制到指定表

' 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

关于vba - 复制粘贴 Excel VBA 代码说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13836891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com