Define As String variably using If & ElseIf

SamanthaNZ

New Member
Joined
Aug 16, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi there experts!

I have a preexisting VBA code to create and submit data files into a Snowflake Data Warehouse. Now, they'd like me to change the data file names to have a version suffix, to differentiate files in case we need to resubmit them during the month. For example "..._01", "..._02", "..._03" etc. It is very unlikely we would submit more than four times per month.

I have "Dim FileName As String". This currently includes the file location and file name which includes a fixed title component, the month and year. I would now like to define FileName to also include the appropriate suffix. If this can be achieved, I won't have to amend the entirety of the VBA code where FileName is repeatedly referred to. I have tried the code below, but when the macro progresses it won't allow me to save the new file as FileName, so I must not have defined it correctly.

Is it possible to change the definition of FileName depending on what files already exist?

Thank you so much!!
Samantha
 

Attachments

  • Capture.JPG
    Capture.JPG
    93.9 KB · Views: 14

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi, @SamanthaNZ, welcome to the Forum
It should be:
VBA Code:
If Dir(Checkfile1) = "" Then
it means if Checkfile1 doesn't exist.
Do the same to all other Checkfile
 
Upvote 0
Hello Akunini! Thank you so much for your help. I made the changes, but am still getting the same error when I go to save the new data file referring to FileName. Any ideas would be amazing. ?
 

Attachments

  • CaptureA.JPG
    CaptureA.JPG
    31.7 KB · Views: 8
  • CaptureB.JPG
    CaptureB.JPG
    19.5 KB · Views: 9
Upvote 0
Can you post your code as text not as an image?
 
Upvote 0
You actually have multiple issues.
  • Dir (CheckFile1) is returning ""
    because it needs ".xlsx" added to the name
    eg
    CheckFile1 = PublicLocation & Name1 & ".xlsx"

  • ActiveWorkbook.SaveAs PublicLocation & FileName, 51
    Needs to be changed to
    ActiveWorkbook.SaveAs FileName, 51

    FileName already includes the filepath
    eg FileName based on CheckName1 returns > C:\Users\Public\Documents\APAC_Actual_Resin202108_01.xlsx
 
Upvote 0
Solution
Hi Akuini, my apologies, I will post the code as text in future. ?

Hi Alex, OMG that fixed everything!!! You are so clever. Thank you SO SO much. That is amazing, you've made my day! ??
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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