[VBA Related] issue when Create a sub path & export a cell data to the sub path based on the folder name

Daisy_PENG

New Member
Joined
May 15, 2017
Messages
2
Hello everyone,

I am trying to use VBA to create & export some data of the Excel file to some shared profiles.
Below is what I have done so far:

Sub ExportAllSQL()


Dim wsData As Variant
Dim myFileName As String
Dim FN As Integer
Dim p As Integer, q As Integer
Dim path_parent As String, path_real As String
Dim myString As String
Dim company_name As String
Dim lastrow As Long, lastcolumn As Long
Dim weeknum As Variant
Dim company_abbr As String


lastrow = Worksheets("Source_Data").Range("A" & Rows.Count).End(xlUp).Row
lastcolumn = Worksheets("Source_Data").Cells(1, Columns.Count).End(xlToLeft).Column
weeknum = Format(Now(), "ww")
p = lastcolumn
'MsgBox myFileName
For q = 5 To lastrow
company_abbr = Left$(Worksheets("Source_Data").Cells(q, "A"), 5)
company_name = Application.WorksheetFunction.vlookup(company_abbr, Worksheets("Abbriviation").Range("A:B"), 2, False)
path_parent = "\\datasrv\devpt\Web\Bases\Emailings\Anciens mailings\2017" & company_name
MkDir path_parent & "\Week " & weeknum & "" >>> ERROR WHEN ACCESS TO THE PATH/FOLDER
wsData = ActiveSheet.Cells(q, "A").Value
myFileName = wsData
myFileName = myFileName & ".sql"
myFileName = path_parent & "\Week " & weeknum & "" & myFileName
myString = Cells(q, p)
FN = FreeFile
Open myFileName For Output As #FN
Print #FN, myString
Close #FN
Next q
myString = ""
End Sub


The error occurs in the step MkDir path_parent & "\Week " & weeknum & "" and the message shows as cannot find the path. However, by checking the location, it can be seen that the folder of week number under the company's folder is created.

For your information, before I added mkDir part(i.e. when I am using an exist & fixed path for the value of "path"), it works.

It would be perfect if you could kindly share some insight.

Thanks in advance!!!!

Daisy
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If the folder exists then you will get an error if you try to create a folder with that name. try checking if it exists first:

Code:
If Dir(path_parent & "\Week " & weeknum & "" ,vbDirectory) = vbNullString Then
    MkDir (path_parent & "\Week " & weeknum & "")
End If
 
Upvote 0

Forum statistics

Threads
1,223,104
Messages
6,170,124
Members
452,303
Latest member
c4cstore

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