Forum

Modify VBA code whe...
 
Notifications
Clear all

[Solved] Modify VBA code when Blanks for folders

2 Posts
2 Users
0 Reactions
333 Views
(@webbers)
Posts: 147
Estimable Member
Topic starter
 

I found this VBA code that works great for my needs, however there is one little problem.  If there is a Blank in column A, meaning no more folder names, then the VBA code lists the status as "Folder already available".  I would like to have an IF Statement added based on whether or not column A in that row is blank.

For instance, in my screenshot, there are new folder names for cells A4-A6, and the status of "Folder Created" is correct.  However in rows 7-12, column A does not contain a value, so, column B in the corresponding row should reflect a blank cell also.  Below is my code.

2025 01 24 16 31 37

<code>

Option Explicit

Sub Create_Multiple_Folder()

Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets("Sheet1")

Dim sub_folder_path As String

Dim i As Integer

For i = 4 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row

sub_folder_path = sh.Range("C1").Value & Application.PathSeparator & sh.Range("A" & i).Value

If Dir(sub_folder_path, vbDirectory) = "" Then

MkDir (sub_folder_path)

sh.Range("B" & i).Value = "Folder Created"

Else

sh.Range("B" & i).Value = "Folder already available"

End If

Next i

End Sub

</code>

This topic was modified 3 months ago by Sherry Fox
 
Posted : 25/01/2025 7:37 am
Topic Tags
(@debaser)
Posts: 837
Member Moderator
 

I don't really know why you have a table with blank rows in it, but you can use:

 

For i = 4 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
If Len(sh.Cells(i, "A").Value) = 0 Then
sh.Cells(i, "B").Value = vbNullString
Else

sub_folder_path = sh.Range("C1").Value & Application.PathSeparator & sh.Range("A" & i).Value

If Dir(sub_folder_path, vbDirectory) = "" Then

MkDir (sub_folder_path)

sh.Range("B" & i).Value = "Folder Created"

Else

sh.Range("B" & i).Value = "Folder already available"

End If
End If

Next i
 
Posted : 28/01/2025 2:13 am
Share: