I don't understand the need to copy the macro into a spreadsheet. Maybe you mean workbook? That should work fine. Just copy your code in a Module(s).
I could solve this for you fully but maybe you can get it from the code examples.
For the folder picking option:
Sub test_GetFolder()
MsgBox Get_Folder(ThisWorkbook.path, "Folder Picker")
End Sub
Function Get_Folder(Optional FolderPath As String, _
Optional HeaderMsg As String) As String
With Application.FileDialog(msoFileDialogFolderPicker)
If FolderPath = "" Then
.initialFilename = Application.DefaultFilePath
Else
.initialFilename = FolderPath
End If
.Title = HeaderMsg
If .show = -1 Then
Get_Folder = .SelectedItems(1)
Else
Get_Folder = ""
End If
End With
End Function
For finding CSV files, Dir() can have several issues. Since batch routines are fairly common, I made an example to show you how to replace your Dir() method and how to call a routine and pass the filename as the first and only input parameter. Obviously, you would replace the perco routine with yours. In your routine, remove the 2nd input parameter and add the code from your Dir() routine to set your range for the import.
'http://www.mrexel.com/forum/excel-questions/869792-run-same-macro-multiples-files-same-folder.html
Sub Test_kBatch()
kBatch "X:\FileFolder\csv\*.csv", "Module1.perco"
End Sub
Sub kBatch(myDir As String, myMacro As String, _
Optional tfSubFolders As Boolean = False)
Dim s As String, a() As String, v As Variant
If tfSubFolders Then
s = CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & myDir & """" & " /b /s").StdOut.ReadAll
Else
s = CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & myDir & """" & " /b").StdOut.ReadAll
End If
a() = Split(s, vbCrLf)
If UBound(a) = -1 Then
MsgBox myDir & " files not found.", vbCritical, "Macro Ending"
Exit Sub
End If
ReDim Preserve a(0 To UBound(a) - 1) As String 'Trim trailing vblfcr
For Each v In a()
If tfSubFolders Then
'Debug.Print v
Application.Run myMacro, v
Else
s = Left$(myDir, InStrRev(myDir, "\"))
Application.Run myMacro, s & v
End If
Next v
End Sub
Sub perco(aFile As String)
Dim MyString As String, MyVals As Variant, c As Range, lr As Long
Dim wb As Workbook
If Len(Dir(aFile)) = 0 Then
MsgBox aFile & " does not exist.", vbCritical, "Macro Ending"
Exit Sub
End If
Set wb = Workbooks.Open(aFile)
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each c In Range("A1:A" & lr)
MyString = c.Value
MyVals = Split(MyString, ",")
MyVals(5) = "^^"
c.Value = Replace(Join(MyVals, ","), ",^^,", ",")
Next c
wb.Close True
End Sub