• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Insert specific workbooks into a specific folder

Hello All,


I have 6 excel workbooks(templates) and 123 folders, some folders get all 6 workbooks but other get less, is there a way(script) that I could run and insert them as requested.


Example:

temp1,tem2,temp3 > folder 1

temp2 > folder 2

temp1, temp4, temp6 > folder 3


Any suggestions would be greatly appreciated


Dennis
 
Hi, dgavilanes


Give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/Insert%20specific%20workbooks%20into%20a%20specific%20folder%20%28for%20dgavilanes%20at%20chandoo.org%29.xlsm


This is the code:

-----

[pre]
Code:
Option Explicit

Sub ShouldAdjust()
' constants
'  ws & ranges
Const ksWS = "Hoja1"
Const ksFolder = "FolderList"
Const ksFile = "FileList"
Const ksShould = "ShouldTable"
'  others
Const ksX = "X"
' declarations
Dim rngFo As Range, rngFi As Range, rngS As Range
Dim fs As Object, fld As Object, fn As Object
Dim sPathSource As String, sFileSource As String
Dim sPathTarget As String, sFileTarget As String
Dim I As Long, J As Integer
' start
'  ranges
Set rngFo = Worksheets(ksWS).Range(ksFolder)
Set rngFi = Worksheets(ksWS).Range(ksFile)
Set rngS = Worksheets(ksWS).Range(ksShould)
'  file system
Set fs = CreateObject("Scripting.FileSystemObject")
' proceso
sPathSource = ActiveWorkbook.Path & Application.PathSeparator
With rngS
For I = 1 To .Rows.Count
' folders
sPathTarget = sPathSource & rngFo(I, 1).Value
If Not fs.FolderExists(sPathTarget) Then fs.CreateFolder sPathTarget
For J = 1 To .Columns.Count
' files
sFileSource = sPathSource & Application.PathSeparator & rngFi(1, J).Value
sFileTarget = sPathTarget & Application.PathSeparator & rngFi(1, J).Value
If .Cells(I, J).Value = ksX Then
If Not fs.FileExists(sFileTarget) Then _
fs.CopyFile sFileSource, sFileTarget, True
End If
Next J
Next I
End With
' end
'  file system
Set fs = Nothing
'  ranges
Set rngS = Nothing
Set rngFi = Nothing
Set rngFo = Nothing
'  beep
Beep
End Sub
[/pre]
-----


Just advise if any issue.


Regards!
 
Thanks for your response,


I try running it but stops short here. Maybe I'm missing something.


fs.CopyFile sFileSource, sFileTarget, True


Regards


Dennis
 
Hi, dgavilanes!


Three things:


a) There are three dynamic named ranges defined, FolderList (column A from A2), FileList (row 1 from B1), and ShouldTable (FolderList x FileList). If you aren't using my uploaded file then get sure you defined those names properly


b) The six files are supposed to be in the same folder as the workbook with the macros. If they're not there, just copy or move them.


c) The folders are supposed to be subfolders (with relative paths, not full) of the same folder containing this workbook and the six workbooks. If you need to make them not dependent of that path, you'd have to adjust the code accordingly.


If none of these help, consider uploading the file with the issue.


Regards!


EDITED
 
Hi, dgavilanes!

Glad you solved it. Thanks for your feedback and for your kind words too. And welcome back whenever needed or wanted.

Regards!
 
Back
Top