excel VBA令人难以置信的新手。我在许多不同的地方发现了这个问题,但似乎无法操纵我曾尝试过的代码。基本上,我有一个工作簿,可以创建一个新工作簿,完全复制一个工作表,保存工作簿,然后需要从原始工作簿传输第二个工作表。我已经完成了这件事:
Wb1.Sheets("Scorecard").Range("A1:M37").Value(11) = Wb.Sheets("Scorecard").Range("A1:M37").Value(11)
其中Wb1是"转移到"本书和Wb是"来自"工作簿。问题是这不是要隐藏的页眉/页脚,图片(徽标)或隐藏列。
我试过这个,但它似乎只是打开另一本书并冻结我的系统并打开第三本书:
Wb.Sheets("Scorecard").Copy
Wb1.Sheets("Scorecard").PasteSpecial xlPasteValues
我觉得我错过了一些非常明显的东西,但我已经厌倦了将头撞在墙上。这一点的完整代码如下:
Sub NewWb()
Dim Aname As String
Dim HospName As String
Dim DateDone As String
Dim xPath As String
Dim Wb As Workbook
Dim Wb1 As Workbook
Set Wb = ThisWorkbook
xPath = Application.ActiveWorkbook.Path
Aname = "CS Diversion Prevention Assessment"
HospName = Sheets("Hospital ID").Range("I8")
DateDone = Replace((Sheets("Hospital ID").Range("I13").Text), "/", "-")
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("Compliance Checklist").Copy
With ActiveSheet.UsedRange
.Value = .Value
End With
ActiveSheet.Shapes("Button 1").Delete
Sheets.Add.Name = "Scorecard"
ActiveWorkbook.SaveAs Filename:=xPath & "\" & HospName & "." & Aname & "." & DateDone & ".xlsx"
Set Wb1 = Workbooks.Open(xPath & "\" & HospName & "." & Aname & "." & DateDone & ".xlsx")
Wb.Activate
提前致谢
答案 0 :(得分:2)
这在Excel 2013上使用Worksheet.Copy
方法为我工作。我也尝试使用Worksheet.Move
方法进行移动,但移动工作表需要大约10秒钟(!)。
Factory<T>
编辑2 :复制后,要将所有公式转换为平面值,请尝试Ron de Bruin's MVP site提供:
Public Sub copy_it_buster()
Dim to_copy As Worksheet, copy_after As Worksheet
Set to_copy = Workbooks("Book2").Sheets(1) ' Or whatever worksheet you're moving from
Set copy_after = Workbooks("Book3").Sheets(1) ' Or whatever worksheet you want to copy after.
' Note that this is in a different workbook!
to_copy.Copy After:=copy_after
'to_copy.Move After:=copy_after ' if you don't want to keep it where
' it was originally
End Sub
修改对于我最初测试的dim sh as Worksheet
set sh = ' whatever your new worksheet is
With sh.UsedRange
.Value = .Value
End With
案例,当我开始时:
Move
我结束了:
Book2 Book3
Sheet1 Sheet1
对于no Book2! Book3
Sheet1 Sheet1 (2)
,修改,我得到:
Copy
注意,这需要目标工作簿已经存在。此外,如果您想在现有工作表之前复制/移动到而不是之后的,只需将Book2 Book3
Sheet1 Sheet1 Sheet1(2)
更改为After
{{1}调用。
和PS :为减少对此项目和未来项目的挫败感,请使用Before
和Copy
等名称,而不是FromWorkbook
和ToWorkbook
。它可能看起来不多,但根据我的经验,它可以在调试时显着减少心理负担!
答案 1 :(得分:0)
您是否考虑将源工作簿(Wb)保存/复制为新工作簿(Wb1),然后编辑新工作簿?
所以不要创建一个新的空白wb并添加到它...创建一个原始的副本,然后从中删除你需要的。