Consulting

Results 1 to 11 of 11

Thread: Folders path in excel cell

  1. #1

    Folders path in excel cell

    Hi all,

    I have this below code that create a duplicate copy with values only and saves in the path mentioned .

    now i cant always go to VB editor and put a new path there . hence i want that to put some where in sheet1("A2") range . and i can change it as per my need.

    below is the code that i got .

    Sub SaveAs_NewWb_02()'Nov 19, 2016
    Dim mywb As Workbook, wb As Workbook
    Set mywb = ThisWorkbook
    Dim ws As Worksheet
    Dim v As Variant, vv As Variant
    v = Array("summary", "invoices", "credits") '<< sht names
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set wb = Workbooks.Add
    For Each vv In v
    Set ws = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
    ws.Name = vv
    mywb.Sheets(vv).UsedRange.Copy
    ws.Cells(1, 1).PasteSpecial xlPasteValuesAndNumberFormats
    ws.Cells(1, 1).PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    ws.UsedRange.EntireColumn.AutoFit
    Application.CutCopyMode = False
    Next
    For Each sh In wb.Worksheets
    If sh.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious) Is Nothing Then
    sh.Delete
    End If
    Next
    With wb
    'change path as needed
    .SaveAs "c:\Users\Hudson" & Sheets("invoices").Range("B2").Value, 51 'formats: 51=xlsx 52=xlsm, 56=xls
    .Close False
    End With
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub
    
    any inputs are appreciated.
    
    Thanks .

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    .SaveAs Sheet1.[A2], 51

  3. #3
    Hi ken . Thanks for your advice . but this is not working .

  4. #4
    i mean to say . i am getting subsriprion error..

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Obviously, sheet1 codename must exist in the activeworkbook and the full path and filename must be in that cell. It must also be an existing drive path and a legal filename as well. You don't need to set a file extension if you set a file format though I like to do that too.

    If you want to use the current user's profile folder as in #1 you can do it like this:
    Sub Main()  
      Dim fn As String
      
      Sheet1.[a1] = "ken"
      
      fn = Environ("userprofile") & "\" & Sheet1.[a1] & ".xlsx"
      MsgBox fn
      'ThisWorkbook.SaveAs fn, 51
    End Sub

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Assuming that everything else works, are there spaces in B2 and is there a leading backslash?

    .SaveAs "c:\Users\Hudson" & Sheets("invoices").Range("B2").Value, 51
    Case 1

    B2 = Some Data, then path = c:\users\HudsonSome Data.xlsx


    Case 2

    B2 = \Some Data, then path = c:\users\Hudson\Some Data.xlsx

    Personally, I'm guessing that it's missing a back slash, and you wanted

    .SaveAs "c:\Users\Hudson\" & Sheets("invoices").Range("B2").Value, 51
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Ken. i ensured all the above and tried . still i an getting the same error message . i don't know why . and i cant use above code because code should pic three sheets with values only ( Array("summary", "invoices", "credits") basically its a template i designed .after macro got executed i want only three sheets from the workbook needs to saved on my desired path
    and path necessarily be same all the time.



  8. #8
    Hi paul. thanks for your advice . where do you want me to put slash .?. ideally it is not necessary . code is working fine . and all i am looking for a dynamic selection of path from excel cell rather than VB editor .

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by Hudson View Post
    Hi paul. thanks for your advice . where do you want me to put slash .?. ideally it is not necessary . code is working fine . and all i am looking for a dynamic selection of path from excel cell rather than VB editor .
    Post #6, second CODE block
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you are the only one using that template, then the hardcoded userprofile of c:\Users\Hudson is fine. As Paul said, #1 needs the backslash after Hudson.

    The brackets of B in #1 is what you wanted changed? I did that for you with Environ() in #5 so any userprofile would work for the current user. Obviously, no one else could use another's user's profile.

    Since the added workbook's 3 sheets have the same values as ThisWorkbook's same 3 sheets, then prefixing the period for the wb in #1 is not needed but I did it for you here and show how #1 and #5 can be combined.
    .SaveAs Envrion("userprofile") & "\" & .WorkSheets("invoices").Range("B2").Value & ".xlsx", 51 'formats: 51=xlsx 52=xlsm, 56=xls

  11. #11
    Paul and ken . thanks guys .

Posting Permissions

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