Create new folder if folder does not exist

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
203
Office Version
  1. 2013
Platform
  1. Windows
I'm struggling with this one. It continually errors out when attempt to create the dir.

Location of error is last line MKDir partsubfolder

Any assistance would be greatly appreciated. Thanks in advance!

Code below;

VBA Code:
Dim sourceDir As String
Dim folder_exists As String
Dim partSubfolder As String

    'source directory
    sourceDir = "C:\Users\dans\documents\" & CustomerTB.Value & "\"
    partSubfolder = sourceDir & PartNoCB.Value & "\"
    
    'check if year subfolder exists, if it doesn't then create a new folder
    folder_exists = Dir(partSubfolder, vbDirectory)
    If folder_exists = vbNullString Then MkDir partSubfolder
 
TY!

I would assume both of these subs can be ran under the same function?
Yes because Sub CreateDirectory is not "sensitive" to if there is a "\" at the end of the path or not. So to encompass both subs, you can do this:

If you mean "called" (with a Call statement . . . . sorry for the technicality), then absolutely.
VBA Code:
        Sub Test__Move_This_File_From_This_Dir_to_This_Dir()

Dim sourcePath As String
Dim destinationPath As String
Dim fileName As String
sourcePath = "something"
destinationPath = "something"
fileName = "something" 'Include file extension, of course

Dim sourceDir As String
Dim folder_exists As String
Dim partSubfolder As String

'source directory
sourceDir = "C:\Users\dans\documents\" & CustomerTB.Value & "\"
partSubfolder = sourceDir & PartNoCB.Value & "\"

Call CreateDirectory(partSubfolder)
Call Move_This_File_From_This_Dir_to_This_Dir("ff.txt", sourcePath, destinationPath)
End Sub

Sub Move_This_File_From_This_Dir_to_This_Dir(fileName As String, source_Directory As String, destination_Directory As String)
'https://stackoverflow.com/a/16943127

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.MoveFile(source_Directory & "\" & fileName, destination_Directory & "\" & fileName)

End Sub
I get an error on the last line.

What is the ff.txt for?
Sorry, that should be fileName.

And I am working to answer your previous question. I will be a few more minutes.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
In this (generalized) example, I have "Start Folder" and "End Folder" (they are technically subfolder names):
VBA Code:
Sub Test__2()

'File Name
    Dim fileName As String
    fileName = "something" 'Include file extension, of course

'Source directory
    Dim sourceDir As String
    Dim partSubfolder As String
    sourceDir = "C:\Users\dans\documents"
    partSubfolder = sourceDir & "\" & "Start Folder" 'There is no & "\" at the end

'Destination directory
    Dim destinationSubfolder As String

    sourceDir = "C:\Users\dans\documents"
    destinationSubfolder = sourceDir & "\" & "End Folder" 'There is no & "\" at the end

'Create a folder (and all subfolders implied to exist by partSubfolder) (And do the same for destinationSubfolder)
Call CreateDirectory(partSubfolder)
Call CreateDirectory(destinationSubfolder)

'Move
Call Move_This_File_From_This_Dir_to_This_Dir(fileName, partSubfolder, destinationSubfolder)

End Sub
 
Upvote 0
figured out the issue.

I am using a listbox, not a textbox. ?
 
Upvote 0
figured out the issue.

I am using a listbox, not a textbox. ?
Do you mean Excel Table (Ctrl T) instead of normal range (cell)?

If so, then you just need ListBox (table) name.Range("cell address")

For example, if you have a table named "CustomerTB." from a sheet named "sheetName"
Excel Formula to LaTeX Print Converter 4.1.xlsb
ABCDE
1Column1Column2Column3Column4Column5
2
3
4
5
6
7
8
9
10
11
12a
Sheet1


Then the following will display/get the text "a" from the table.
VBA Code:
Sub Show_A_Cell_In_The_Specified_Table_In_The_Specified_SheetName()

Dim tbl As ListObject
Set tbl = Sheets("sheetName").ListObjects("CustomerTB.")

MsgBox tbl.Range(12, 3)

End Sub
 
Last edited:
Upvote 0
I can't see why you can't just replace CustomerTB (I wrote "CustomerTB." in the last post but I meant to write "CustomerTB", sorry, but it is okay to have "."'s in table names) with Sheets("sheetName").Range("C12").value (where "C12" is the position of where "a" is in the previous example) to get "a" (still reusing the previous example's Excel table) instead of having to do Sheets("sheetName").ListObjects("CustomerTB").Range(12,3).value (I forgot to put the .value in the last post too, but if you don't put that, it's the default property of Range().), but:

If you must based on what your code is doing, here is a little function I just wrote so that you can still input the original (individual/single) cell address and it will retrieve it from the table, no matter where the top-left corner of that table is located in the sheet. (If you offset the table in the previous example, you still have to have the (12,3) to still give you the value of "a", because the (12,3) is relative to the position of the table in the spreadsheet!)

(Again, in this example, the code will MsgBox "a".)

Excel Formula to LaTeX Print Converter 4.1.xlsb
ABCDEFGH
1
2
3
4
5
6Column1Column2Column3Column4Column5
7
8
9
10
11
12
13
14
15
16
17a
sheetName


VBA Code:
Sub Test__Get_Val()
MsgBox Get_Val("F17", "CustomerTB", "sheetName")
End Sub
Function Get_Val(rangeAddress As String, tableName As String, sheetName As String)

Dim cellAddressRow As Long
cellAddressRow = Sheets(sheetName).Range(rangeAddress).Row

Dim cellAddressColumn As Integer
cellAddressColumn = Sheets(sheetName).Range(rangeAddress).Column

With Sheets(sheetName).ListObjects(tableName)
    Dim startRow As Long
    startRow = .DataBodyRange(1, 1).Row

    Dim startColumn As Integer
    startColumn = .DataBodyRange(1, 1).Column

    Get_Val = .Range(cellAddressRow - startRow + 2, cellAddressColumn - startColumn + 1).Value
End With

End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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