VBA Output to a specific column.

dhosi439

Board Regular
Joined
May 13, 2009
Messages
62
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:

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.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try

Code:
Sub Print_List(anyList() As String)
Dim i As Integer
Dim MyCol As String
'Clear any previous text
MyCol = InputBox("Please Type A Column Letter")
Range(Cells(1, MyCol), Cells(1, MyCol).End(xlDown)).ClearContents
For i = LBound(anyList) To UBound(anyList)
Cells(i, MyCol).Value = anyList(i)
Next i
'Format
Range(Cells(1, MyCol), Cells(1, MyCol).End(xlDown)).Columns.AutoFit

End Sub

Hope that helps..
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top