我有以下子程序。我是VBA的新手,所以我认为它效率低下且笨拙。本质上,我试图调用另一个执行某些操作的子例程(UpdateBlocksSingle),然后我想对结果进行截图,然后将其粘贴到名为“ Screenshots”的新扩音器中,然后将结果导出为pdf。所有这些都是循环完成的,因此我生成了很多屏幕截图(以及pdf中的很多页面)。我还将开始将屏幕截图带出循环的过程。
继续产生1004错误的片段是:
CopyRangeSingle.CopyPicture xlScreen, xlPicture
Screen.Paste Destination:=PasteRange.Offset(56 * i, 1)
Sheets("Screenshots").Rows(56 * i).PageBreak = xlPageBreakManual
该错误将随机发生,即有时在循环中进行10次迭代之后,有时又是在1或2之后。其他情况下,当我将外观类型从xlScreen更改为xlPrinter时,我将不再收到该错误。安迪关于如何更改代码的建议,使我可以成功地循环浏览并截取屏幕截图,同时保持xlScreen的分辨率。
Sub LoopWaterProviders()
'If a former Screenshot tab exits delete it
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name = "Screenshots" Then
Application.DisplayAlerts = False
Sheets("Screenshots").Delete
Application.DisplayAlerts = True
End If
Next
Call CreateSheet
Sheets("BlockChart").Select
Dim WaterProviders As Long
Dim OpenPDF As Boolean
Dim ProviderType As Long
Dim j As Long
Dim i As Long
Dim OutSht As Worksheet
Dim CopyRangeSingle As Range
Dim CopyRangeBlock As Range
Dim PasteRange As Range
Dim Block As Worksheet
Dim Screen As Worksheet
j = 1
ProviderType = Sheets("Multiple Block Charts").Range("A1").Value
OpenPDF = Sheets("Multiple Block Charts").Range("A2").Value
Set Screen = Sheets("Screenshots")
Set Block = Sheets("BlockChart")
Set CopyRangeSingle = Block.Range("S1:AJ50")
Set CopyRangeBlock = Block.Range("A1:P43")
Set PasteRange = Screen.Cells(1, 1)
If ProviderType = 1 Then
WaterProviders = Sheets("Calculations_Single").Range("C11").Value
ElseIf ProviderType = 2 Then
WaterProviders = Sheets("Multiple Block Charts").Range("F13").Value
ElseIf ProviderType = 3 Then
WaterProviders = Sheets("Multiple Block Charts").Range("H15").Value
ElseIf ProviderType = 4 Then
WaterProviders = Sheets("Multiple Block Charts").Range("K61").Value
Else: WaterProviders = Sheets("Calculations_Single").Range("C11").Value
End If
Application.DisplayStatusBar = True
CopyRangeBlock.CopyPicture xlPrinter, xlPicture
Screen.Paste Destination:=PasteRange
Sheets("Screenshots").Rows(56).PageBreak = xlPageBreakManual
Application.CutCopyMode = False
Worksheets("BlockChart").Select
For i = 1 To WaterProviders
If ProviderType = 1 Then
Sheets("Calculations_Single").Range("C12").Value = i
ElseIf ProviderType = 2 Then
Sheets("Calculations_Single").Range("C12").Value = Sheets("Multiple Block Charts").Cells(i + 1, 6)
ElseIf ProviderType = 3 Then
Sheets("Calculations_Single").Range("C12").Value = Sheets("Multiple Block Charts").Cells(i + 1, 9)
ElseIf ProviderType = 4 Then
Sheets("Calculations_Single").Range("C12").Value = Sheets("Multiple Block Charts").Cells(i + 1, 12)
Else: Sheets("Calculations_Single").Range("C12").Value = i
End If
Call UpdateBlocksSingle
CopyRangeSingle.CopyPicture xlScreen, xlPicture
Screen.Paste Destination:=PasteRange.Offset(56 * i, 1)
Sheets("Screenshots").Rows(56 * i).PageBreak = xlPageBreakManual
Next i
Sheets("Screenshots").Range("A:S").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="G:\P_and_R\DATA_TOOLS\Shortage Impacts\Single_Year_BLOCK_CHART\Provider_and_CAP_Supply_Block_AC_ver1.X_Output\ProviderOutputCharts", _
OpenAfterPublish:=OpenPDF, _
Quality:=xlQualityHighest, _
From:=1, _
To:=WaterProviders + 1
MsgBox "Export complete and PDF saved to 'G:\P_and_R\DATA_TOOLS\Shortage Impacts\Single_Year_BLOCK_CHART\Provider_and_CAP_Supply_Block_AC_ver1.X_Output' "
Sheets("BlockChart").Select
End Sub