Close window(abc20231231.xls) using vba

chingg1011

New Member
Joined
Oct 8, 2021
Messages
12
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
hi vba expert,

I m setting a macro to copy data from one excel to my workbook excel, but it involved in more than one source file (abc 20231231.xls, xyz20231231.xls...etc)

when I done on 1st file (abc.xls), I would close it, and open another file (xyz.xls) to copy another data to my workbook.

the code is showing a problem below

window(abc20311231.xls).close

my problem is the data file name is variable every month (abc20240131.xls), how to write the code to avoid running marco failure ? address the specific window file to close

Dennis
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
To help you we'll need see your actual code. The code you showed is not valid, so it won't even run--but maybe that's the problem. Also you should close the file, not the window.

Please show all your code and let's start there. It would be very helpful if you use code tags. Select the code then click the "VBA" button in the edit controls.
 
Upvote 0
hi expert,

I attached my problem

how to activate current work book being activated as the work file name is changed every month. how to write the code

the workbook file name is "macro_dm_yyyymmdd
variable factor on the date
=====================================================================

Sub Loan_data()
'
' Loan_data Macro
'
Dim myworkbook As String
myworkbook = InputBox("Enter file name:", "Marco_dm_20231231.xlsm")
If myworkbook <> "" Then
Range("F23") = myworkbook
Application.ScreenUpdating = False

'
Range("C23").Select
Selection.Copy
Workbooks.Open Filename:=Range("c23").Text

Range("A2:AX2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Windows.Application("myworkbook").Activate
Sheets("Loan 1").Select
Range("A2").Select
ActiveSheet.Paste
Windows("LOAN Table 2024-01-03T1650.csv").Activate
ActiveWindow.Close
Sheets("Sheet1").Select
Range("C25").Select
Selection.Copy
Workbooks.Open Filename:=Range("C25").Text

Range("A2:P2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Marco_dm.xlsm").Activate
Sheets("Loan 2").Select
Range("A2").Select
ActiveSheet.Paste
Windows("Loan Repayment Schedule - Live 2024-01-03T1652.csv").Activate
ActiveWindow.Close
Sheets("Sheet1").Select
ActiveWorkbook.Save
End If

1706760159000.png


thx
 
Upvote 0
Use a workbook variable to hold a reference to the workbook opened:

VBA Code:
dim SourceBook as workbook
set sourcebook = Workbooks.Open(Filename:=Range("c23").Text)

then when you are finished with it, close it using the variable:

VBA Code:
sourcebook.close
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top