Share via

VBA: Adding folders with names from an array

Anonymous
2023-08-14T21:16:51+00:00

I have an array with (for example) the following values:

arrSubProj(1) = "AAA"

arrSubProj(2) = "BBB"

arrSubProj(3) = "CCC"

I want to use these as the names of folders, which I can later move to be subfolders of another folder - that is, ending with something like:

MyTopFolder

   AAA 

   BBB

   CCC

But when I try:

If optCommercial.Value = True And txtNumSubProj.Value > 0 Then

For I = 1 To txtNumSubProj.Value

        Set mySubFolder = myFolders.Add(" &  arrSubProj(I) &  ") 

Next I 

I get a message that the folder already exists - and indeed the folder & arrSubProj(I) & is there - it's just not what I wanted.

How do I get it to use the values in the array in the Add, instead of the array name itself?

Help, please.

Many thanks!

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

9 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2023-08-14T21:34:58+00:00

    Does this work?

            Set mySubFolder = myFolders.Add(arrSubProj(I))
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. HansV 462.6K Reputation points
    2023-08-15T09:36:54+00:00

    If you add

            Debug.Print myFolders.Add(arrSubProj(I)) 
    

    below the the For I = 1 To txtNumSubProj.Value line, what do you see in the Immediate window?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-08-14T22:24:12+00:00

    Dim myFileSys As New FileSystemObject

    Dim myBaseFolder As Folder

    Dim mySubFolder As Folder

    Dim myFolders As Folders

    Dim strRootPath As String

    Dim intIDYear As Integer

    Dim I as integer

    intIDYear = Right(cboIDYear.Value, 2)

    txtBDNumber.Value = txtBDNumber.Value & cboProposalType.Value

    strRootPath = "\syn01\Proposals\EGS" & cboIDYear.Value & ""

    Set myBaseFolder = myFileSys.CreateFolder(strRootPath & "EGS" & intIDYear & txtBDNumber.Value & " " & txtClientName.Value & " " & txtClientProposalTitle.Value & "")

    ChDir (myBaseFolder)

    Set myFolders = myBaseFolder.SubFolders

    Set mySubFolder = myFolders.Add("Kickoff") 
    
    Set mySubFolder = myFolders.Add("Introduction") 
    
    Set mySubFolder = myFolders.Add("Signed") 
    
    Set mySubFolder = myFolders.Add("Submitted") 
    
    Set mySubFolder = myFolders.Add("Postmortem") 
    
    Set mySubFolder = myFolders.Add("Site Survey") 
    

    etc. for several other named folders relevant to the projects we work on.

    followed by

    If optCommercial.Value = True And txtNumSubProj.Value > 0 Then

    For I = 1 To txtNumSubProj.Value
    
            Set mySubFolder = myFolders.Add(arrSubProj(I)) 
    
    Next I 
    

    End If

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2023-08-14T22:13:28+00:00

    What is myFolders?

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-08-14T22:01:35+00:00

    Unfortunately, it generates a "Run-time error '5': Invalid procedure call or argument"

    Was this answer helpful?

    0 comments No comments