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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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,214,516
Messages
6,119,978
Members
448,934
Latest member
audette89

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