VBA Code - pick file name from cell value

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,494
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

This is part of my macro which I am using.

Code:
Sub makecopy()

    Workbooks.Add
    ChDir "C:\BACKUP"
    ActiveWorkbook.SaveAs Filename:="C:\Backup\[B][COLOR=#ff0000]Factory Target[/COLOR][/B].xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False  
    
End Sub

Can I tell the code to pick file name from cell value & some free defined text in the code.

For Example

Cell A1 = "Honda"
Cell B2 = "Civic"

I would like the file name to be "MAKE Honda MODEL Civic"

something like this
"MAKE" & Range A1.value & "MODEL" & Range B2.Value



Any help would be appreciated

Regards,

Humayun
 
Whilst this shouldn't make any difference given the error you're getting, but it's worth trying
Code:
   Dim fName As String
   fName = "C:\BACKUP\Customer" & Range("A1").Value & "Supplier" & Range("F1").Value & Format(Now, "yyyymmddhhmmss") & ".xlsx"
   MsgBox fName
   
   
   Workbooks.Add
   ActiveWorkbook.SaveAs fName, 51
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Thanks Fluff,

I don’t have access to my computer right now as I am off from my work.
But surely tomorrow I will try the solution you provided.

I will keep you posted

Take care
 
Last edited:
Upvote 0
Hi Fluff,

I tried the solution you provided... Its working fine

Now its debugging (Run time error 9 - Subscript out of range)

Code:
 Windows("Factory Target.xlsx").Activate

Obviously the name has changed that is why it is giving error

It should be like activate fName
 
Upvote 0
Try it like
Code:
Application.DisplayAlerts = False
Dim Wbk As Workbook
Dim fName As String
   fName = "C:\BACKUP\Customer" & Range("A1").Value & "Supplier" & Range("F1").Value & Format(Now, "yyyymmddhhmmss") & ".xlsx"
   MsgBox fName
   
   
   Workbooks.Add
   ActiveWorkbook.SaveAs fName, 51
   setwbk = ActiveWorkbook
    
    Windows("REPORTS").Activate
    Sheets("FACTORY TARGETS").Select
    
     Range("A1:AF1000").Select
    Selection.Copy
    
   Wbk.Activate
 
Upvote 0
run time error 438
Object does not support this property or method


Code:
 setwbk = ActiveWorkbook
 
Upvote 0
Oops, there should be a space after the word "Set"
 
Upvote 0
Thanks Fluff

All working fine now..... I also figured out why it was giving error on saving the file

The range references given in the code are cell A1 & F1

In cell A1 there is value like this Humayun / Fluff

Excel does not allow "/" in the file name

This was causing the problem
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

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