VBA Code to Switch Between Workbooks and Get the Filename as Text without the Path

UncleBajubjubs

Board Regular
Joined
Jul 11, 2017
Messages
111
Office Version
  1. 2010
Hello, I currently have a workbook which another worker will enter information into, and which I will then pull information out of into a workbook I use. The workbook they have is a form in which they will fill in all necessary information about a project. I get the workbook from then, open it up, and my workbook copies all the data from it. My only issue is that some of them use an older version of the form, which has a cell's number format as 'text' when it should be 'number'. If I copy this, it messes up the data in my program, so I'm trying to fix the cell prior to copying the data.

I use
Dim filename As Variant
filename = Application.GetOpenFilename()

to open up their workbook to get the data, and I tried

Windows(filename).Activate
Range("P20").Select
Selection.NumberFormat = "General"

but it tells me Subscript out of range on that first line, as the filename contains the entire path of the file (C/User/Documents...etc.). Is there an easy way to remove the file path so that it is just the file name? Unfortunately I can't just have it be a set name, because the others working with it will name it whatever project they are working on.

This is my first post so I tried to have as much information as possible, thank you!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Welcome to the forums!

Couple ways to approach this. To answer your direct question, try using filename = activeworkbook.name

A more elegant approach would be to create a workbook variable and use that to reference that workbook. This way, you can easily reference that workbook in later parts of the code.

For example:

Code:
Public Sub foo()
Dim sWB     As Workbook
    
Set sWB = ActiveWorkbook

sWB.Range("P20").NumberFormat = "General"
End Sub
 
Last edited:
Upvote 0
Welcome to the forums!

Couple ways to approach this. To answer your direct question, try using filename = activeworkbook.name

A more elegant approach would be to create a workbook variable and use that to reference that workbook. This way, you can easily reference that workbook in later parts of the code.

For example:

Code:
Public Sub foo()
Dim sWB     As Workbook
    
Set sWB = ActiveWorkbook

sWB.Range("P20").NumberFormat = "General"
End Sub

Hello, Thank you for the reply, I still have the issue though, as the activeworkbook will be the one I have opened, not the one that contains the data I need. I am not sure how to switch the other one to being the activeowrkbook.
Thank you!
 
Upvote 0
Ahh, I believe I misunderstood your question. Are you using VBA to open this other workbook? Or are you opening the workbook and then running code from a different workbook?
 
Upvote 0
So I am using a workbook called"Data Program". The other workers will fill out a workbook called "Form". I open up both Data Program and Form. I then run a macro in Data Program which has me select the file to pull data from. All of that works, and it pulls the data from Form into Data Program. The issue is that some of the workers are using an old version of Form in which a cell incorrectly has its number format set as "text" when it needs to be "general". I am trying to use the macro in "Data Program" to modify the cell in "Form". Were the file always named "Form", I would use

Windows("Form").Activate

to switch to Form and change it. However, the workers rename it match the project they are working on, and I will not be the only one using Data Program, so it needs a better solution than manually renaming the file each time. I've tried using

filename = Application.GetOpenFilename()

but that includes the full file path, so I can't use that in combination with

Windows().Activate

Thank you again!
 
Upvote 0
I think I understand now. You're using the code to open a dialogue box which will open the file (since it isn't always named "Form"), and you need a way to parse out the workbook name.... or at least reference the workbook. That correct?

If so, lets go about it a slightly different route.

Try:
Code:
Public Sub foo()
Dim formWB      As Workbook
Dim filename    As String
filename = Application.GetOpenFilename()
Set formWB = Workbooks.Open(filename)
End Sub

Now, whenever you need to reference the Form workbook, you can use "formWB" in place of "Workbooks(name)."

For example, instead of Workbooks(name).Activate, you can use formWB.activate. Or instead of Workbooks(name).Sheets("Sheet1").Range("A1").Copy, you can use formWB.Sheets("Sheet1").Range("A1").Copy
 
Upvote 0
That all works now, thank you!
Unfortunately, excel now correctly goes to cell in the workbook, but gives me

Unable to set the numberformat property of the range class.

I have

rfqwb.Activate
Call UnProtectWorkbook



Range("P20:AA20").Select


Selection.NumberFormat = "General"

And that last line is where the code breaks. Any clues?
 
Upvote 0
Only thing I can assume is that the worksheet which contains the range you want to adjust the numberformat on is protected. Make sure to unprotect the worksheet (I see you have an unprotectworkbook sub you're calling, which might only be hitting it at the workbook level)
 
Upvote 0
Great! Thanks for the feedback. Have a good one!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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