excel vba - Creating a folder and sub folder where none exist, but don't if they do -
ok, i've been working people on code, , have have come this:
this works universally between mac , pc.
option explicit sub createfolders() dim sheet1 worksheet 'sheet1 dim lastrow long, fstcell long dim strcompany string, strpart string, strpath string dim basefolder string, newfolder string dim cell range set sheet1 = sheets("sheet1") application .screenupdating = false .displayalerts = false .enableevents = false end sheet1 lastrow = sheet1.cells(rows.count, "b").end(xlup).row basefolder = "lists!$g$1" 'folders created within folder – change sheet of like. if right(basefolder, 1) <> application.pathseparator _ basefolder = basefolder & application.pathseparator each cell in range("s3:s" & lastrow) 'change suit 'company folder - column newfolder = basefolder & cell.value if len(dir(newfolder, vbdirectory)) = 0 mkdir newfolder 'part number subfolder - column c newfolder = newfolder & application.pathseparator & cell.offset(0, 1).value if len(dir(newfolder, vbdirectory)) = 0 mkdir newfolder next end application .screenupdating = true .displayalerts = true .enableevents = true end end sub
basefolder = "lists!$g$1"
this assigns basefolder literal value "lists!$g$1", not contents of cell. meant
basefolder = woksheets("lists").range("$g$1").value (or basefolder = [lists!$g$1], if prefer syntax).
also might find function useful: makesuredirectorypathexists.
Comments
Post a Comment