我正在编写一个程序,将数据从一个工作簿复制并粘贴到另一个工作簿。我想在一个不同的位置保存一个工作簿,关闭它,然后从文件中打开一个新的工作簿并执行相同的操作(我从多个工作簿复制数据并将这些数据粘贴到一个主工作簿中)。我还需要帮助反转标志(例如:我将从wbk1复制数字1,我需要在wbk2中粘贴-1)。请帮忙!我是VBA的新手,不明白为什么我的代码无法工作!
这是我的代码:
函数GetBook()As String GetBook = ActiveWorkbook.Name 结束功能
Sub Paste() Dim wbk As Workbook 昏暗的wbkH作为工作簿 Dim fso As Object Dim COID As String
设置wbk =工作簿(" 0_Master脚注操作租约2014年5月_LIVE_essbase") COID =" 6985" '用于在wbk中搜索的设施号
设置wbkH =工作簿(GetBook)
'减量 wbkH.Activate'选择医院文件 表格("添加和过期")。选择'选择ws
Columns("G:G").Select
Range("G:G").Activate
Selection.Find(What:="Total Lease", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select 'Selects entire row
With ActiveCell
Range(Cells(.Row, "H"), Cells(.Row, "H")).Select 'Select first total in column
Selection.Copy
'enter in hospitals COID
wbk.Activate
Sheets("Compare CY to PY").Select
Columns("C:C").Select
Range("C:C").Activate
Selection.Find(What:=COID, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select
With ActiveCell
Range(Cells(.Row, "J"), Cells(.Row, "J")).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False 'clears clipboard
End With 'I NEED TO CHANGE SIGN ON THIS POSTED VALUE (EX. 1 TO -1)
End With
wbkH.Activate 'Select Hospitals document
Sheets("Misc Reconciling Items").Select 'select ws
Columns("A:A").Select
Range("A:A").Activate
Selection.Find(What:="Annualized", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select 'Selects entire row
With ActiveCell
Range(Cells(.Row, "D"), Cells(.Row, "D")).Select 'Select first total in column
Selection.Copy
'enter in hospitals COID
wbk.Activate
Sheets("Compare CY to PY").Select
Columns("C:C").Select
Range("C:C").Activate
Selection.Find(What:=COID, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select
With ActiveCell
Range(Cells(.Row, "L"), Cells(.Row, "L")).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False 'clears clipboard
End With 'I NEED TO CHANGE SIGN ON THIS POSTED VALUE (EX. 1 TO -1)
End With
wbkH.Activate
ActiveWorkbook.SaveAs ("C:\Program Files\" & GetBook) 'THIS CODE WONT WORK AND I HAVE TRIED VARIOUS CODES.
答案 0 :(得分:1)
根据您的主题标题,似乎唯一的问题是只保存Excel工作簿(重新:行ActiveWorkbook.SaveAs ("C:\Program Files\" & GetBook) 'THIS CODE WONT WORK AND I HAVE TRIED VARIOUS CODES
)。如果这是正确的,那么几个代码片段可以帮助(C#):
例子1.关闭并保存
object misValue = System.Reflection.Missing.Value;
ActiveWorkbook.Close(true, filePath, misValue);
示例2(来自http://msdn.microsoft.com/en-us/library/h1e33e36.aspx)
this.SaveAs(@"C:\Book1.xml", missing,
missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
示例3(来自http://msdn.microsoft.com/en-us/library/h1e33e36.aspx)
this.Application.ActiveWorkbook.SaveAs(@"C:\Test\Book1.xml",
Excel.XlSaveAsAccessMode.xlNoChange);
此致