Message Box

superfb

Active Member
Joined
Oct 5, 2011
Messages
255
Office Version
  1. 2007
Platform
  1. Windows
Hi

i have a macro code that creates a folder by what i specify in a cell. i woud like this code to incorporate a message box telling me what folder has been created

Code:
 Sub MakeSubFolder()

Dim lngCount As Long
lngCount = 2
If Len(Dir(Cells(1, 20), vbDirectory)) = 0 Then
   MkDir Cells(1, 20)
Else
    Do Until Len(Dir(Cells(1, 20) & " - " & lngCount, vbDirectory)) = 0
        lngCount = lngCount + 1
    Loop
   MkDir Cells(1, 20) & " - " & lngCount
End If


' message box
MsgBox "Folder Created", vbOKOnly, "Cars"

Please note i usually create a folder in the following format YYYYMMDD If there is two folders with the same name the following folder creates YYYYMMDD - 2..I would like the message box to say what folder has been created.....

many thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Re: Message Box help

try

Code:
Sub MakeSubFolder()

Dim lngCount As Long, [COLOR=#006400]Msg As String[/COLOR]
lngCount = 2
If Len(Dir(Cells(1, 20), vbDirectory)) = 0 Then
   MkDir Cells(1, 20)
Else
    Do Until Len(Dir(Cells(1, 20) & " - " & lngCount, vbDirectory)) = 0
        lngCount = lngCount + 1
    Loop
   MkDir Cells(1, 20) & " - " & lngCount
   [COLOR=#006400]Msg = " - " & lngCount[/COLOR]
End If


' message box
MsgBox "Folder Created" [COLOR=#006400]& vbCr & Cells(1, 20) & Msg[/COLOR] , vbOKOnly, "Cars"
 
Upvote 0
Re: Message Box help

That is awesome - works a treat! can you explain very kindly the new code added by you what it does???? just so i could have a better understanding
 
Upvote 0
Re: Message Box help

Code:
Dim lngCount As Long, Msg As String
lngCount = 2
If Len(Dir(Cells(1, 20), vbDirectory)) = 0 Then
   MkDir Cells(1, 20)
Else
    Do Until Len(Dir(Cells(1, 20) & " - " & lngCount, vbDirectory)) = 0
        lngCount = lngCount + 1
    Loop
   MkDir Cells(1, 20) & " - " & lngCount
   Msg = " - " & lngCount   ([B][I]Note 1[/I][/B])
End If

MsgBox "Folder Created" & vbCr & Cells(1, 20) & Msg , vbOKOnly, "Cars"  ([B][I]Note 2[/I][/B])

Note 1
Msg = whatever the code added to YYYYMMDD when creating the new folder

Note 2
MsgBox "Folder Created" & vbCr & Cells(1, 20) & Msg

& = concatenate (join text together)
vbCr = carriage return (puts text on next line)

If folder YYYYMMDD did not exist then Msg is empty
Cells(1, 20) & Msg returns:
Folder Created
20190325

If folder YYYYMMDD did exist then Msg contains a value
Cells(1, 20) & Msg(=whatever the code added to YYYYMMDD) returns:
Folder Created
20190325 - 2
 
Last edited:
Upvote 0
Re: Message Box help

A tidier way to achieve what you want

Code:
Dim lngCount As Long, Msg As String
lngCount = 2
If Len(Dir(Cells(1, 20), vbDirectory)) = 0 Then
   Msg = Cells(1, 20)
   MkDir Msg
Else
   Do Until Len(Dir(Cells(1, 20) & " - " & lngCount, vbDirectory)) = 0
        lngCount = lngCount + 1
   Loop
   Msg = Cells(1, 20) & " - " & lngCount
   MkDir Msg
   
End If

' message box
MsgBox "Folder Created" & vbCr & Msg , vbOKOnly, "Cars"
 
Upvote 0
Re: Message Box help

Hiya is there a way I could incorporate the Text formula Text("YYYYMMDD") in the msgbox .....thanks
 
Upvote 0
Re: Message Box help

Try like this

Code:
Msg = Format(Cells(1, 20), "YYYYMMDD")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
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