I am trying to copy and pasting some values from a worksheet which will be changing name according to different dates. Ideally, the code should open it, activate it and copy a table from a sheet called "Geographic bond distribution" and then pasting it to the current workbook.
When debugging the code, the last line returns a Run-time error '9' as probably I am messing up something when opening / calling the workbook. In fact, if I manually open the file the code works .
Many thanks!
Sub GEO_Newton()
Dim fileName As String
fileName = Dir(ThisWorkbook.Path & "bnymgf_sustainable_global_dynamic_bond_summary*.*")
If fileName <> "" Then
Workbooks.Open fileName:=ThisWorkbook.Path & "\Underlying\" & fileName
End If
'Declare iteration object variable
Dim iWorkbook As Workbook
'Loop through all open workbooks
For Each iWorkbook In Application.Workbooks
'Do the following:
'(1) Test whether the first/leftmost 9 letters of the name of the workbook (the loop is currently iterating through) spell "Excel VBA"
'(2) If the condition is met, activate the workbook (the loop is currently iterating through)
If Left(iWorkbook.Name, 18) = "bnymgf_sustainable" Then iWorkbook.Activate
Worksheets("Geographic bond distribution").Activate