我正在创建复制/粘贴vba代码,以将我的财务模型转移到单独的excel模型中的粘贴版本,并且无法弄清楚如何仅粘贴打印区域中显示的内容。当前,它工作正常,但是它将所有数据粘贴到打印范围之外,并显示隐藏在行/列中但未显示的所有数据。
我似乎找不到要添加到已经工作的代码中的函数或简单函数。我是VBA的新手,不确定添加此功能的最佳方法。
Sub Test()
Dim wb As Workbook, wbPaste As Workbook, wsExhibit1 As Worksheet, wsPaste As Worksheet, wsInputs As Worksheet, _
wsExhibit2 As Worksheet
With Application
.ScreenUpdating = False
.Calculation = xlCalculationAutomatic
.EnableEvents = False
End With
Set wb = ThisWorkbook
Set wbPaste = Workbooks("Copy_PasteWorkbook.xlsx")
With wb
Set wsExhibit1 = .Sheets("Value_Summary")
Set wsInputs = .Sheets("Inputs")
Set wsExhibit2 = .Sheets("Calculations")
End With
With wbPaste
Set wsPaste = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
With wsPaste
wsExhibitA1.UsedRange.Copy
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.Range("A1").PasteSpecial xlPasteColumnWidths
.Name = .Cells(1, 3)
End With
wsInputs.Range("Selected_Calculation").Value = 1
Do Until wsInputs.Range("Selected_Calculation").Value > wsInputs.Range("Total_Calculations").Value
Application.Calculate
With wbPaste
Set wsTemp = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
With wsTemp
wsExhibit2.UsedRange.Copy
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.Range("A1").PasteSpecial xlPasteColumnWidths
.Name = .Cells(1, 3)
End With
wsInputs.Range("Selected_Calculation").Value = wsInputs.Range("Selected_Calculation").Value + 1
DoEvents
Loop
wsInputs.Range("Selected_Calculation").Value = 1
With Application
.ScreenUpdating = False
.Calculation = xlCalculationAutomatic
.EnableEvents = False
.CutCopyMode = False
End With
End Sub
我希望仅将工作簿中设置在打印区域内的内容粘贴到各个页面上。不粘贴隐藏数据或超出范围的数据。
答案 0 :(得分:1)
Print_Area
是一个命名范围。
Sheets(“SheetName”).Range(“Print_Area”).Copy
为避免隐藏单元格:
Sheets(“SheetName”).Range(“Print_Area”).SpecialCells(xlCellTypeVisible).Copy