Not getting exact result while creating directory in vba

Gautham A

Board Regular
Joined
May 25, 2020
Messages
107
Office Version
  1. 2016
Platform
  1. Windows
Im writing a VBA code to check if a folder exists and if the folder exists just paste the Excel sheet. If the folder doesn't exist create a new folder in run time and save a sheet in that folder.

Code:
If(Len(Dir("C:\Users\Acer\Desktop\Report",vbdirectory))=0 Then
MkDir "C:\Users\Acer\Desktop\Report"
Activeworkbook.saveas filename:="C:\Users\Acer\Desktop\Report\file.xlsx", File format:=51
Else
filename:="C:\Users\Acer\Desktop\Report\file.xlsx", File format:=51
End If

The code works fine for the first time to create a new folder and save the Excel sheet. But on other instances it is not saving the file in the path.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Perhaps it is saving the file ... but it is overwriting the previous file ?
 
Upvote 0
I deleted all the files in that folder and also tried it. But still I'm not able to save again
 
Upvote 0
You aren't saving the file inside the else, it should be
VBA Code:
Else
ActiveWorkbook.Saveas filename:="C:\Users\Acer\Desktop\Report\file.xlsx", File format:=51
End If
 
Upvote 0
Apologies fluff this is a typing mistake by me here. Actually I saved it inside the else as well. But every time the loop doesn't come to the else part. The if condition becomes true even if the folder exists already
 
Upvote 0
If the folder exists & it still went to create the folder you would get an error.
 
Upvote 0
How should I write code for this?
If the folder exists just save the Excel file in there and if the folder doesn't exist create a folder and save the file in the created folder
 
Upvote 0
That's what your code does.
 
Upvote 0
Let us say the folder exists, then if part of the code should become false, but Everytime it becomes true. Any reason?
 
Upvote 0
Let us say the folder exists, then if part of the code should become false
That's correct & it's what happens when I run your code.
If the folder exists do you get any error messages?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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