copying and pasting different

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Copying and pasting from a different workbook which name change

Copying and pasting from a different workbook which name change

resolvedResolved · Medium Priority · Version 2010

Copying and pasting from a different workbook which name change

Hi,

I need to copy some data from one workbook to another every month but the workbook and the name are different.

What I have is :

Workbooks.Open Filename:= _
"O:\Treasury\RiskCtrl\Risk Reporting\SRA\Trader Sign Off\Explainers 2016\4. April 2016 Explanations.xlsm"
Range("A2:G571").Select
Selection.Copy
Windows("Management Pack May.xlsm").Activate
ActiveWindow.ScrollColumn = 6
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save
Windows("5. May 2016 Explanations.xlsm").Activate
ActiveWindow.Close

I want my macro to be able to open the correct file for the month I need the information for, next month the file will be called 5.May2016 and so on

Thanks
Nuria

RE: Copying and pasting from a different workbook which name cha

Hi Nuria,

Thank you for the forum question.

You have different options to do what you want.

One option is to use the file dialog picker or you you can create an input box but you will need to work with variables. I do not know if you have any variable knowledge but it is a way of storing information in the computer's memory.

The first code below will ask you to select the file you want to copy from and when you have selected the file the range will be copied and pasted to the destination.

Try:

Sub AnyNameYouLike()


'speed up macro code
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

'declare variables
Dim FileName As String
Dim WorkBk As Workbook


With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show = True
FileName = .SelectedItems(1)
Else
MsgBox "File not selected!", , "File selecter"
Exit Sub
End If

End With
Set WorkBk = Workbooks.Open(FileName)

Range("A2:G571").Select
Selection.Copy
Windows("Management Pack May.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save

WorkBk.Close savechanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic

End Sub


Or you can create a an input where Excel will prompt you for a file name.

Sub AnyNameYouLike()


'speed up macro code
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

'declare variables
Dim FileName As String
Dim WorkBk As Workbook

FileName=InputBox("Please enter the file name")


Set WorkBk = Workbooks.Open("O:\Treasury\RiskCtrl\Risk Reporting\SRA\Trader Sign Off\Explainers 2016\" & FileName)

Range("A2:G571").Select
Selection.Copy
Windows("Management Pack May.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save

WorkBk.Close savechanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic

End Sub

Please test the code in copies of your workbooks and let me know if it is working.

If you want Excel automatically to know the name of the next workbook it is much more complicated.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Fri 20 May 2016: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Quickly Adding New Worksheets

Want to place a new Excel worksheet before current worksheet. Use keystroke SHIFT+F11

View all Excel hints and tips


Server loaded in 0.07 secs.