下面的宏打开一个外部工作簿,取消隐藏特定工作表并将几个文本值读入变量,根据这些变量值在当前工作簿中创建一个新工作表,复制该工作簿中工作表的内容,然后粘贴到新的。
但是,当我使用.Paste
时,它工作正常,但不会保留格式,只会在文本中粘贴。
如果我尝试纠正此问题并使用
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
失败了。
为什么会这样?
Sub addsheet()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
filename = Sheets("Instructions").Range("C13").Value
report = "report.xlsx"
report_filepath = "\\xxx\xxx\xxx\report.xlsx"
Dim report_name As String
Workbooks.Open Filename:=(report_filepath)
Windows(report).Activate
Sheets("Info").Visible = True
Sheets("Info").Activate
report_month = Range("B5").Text
report_year = Range("B4").Text
Sheets("Report").Range("A1:AJ498").Copy
Windows(filename).Activate
Windows(report).Close
Set newsheet = Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet)
report_name = (report_month & " " & report_year)
newsheet.Name = (report_name)
Sheets("Instructions").Range("C15").Value = (report_name)
Sheets(report_name).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Instructions").Activate
End Sub
答案 0 :(得分:1)
就像我在评论中提到的那样,你应该避免使用.Select/.Activate
。您可能希望看到THIS
如果我更改您的代码并使用对象,那么您的代码可能如下所示。
在粘贴之前移动复制代码并在其间包含DoEvents
,以便Excel有足够的时间将数据放到剪贴板上。
试试这个
<强> UNTESTED 强>
Sub addsheet()
Dim report_name As String
Dim Filename As String, report_filepath As String, report As String
Dim thisWb As Workbook, thatWb As Workbook
Dim thatWs As Worksheet, NewSheet As Worksheet
Dim report_month As String, report_year As String, report_name As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set thisWb = ThisWorkbook
With thisWb
Set NewSheet = .Sheets.Add(After:=.Sheets(.Worksheets.Count), Count:=1, Type:=xlWorksheet)
End With
report_filepath = "\\xxx\xxx\xxx\report.xlsx"
Set thatWb = Workbooks.Open(report_filepath)
Set thatWs = thatWb.Sheets("Info")
With thatWs
report_month = .Range("B5").Value
report_year = .Range("B4").Value
End With
report_name = report_month & " " & report_year
NewSheet.Name = report_name
thisWb.Sheets("Instructions").Range("C15").Value = report_name
thatWb.Sheets("Report").Range("A1:AJ498").Copy
DoEvents
NewSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
NewSheet.Range("A1").PasteSpecial Paste:=xlPasteFormats, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
thisWb.Sheets("Instructions").Activate
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
答案 1 :(得分:0)
根据Office帮助文件,当您使用Worksheet.PasteSpecial
方法&#34;您必须在使用此方法之前选择目标范围。&#34; 。
如果你改变了
Sheets(report_name).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
到
Sheets(report_name).Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
然后它会起作用。
.Paste
方法似乎默认为&#34; A1&#34;如果没有指定范围。
进一步调查显示,如果您使用.PasteSpecial
而没有任何参数,即Sheets(report_name).PasteSpecial
,那么它将在不指定范围的情况下工作。但是,如果包含参数,则表明您必须指定范围