Consulting

Results 1 to 3 of 3

Thread: VBA code to identify folder based on folder name given in a excel sheet

  1. #1

    VBA code to identify folder based on folder name given in a excel sheet

    Hi,

    Can anybody help me with this?

    Suppose the name (ABCD) is mentioned in range A2. There is a folder named ABCD in C:\Users\ . I want the macro to identify ABCD folder based on range A2 and save the excel sheet in that folder.

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is not just

    ActiveWorkbook.SaveAs "C:\Users\" & Environ("Username") & "\" & Activesheet.Range("A2").Value & "\" & Activeworkbook.Name
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld View Post
    Is not just

    ActiveWorkbook.SaveAs "C:\Users\" & Environ("Username") & "\" & Activesheet.Range("A2").Value & "\" & Activeworkbook.Name

    I have this code. But it is not saving the file in the folder.

    Sub Test_Folder_Exist_With_Dir()    Dim FolderPath As String
        Dim TestStr As String
    
    
        FolderPath = "C:\Users"
        If Right(FolderPath, 1) <> "\" Then
            FolderPath = FolderPath & "\"
        End If
    
    
        TestStr = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
        On Error Resume Next
        TestStr = Dir(FolderPath)
        On Error GoTo 0
        If TestStr = "" Then
            MsgBox "Folder doesn't exist"
        Else
          ActiveWorkbook.SaveCopyAs Filename:=FolderPath & ThisWorkbook.Sheets("Sheet1").Range("A2").Value & ".xlsm"
        End If
    
    
    End Sub
    Ok i figured it out. Thanks
    ActiveWorkbook.SaveCopyAs Filename:=FolderPath & ThisWorkbook.Sheets("Sheet1").Range("A2").Value & "\" & ThisWorkbook.Sheets("Sheet1").Range("A2").Value & ".xlsm"
    Last edited by rockybalboa; 04-29-2014 at 10:42 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •