VBA: Open Excel Workbook using cells value as variable for file name

timal03

New Member
Joined
Aug 3, 2012
Messages
6
Hello,

I try to write this macro which could enable me to open files according to cells value I chose previously in my active workbook (In "Inputs" sheet to be accurate). These files I want to open are closed and under other folders.

Here is my code but I have this message poping up "Type Mismatch, Error '13':

I am just discovering VBA... Can somebody try to solve this problem please ?

Thanks a lot !

Here is my code:

Code:
Sub ImportFile()
Dim A As String
Dim B As String
Dim C As String

If Sheets("Inputs").Range("A_A1") > Sheets("Inputs").Range("G6") Then
A= Sheets("Inputs").Range("A_A1") & "_12"

Else: Sheets("Inputs").Range("A_A1") = Sheets("Inputs").Range("G6")

If Sheets("Inputs").Range("M3") < 10 Then
A = Sheets("Inputs").Range("A_A1") & "_0" & Sheets("Inputs").Range("M3")

Else
If Sheets("Inputs").Range("M3") >= 10 Then
A = Sheets("Inputs").Range("A_A1") & "_" & Sheets("Inputs").Range("M3")
End If
End If


If Sheets("Inputs").Range("B_B1") = "BLUE Then
B = "BLUE"
Else
B = "RED"

C = Range("C_C1") & " - " & A 

End If
End If

Workbooks.Open Filename:="S:\XXXX\ZZZZ\& A & " \ " & B & " \ " & C & *.xls"

End Sub
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
try adding .value on the end of your ranges to look at the value in the ranges rather than the range objects themselves.

example: A= Sheets("Inputs").Range("A_A1").value & "_12"


hope that helps

mrhopko
 
Upvote 0
Hello,

Thank you for your help...

I did what you adviced me "mrhopko" but I still have the same error message.

And I don't know to which line it reffers to ... There is no highlighted line like I sometimes see...

Unsolving problem ? :)

Thank you
 
Upvote 0
In the VBE. go Tools>Options>General Tab and under the Error Trapping section ensure that "Break in Class Module" is the option selected. Change if not, select the OK button and re-run the code. Now, is the problem line selected when it debugs?
 
Upvote 0
Hey, with a "light" late, thank you for your help.

I could have managed it at the end.

Thank you again
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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