Error catching/ handling if file not open

Rip1971

Board Regular
Joined
Nov 3, 2020
Messages
65
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would like to put a error handling code taht does the following. in my code i have this line. this works well if the srcWS is already open. But when i do not have the file open i would like a msg telling me to open the file. or even better a (don't know how it is called) but a explore window so i can locate and open the file which is then used as srcWS.

Since the code is started from the active workbook is the order in which this is done important??

VBA Code:
Sub CopyDepartement(control As IRibbonControl)
   Application.ScreenUpdating = False
   Dim LastRow As Long, r As Long
   Dim srcWS As Worksheet, desWS As Worksheet
   Dim Ary As Variant, Nary As Variant
   Set srcWS = Workbooks("Bewonerslijst.xlsx").Sheets("Sheet1")
   Set desWS = ActiveWorkbook.Sheets("Data")

thanks in advance
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
VBA Code:
   Dim Wbk As Workbook
   
   On Error Resume Next
   Set Wbk = Workbooks("Bewonerslijst.xlsx")
   On Error GoTo 0
   If Wbk Is Nothing Then Set Wbk = Workbooks.Open("yourfilepath\Bewonerslijst.xlsx")
   Set srcWS = Wbk.Sheets("Sheet1")
change yourfilepath to match the location of the file.
 
Upvote 0
This works good. only had to change which workbook is set first desWS and srcWS because of active workbook is no longer the workbook that started the code.
However is still requires me to know the location upfront. How can I make i so that even if i do not know location I can open explorer select file and then use.

VBA Code:
Set desWS = ActiveWorkbook.Sheets("Data")
   
   On Error Resume Next
   Set Wbk = Workbooks("Bewonerslijst.xlsx")
   On Error GoTo 0
   If Wbk Is Nothing Then Set Wbk = Workbooks.Open("C:\Users\u123456\OneDrive\Desktop\Bewonerslijst.xlsx")
   Set srcWS = Wbk.Sheets("Sheet1")
 
Upvote 0
How about
VBA Code:
   Dim Fname As String
   
   On Error Resume Next
   Set Wbk = Workbooks("Bewonerslijst.xlsx")
   On Error GoTo 0
   If Wbk Is Nothing Then
      Fname = Application.GetOpenFilename
      Set Wbk = Workbooks.Open(Fname)
   End If
 
Upvote 0
How about
VBA Code:
   Dim Fname As String
 
   On Error Resume Next
   Set Wbk = Workbooks("Bewonerslijst.xlsx")
   On Error GoTo 0
   If Wbk Is Nothing Then
      Fname = Application.GetOpenFilename
      Set Wbk = Workbooks.Open(Fname)
   End If
This is what i was looking for for opening the file thank you. how do i know that the right sheet is selected within that file. What i have now is adding the line below which tells the sheet name.
Set srcWS = Wbk.Sheets("Sheet1")

But as you can imaging that ppl change this without knowing the impact of this change.
slowly working on making it an user friendly piece of code.
 
Last edited:
Upvote 0
You could use the sheets codename, but you would need to change the codename to something non-standard.
 
Upvote 0
This is what i was looking for for opening the file thank you. how do i know that the right sheet is selected within that file. What i have now is adding the line below which tells the sheet name.
Set srcWS = Wbk.Sheets("Sheet1")

But as you can imaging that ppl change this without knowing the impact of this change.
slowly working on making it an user friendly piece of code.
Is there also a line that prevents error when someone presses the cancel button instead of the oke button
 
Upvote 0
How about
VBA Code:
   If Wbk Is Nothing Then
      Fname = Application.GetOpenFilename
      If Fname = "False" Then Exit Sub
      Set Wbk = Workbooks.Open(Fname)
   End If
 
Upvote 0
Solution
Thanks for al the pieces of code. together they do what i want.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,041
Members
449,206
Latest member
Healthydogs

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