I am using VBA to list the folders in a selected directory. However, the output is placed in column A, how can I have VBA prompt me to choose a column to put the information in?
Here is my code:
Sub Print_List is where the information is removed and output.
I want to be able to choose a column, remove the previous data, then apply the new data.
Any help would be great. I am at a loss on how to be able to choose a column.
Here is my code:
Code:
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
x = SHBrowseForFolder(bInfo)
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
Sub List_Sub_Directories()
Dim dirList() As String
Dim Msg As String
Dim Directory As String
Msg = "Select a location containing the files you want to list."
Directory = GetDirectory(Msg)
If Directory = "" Then Exit Sub
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
List_Directories Directory, dirList()
Print_List dirList()
End Sub
Sub List_Directories(anypath As String, dirList() As String)
Dim dirOutput As String, i%
dirOutput = Dir(anypath, vbDirectory)
Do While dirOutput <> ""
If dirOutput <> "." And dirOutput <> ".." Then
If (GetAttr(anypath & dirOutput) And vbDirectory) = vbDirectory Then
i = i + 1
ReDim Preserve dirList(1 To i)
dirList(i) = anypath & dirOutput
End If
End If
dirOutput = Dir()
Loop
End Sub
Sub Print_List(anyList() As String)
Dim i As Integer
'Clear any previous text
Range(Cells(1, 1), Cells(1, 1).End(xlDown)).ClearContents
For i = LBound(anyList) To UBound(anyList)
Cells(i, 1).Value = anyList(i)
Next i
'Format
Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Columns.AutoFit
End Sub
Sub Print_List is where the information is removed and output.
I want to be able to choose a column, remove the previous data, then apply the new data.
Any help would be great. I am at a loss on how to be able to choose a column.