Paste to cell instead of a msg box - VBA Edit

cjouban

New Member
Joined
Jan 21, 2014
Messages
23
I found this macro and it almost does what I need, except for the msg box. Can anyone help me edit the code so it paste the count result to a cell in my open workbook instead of the message box. Feels like there is something really easy that I'm missing to accomplish this.
Code:
Sub FolderColl()
    Dim oFSO As Object
    Dim folder As Object
    Dim subfolders As Object
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = oFSO.GetFolder("J:\folderpath....")
    Set subfolders = folder.subfolders
    Sheet1.Range("B2").Value = subfolder.Count
    MsgBox subfolders.Count
    
    
    Set oFSO = Nothing
    Set folder = Nothing
    Set subfolders = Nothing
    'release memory


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
Sub FolderColl()
    Dim oFSO As Object
    Dim folder As Object
    Dim subfolders As Object
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = oFSO.GetFolder("J:\folderpath....")
    Set subfolders = folder.subfolders
    Sheet1.Range("B2").Value = subfolder.Count
    Sheet1.Range("A1").Value = subfolders.Count
    
    
    Set oFSO = Nothing
    Set folder = Nothing
    Set subfolders = Nothing
    'release memory


End Sub


This is should put the value on sheet 1 cell A1 rather than display a message box
 
Last edited:
Upvote 0
Code:
Sub FolderColl()
    Dim oFSO As Object
    Dim folder As Object
    Dim subfolders As Object
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = oFSO.GetFolder("J:\folderpath....")
    Set subfolders = folder.subfolders
    Sheet1.Range("B2").Value = subfolder.Count
    Sheet1.Range("A1").Value = subfolders.Count
    
    
    Set oFSO = Nothing
    Set folder = Nothing
    Set subfolders = Nothing
    'release memory


End Sub


This is should put the value on sheet 1 cell A1 rather than display a message box

Gives me a Run-time error '424': Object required
 
Upvote 0
Code:
Sub FolderColl()    Dim oFSO As Object
    Dim folder As Object
    Dim subfolders As Object
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = oFSO.GetFolder("[COLOR=#ff0000]change me for your filepath ie: C:\users\JoeBloggs\[/COLOR]")
    Set subfolders = folder.subfolders
    Range("[COLOR=#0000ff]A1[/COLOR]").Value = subfolders.Count
    
    Set oFSO = Nothing
    Set folder = Nothing
    Set subfolders = Nothing
    'release memory




End Sub

Change the red text for your filepath your looking to count subfolders in.
Change the Blue cell reference for the cell you wish the number to go into.

This will work on whichever sheet is the active sheet at the time, so if your assigning this macro to a button on a sheet, there should be no issues.
 
Upvote 0
Perfect. Thank you. Looks like I was missing a "s" in my original code Sheet1.Range("B2").Value = subfolders.Count .


​Thanks Again
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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