复制和粘贴

时间:2016-01-06 03:24:18

标签: excel vba excel-vba module

我最近进入了VBA编码,但是,即使代码编译没有任何打嗝,它仍然无法实现它的预期目的。 从本质上讲,这仅仅是一个测试程序,用于将信息从一个工作簿复制并粘贴到特定单元格中的另一个工作簿。

Sub CopyOpenItems()

   ' CopyOpenItems Macro  
   ' Copy open items to sheet.  
   ' Keyboard Shortcut: Ctrl+Shift+O  
   '  
   Dim wbTarget As Workbook 'workbook where the data is to be pasted  
   Dim wbThis As Workbook 'workbook from where the data is to copied  
   Dim wsTarget As Worksheet  
   Dim wsThis As Worksheet  
   Dim strName As String   'name of the source sheet/ target workbook  

   'set to the current active workbook (the source book)  
   Set wbThis = ActiveWorkbook  

   'get the active sheetname of the book 
   strName = ActiveSheet.Name  

   'open target workbook  
   Set wbTarget = Workbooks.Open("C:\Users\Administrator\Desktop\Excel Testing\Excel Info Testing 2.xlsx")  

   'set variable to current worksheet  
   Set wsThis = ActiveSheet  

   'set variable to target worksheet 
   Set wsTarget = wbTarget.Worksheets(strName)  

   wsThis.Range("C6").Select  

   'clear any thing on clipboard to maximize available memory 
   Application.CutCopyMode = False  

   'copy the range from source book  
   wsThis.Range("C6").Copy  

   'paste the data on the target book  
   wsTarget.Range("E5").PasteSpecial  

   'clear any thing on clipboard to maximize available memory  
   Application.CutCopyMode = False  

   'save the target book  
   wbTarget.Save  

   'close the workbook  
   wbTarget.Close  

   'clear memory  
   Set wbTarget = Nothing  
   Set wbThis = Nothing  

End Sub

0 个答案:

没有答案