如何在该宏中添加循环以重复“x”次

问题描述 投票:0回答:1

我正在尝试将多个宏合并为一个。如何添加一个循环,使其重复“x”次,其中“x”是 Sheet3!A1 的值,然后执行打印预览功能?

这是我到目前为止的VBA代码。我似乎无法让循环正常工作。我正在尝试弄清楚我需要添加什么到“我无法弄清楚要放在这里的内容”

Sub Calculate()
    ' Macro to copy data to a new row, run it a specified number of times, and print output.
    
    ' Turn off screen updating.
    Application.ScreenUpdating = False
    
    ' Declarations.
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet
    Dim targetRange As Range
    Dim loopCount As Long
    Dim i As Long
    Dim printSheet As Worksheet
    
     ' Set variables.
    Set copySheet = Worksheets("Sheet5")
    Set pasteSheet = Worksheets("Sheet6")
    
    ' Unlock pasteSheet to allow editing.
    pasteSheet.Unprotect Password:="password"
    
    ' Set targetRange as the last cell in column C with a value.
    Set targetRange = pasteSheet.Cells(pasteSheet.Rows.Count, 3).End(xlUp).Offset(1, 0)
    
    ' Set targetRange as the first cell in column C without conditional formatting
    ' under the last cell in column C with no value.
    Do Until targetRange.FormatConditions.Count = 0
        Set targetRange = targetRange.Offset(1, 0)
    Loop
    
    ' Copy range C22:M22.
    copySheet.Range("C22:M22").Copy
    
    ' Paste the copied range into targetRange.
    targetRange.PasteSpecial xlPasteAll
    
    ' Lock pasteSheet to prevent further editing.
    pasteSheet.Protect Password:="password"
    
    ' Get the loop count from Sheet3!A1
    loopCount = Sheets("Sheet3").Range("A1").Value
    
    ' Loop from 1 to the specified loop count
    For i = 1 To loopCount

   '          # **"I CANT WORK OUT WHAT TO PUT HERE"**

    Next i
    
    ' Unprotect and unhide printSheet.
    printSheet.Visible = xlSheetVisible
    printSheet.Unprotect Password:="password"
    
    ' Open print preview for the specified range.
    printSheet.Range("B1:N46").PrintPreview
    
    ' Hide and protect printSheet again.
    printSheet.Protect Password:="password"
    printSheet.Visible = xlSheetHidden
    
    ' Turn off the cut-copy mode.
    Application.CutCopyMode = False
    
    ' Turn on screen updating.
    Application.ScreenUpdating = True
End Sub

我尝试将其复制为我想要重复的任务“我无法弄清楚要放在这里的内容”

' Set variables.
    Set copySheet = Worksheets("Sheet5")
    Set pasteSheet = Worksheets("Sheet6")
    
    ' Unlock pasteSheet to allow editing.
    pasteSheet.Unprotect Password:="password"
    
    ' Set targetRange as the last cell in column C with a value.
    Set targetRange = pasteSheet.Cells(pasteSheet.Rows.Count, 3).End(xlUp).Offset(1, 0)
    
    ' Set targetRange as the first cell in column C without conditional formatting
    ' under the last cell in column C with no value.
    Do Until targetRange.FormatConditions.Count = 0
        Set targetRange = targetRange.Offset(1, 0)
    Loop
    
    ' Copy range C22:M22.
    copySheet.Range("C22:M22").Copy
    
    ' Paste the copied range into targetRange.
    targetRange.PasteSpecial xlPasteAll
    
    ' Lock pasteSheet to prevent further editing.
    pasteSheet.Protect Password:="password"
excel vba loops
1个回答
0
投票

我设法解决了我的问题。我试图适应旧的宏并误解了循环函数。

我从上面的代码中删除了以下内容:

' Set targetRange as the last cell in column C with a value.
Set targetRange = pasteSheet.Cells(pasteSheet.Rows.Count, 3).End(xlUp).Offset(1, 0)

' Set targetRange as the first cell in column C without conditional formatting
' under the last cell in column C with no value.
Do Until targetRange.FormatConditions.Count = 0
    Set targetRange = targetRange.Offset(1, 0)
Loop

' Copy range C22:M22.
copySheet.Range("C22:M22").Copy

' Paste the copied range into targetRange.
targetRange.PasteSpecial xlPasteAll

' Lock pasteSheet to prevent further editing.
pasteSheet.Protect Password:="password"

我已将循环修改为:

' Get the loop count from Sheet3!A1
loopCount = Sheets("Sheet3").Range("A1").Value

' Loop from 1 to the specified loop count
For i = 1 To loopCount

    ' Set targetRange as the last cell in column C with a value.
    Set targetRange = pasteSheet.Cells(pasteSheet.Rows.Count, 3).End(xlUp).Offset(1, 0)

    ' Copy range C22:M22.
    copySheet.Range("C22:M22").Copy

    ' Paste the copied range into targetRange.
    targetRange.PasteSpecial xlPasteAll

Next i

上面现在删除了冗余代码,并允许宏运行“X”次,其中“X”是 Sheet3A1 的值

© www.soinside.com 2019 - 2024. All rights reserved.