gpt4 book ai didi

excel - 使用宏复制每 N 行

转载 作者:行者123 更新时间:2023-12-04 22:24:41 25 4
gpt4 key购买 nike

我正在使用 Excel VBA 创建一个宏以从 P7 开始向下复制每隔一行。我希望将这些复制的值作为连续列正常粘贴到另一个工作簿中。我很确定这将需要一个 for 循环,但我不确定如何在 VBA 中执行此操作。下面是我当前的代码,它只是复制填充的行而不跳过。

Option Explicit
Sub copyRange()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim i As Integer
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim strExtension As String
Dim LastRowC As Long
Dim LastRowP As Long
Dim filterRange As Range
Dim copyRange As Range

Const strPath As String = "C:\Users\User1\Desktop\UPLOADS2\"
ChDir strPath
strExtension = Dir(strPath & "*.xls*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource.Sheets("Sheet1")
LastRowC = wkbSource.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
'LastRowP = wkbDest.Worksheets("WIP").Cells(wkbDest.Worksheets("WIP").Rows.Count, "P").End(xlUp).Offset(1).Row
wkbSource.Worksheets("Sheet1").Range("B4:B" & LastRowC).Copy
wkbDest.Worksheets("WIP").Range("P7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
End With
wkbSource.Close savechanges:=False
strExtension = Dir
Loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

最佳答案

您可以管理stepfor循环修改通过next i迭代的数字,这样:

Dim i as Long
For i = 4 to LastRowC Step 2
'Use Cells(i,"B") or Range("B" & i)
Next i

在这种情况下,步骤 2 将使您从 4 到 6 到 8 等等。

关于excel - 使用宏复制每 N 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59035921/

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