VBA to open files to import into calculator

Marit88

New Member
Joined
Mar 8, 2019
Messages
5
Hello all, my first time here and beginning VBA user. I designed a calculator which compiles and interpretes the results of 9 different sets of output from image analysis software. I always name the 8 output files identical: 1 to 9 (excel workbooks). To start the calculator I use a messagebox which allows me to navigate to the particular location from which I would like to open the 9 workbooks (see below code). What currently happens is that if I don't select any files I exit the sub. What I would like is for the sub to end if I don't select 9 files (<9). As I mentioned I am a beginner but can't find the solution anywhere. Much thanks.

Dim fd As FileDialog
Dim file_was_chosen As Boolean


Set fd = Application.FileDialog(msoFileDialogOpen)


With fd
.Filters.Clear
.Filters.Add "Excel File", "*.xl*"
End With


file_was_chosen = fd.Show


If Not file_was_chosen Then
MsgBox "You didn't select a file"
Exit Sub
End If


fd.Execute
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi & welcome to MrExcel.
Try
Code:
If fd.SelectedItems.Count < 9 Then
   MsgBox "You didn't select 9 files"
   Exit Sub
End If
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Actually I should have been more precise - which operator would I use if I want to exit the macro if the number of files + not 9? So it also doesn't run of I select 10 files? I tried the following but it doesn't work.
Thanks!
If fd.SelectedItems.Count =NOT 9
 
Upvote 0
Almost got it, the NOT should go after the if
Code:
If Not fd.SelectedItems.Count =9
 
Upvote 0
Actually I just managed to sort it with the following

If fd.SelectedItems.Count < 9 Then
MsgBox "You didn't select 9 files"
Exit Sub


End If

If fd.SelectedItems.Count > 9 Then
MsgBox "You selected too many files"
Exit Sub

End If
 
Upvote 0

Forum statistics

Threads
1,212,928
Messages
6,110,734
Members
448,294
Latest member
jmjmjmjmjmjm

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