+ Reply to Thread
Results 1 to 3 of 3

two questions referencing a cell in a macro for folder and running a macro on one sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2016
    Location
    Colorado, US
    MS-Off Ver
    2010
    Posts
    2

    two questions referencing a cell in a macro for folder and running a macro on one sheet

    Hi there! I have 2 questions, but first I will input the code:
    Sub automkdir()
    
    Sheets("X").Select
    Dim xdir As String
        Dim fso
        Dim lstrow As Long
        Dim i As Long
        Set fso = CreateObject("Scripting.FileSystemObject")
        lstrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "H").End(xlUp).Row
        Application.ScreenUpdating = False
        For i = 2 To lstrow
            'path to create folders
            xdir = "C:\Users\blue\Documents\" & Range("H" & i).Value & Left(Range("G" & i).Value, 1)
            If Not fso.FolderExists(xdir) Then
                fso.CreateFolder (xdir)
            End If
        Next
        Application.ScreenUpdating = True
           
    '    Application.OnTime Now + TimeValue("00:01:00"), "automkdir"
    
    End Sub
    So this formula works great except for 2 things - It runs on every workbook, I want to limit it just to the workbook and sheet I'm in. I also don't even need it to be running in the foreground and would prefer it just run silently in the background no matter what others worksheets or workbooks I am in, as long as this one is open.

    The other problem I have is instead of C:\Users\blue\Documents" I want it to read blue as a variable that I input on lets say Sheet "Y". So if sheet Y has "blue" in Cell A1, and I change is to "red", the macro will look for C:\Users\red\Documents" instead.

    Thank you for your help!
    Last edited by adebusk; 05-05-2016 at 04:43 PM.

  2. #2
    Registered User
    Join Date
    04-07-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    34

    Re: two questions referencing a cell in a macro for folder and running a macro on one shee

    Try the below code, in this, it will run for "X" sheet only, since the third line of code is activating that sheet only

    Sub automkdir()
    VarUserName = Worksheets("Y").Cells(1, 1).Value
    Sheets("X").Select
    Dim xdir As String
        Dim fso
        Dim lstrow As Long
        Dim i As Long
        Set fso = CreateObject("Scripting.FileSystemObject")
        lstrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "H").End(xlUp).Row
        Application.ScreenUpdating = False
        ActiveSheet.Range("H2:H" & lstrow).Select ' Limiting the selection to ActiveSheet
        For Each Cell In Selection
            'path to create folders
            xdir = "C:\Users\" & VarUserName & "\Documents\" & Cell.Value & Left(Cell.Value, 1)
            If Not fso.FolderExists(xdir) Then
                fso.CreateFolder (xdir)
            End If
        Next
        Application.ScreenUpdating = True
           '    Application.OnTime Now + TimeValue("00:01:00"), "automkdir"
    End Sub

  3. #3
    Registered User
    Join Date
    05-05-2016
    Location
    Colorado, US
    MS-Off Ver
    2010
    Posts
    2

    Re: two questions referencing a cell in a macro for folder and running a macro on one shee

    That worked just about perfect. The only thing I'm missing is I would like for the macro to run 100% silent, in the background. I.e. If I'm on sheet "B" I want it to run only on sheet "X" and not continually pop over to sheet "X"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. in Sheet `inputs' Clear Cell D1 and copy from F1 before running the macro.
    By thilag in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-10-2016, 03:44 AM
  2. [SOLVED] Running a macro on multiple workbooks in a folder
    By Sweepin in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-29-2013, 08:11 PM
  3. [SOLVED] Macro running on active sheet instead of sheet defined in macro
    By davegscott in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-19-2013, 02:04 PM
  4. Running A macro through multiple files in a folder
    By Briansva92 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2012, 01:26 PM
  5. Replies: 2
    Last Post: 06-03-2012, 03:33 PM
  6. macro to save specific sheet as pdf direct to folder linked via cell
    By sdts in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-17-2012, 05:45 AM
  7. Questions regarding Cell Referencing
    By hparteep in forum Excel General
    Replies: 1
    Last Post: 04-01-2006, 12:13 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1