当前位置: 动力学知识库 > 问答 > 编程问答 >

excel - Copying and Pasting

问题描述:

I very recently got into VBA coding, however, even though the code compiles without any hiccups, it still fails to do it's intended purpose.

Essentially, this is merely a test program created to copy and paste information from one workbook to another in a specific cell.

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: