excel


Excel/VBA get full file path from file selection


I am having a VBA macro that does something like that
Sub test()
For Each X In Workbooks("file.xlsx").Sheets("sheetname")
Do something
Next
End Sub
The sheet I am executing this macro from is not the sheet in the for-each loop. I also run this macro by a button in the sheet.
However, the filename and location will be different 99% of the time this macro is executed. So, I want a possibility when this macro is executed, that a "open file" dialog is appearing, where I select a file, and then that file is used in that macro.
I'd also like to make it similar like this:
Dim worksheetname as variable
For Each X in worksheetname.sheets("sheetname")
instead of making it directly in the line, as it is better than having it directly in the code.
However even with the hard coded filename I failed to do so properly (don't know what to dim it as, as well as how to set it to the file name then), if you guys could help me with that as well (in conjunction with making it via the file dialog), it would be great.
found out that one at least so far. so only getting it from a file dialog selection is left now
Thanks!
Xeb
Try something like this, I call it as a function with args for the file type filters and (optional) default directory that it will open:
Sub g()
FilePath = FileSelectBox("*.xlsx")
Msgbox "Filepath = " & FilePath
End Sub
Function FileSelectBox(ByRef FileType As String, Optional ByVal DefaultDir As String) As String
Dim a As Object, FileName As String, varFile As Variant
Set a = Application.FileDialog(msoFileDialogFilePicker)
With a
.AllowMultiSelect = False
.Title = "Select File..."
.Filters.Clear
.Filters.Add "Excel Files", FileType
If Not IsMissing(DefaultDir) And DefaultDir <> "" Then .InitialFileName = DefaultDir
If .Show = True Then
For Each varFile In .SelectedItems
FileSelectBox = varFile
Next varFile
End If
End With
End Function
MSISDN: https://msdn.microsoft.com/en-us/library/office/ff196794.aspx

Related Links

Create 1 list in Excel from 2 other lists
Color specific excel cells based on match condition in python
Powershell+Export-Excel module
Calculate a Subtotal of a Sumproduct
Sub a number with column numbers and show the results in a row
Vlookup in VBA to work on Numbers in different format
Highlight rows if value in column A is x
Deleting entire row based on cell value
VBA listing all Files in subfolder using CMD, and sorting with manually input data
Automated updating of links_Folder/File Names_Excel
Replace text in a cell containing a string with another string
Excel - Issue with using only ints to get range
VBA crashes Excel
Excel Macro to validate CSV field format
Fill Each Cell With Formula
VBA- Copy columns from one source worksheet to end of multiple sheets

Categories

HOME
highcharts
google-maps-api-3
sitecore8
multidimensional-array
jupyter-notebook
form-submit
https
cakephp-3.0
zurb-foundation
amazon-redshift
boost
cordova-plugins
raspberry-pi3
vaadin-grid
datagrid
jasperserver
geometry
axon
ietf-netmod-yang
subplot
libcurl
robots.txt
intercept
raycasting
cider
react-apollo
boto
wikitude
pfsense
eof
ddos
u2
measurement
jqassistant
live
chmod
legacy
swiftmailer
tail-recursion
each
google-nearby
azure-sql-server
ektron
android-device-monitor
data-processing
qnx
carplay
getelementsbyclassname
xc8
audiounit
live-connect-sdk
smartmeter
fuzzy
snobol
erwin
launch-agent
mockery
nested-function
mailman
lzma
ratchet
ibm-jazz
scrapyd
octopress
8085
jseparator
nameerror
ssas-2008
gcdasyncsocket
akeneo
dynamicquery
centura
wpftoolkit
rot13
kindle-fire
apache-tomee
kiwi
rhodecode
mashup
println
gideros
struts2-tiles-plugin
slimv
dynamic-language-runtime
ruby-on-rails-plugins
array-formulas
jquery-textext
circusd
icarousel
fatwire
nsinteger
viewstack
system-design
mfi
ext-direct
actinic
tibco-gi
isaserver

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile