I have the follow macro for a button click. When I run it, I hit a snag at the Label caption line with an 'object required' error. The names of the objects are correct.
Sub Button1_Click()
Dim objFileDialog As Object
Dim objSelectedFile As Variant
Set objFileDialog = Application.FileDialog(3)
With objFileDialog
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel File", "*.*", 1
.Title = "Select Input file"
.Show
For Each objSelectedFile In .SelectedItems
Label2.Caption = objSelectedFile
Next
End With
End Sub
Related
I would like to write a program in VBA.
I would like a window to pop up when the program starts, where I can bring up the file manager with a button and the elements what are (multiple)selected here, would be placed in a textbox under each other and excel would open them all at once.
Now I finished the useform with one button and one textbox, and have a code for the button, to open the file manager and enabled the multiselect. Whats next? Anybody can help me?
Private Sub CommandButton1_Click()
Dim fldlg As FileDialog
Set fldlg = Application.FileDialog(msoFileDialogOpen)
With fldlg
.AllowMultiSelect = True
.Title = "Find"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel", "*.xls,*.xlsx"
End With
rv = fldlg.Show
If rv Then
MsgBox fldlg.SelectedItems(1)
End If
End Sub
To open all the files (or build a string listing them all), you need to cycle through the SelectedItems collection and use each in turn:
Dim fldlg As FileDialog
Set fldlg = Application.FileDialog(msoFileDialogOpen)
Dim strItems
With fldlg
.AllowMultiSelect = True
.Title = "Find"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel", "*.xls,*.xlsx"
rv = .Show
If rv Then
'create a string of text for output using MsgBox
For Each SelectedItem In .SelectedItems
strItems = strItems & SelectedItem & Chr(13)
Next
'display string
MsgBox strItems
'open the files
For Each SelectedItem In .SelectedItems
Workbooks.Open SelectedItem
Next
End If
End With
I use the VBA code below to open a workbook but I get a user-prompt when the workbook is already-opened by another user asking if I would like to open in read-only mode.
I'd like to open the workbook in read-only mode directly without the read-only user prompt.
Sub Report()
Dim fd As FileDialog
Dim Filechosen As Boolean
Dim Savebutton As Boolean
Dim sh As Worksheet
Dim book As Workbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
fd.Filters.Add "Old Excel Files", "*.xls"
fd.Filters.Add "New Excel Files", "*.xlsx"
fd.Filters.Add "macro Excel Files", "*.xlsm"
fd.Filters.Add "any Excel Files", "*.xl*"
fd.FilterIndex = 4
fd.AllowMultiSelect = False
fd.InitialFileName = "https:sharepoint address/BFs/"
Filechosen = fd.Show
If Not Filechosen Then
MsgBox " No File Selected"
Exit Sub
End If
fd.Execute
For Each sh In Worksheets: sh.Visible = True
Next sh
End Sub
When you are giving the command to open the file, it has an optional command to open it in readonly
Just mention it as Readonly:=True
It will not prompt the user
Let me know if you need further clarification
You can open the workbook yourself using the VBA workbooks.open method once you get the filename from the dialog.
Replace in your code as needed. You'll get the idea
Dim xlFileName As String
Dim wb as Workbook
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show Then
xlFileName = .SelectedItems(1)
Else
MsgBox " No File Selected"
Exit Sub
End If
End With
wb.Workbooks.Open (xlFileName), ReadOnly:=True
You can then copy the worksheets in that workbook from the wb.worksheets collection.
I am trying to show the path of a file I select from a dialog box, in a text box.
Sub SelectMonthlyFile()
Dim Data As Office.FileDialog
Set Data = Application.FileDialog(msoFileDialogFilePicker)
With Data
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
If .Show = True Then
Path = .SelectedItems(1)
End If
End With
End Sub
I show an error at Path = .SelectedItems(1).
variable is not defined
I named my cell Path and assumed that this would get the file path copied and pasted here.
I suggest you rename Data to something more meaningful. I've changed it to just FileDialog. The reason for your error is that the code has no idea what Path is. Note that I omitted the clearing of the filter, it is not needed.
Option Explicit 'always have this
Dim strPath As String 'explicitly declare your variable
Sub SelectMonthlyFile()
Dim FileDialog As Office.FileDialog
Set FileDialog = Application.FileDialog(msoFileDialogFilePicker)
With FileDialog
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
'was anything selected?
If .Show <> 0 Then
strPath = .SelectedItems(1)
Debug.Print strPath
End If
End With
End Sub
I get all the time runtime error 9, “subscript out of range” . Dont know how to do this. I get the error by the last line of code below.
Dim Sheetstarybpm As Worksheet
Dim countrowsoldbpm1 As Long
Dim rng2 As Range
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select BPM Report for previous month"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
oldbpm = .SelectedItems.Item(1)
End With
Workbooks.Open oldbpm
Set Sheetstarybpm = ActiveSheet
countrowsoldbpm1 = Range("a10", Range("a10").End(xlDown)).Rows.Count
Set myrange = Range("A10:CW" & countrowsoldbpm1)
Set rng2 = Workbooks(oldbpm).Worksheets(Sheetstarybpm).Range(myrange)
You are using the object methods incorrectly. Once you have stored the Sheetstarybpm you can use this further... So when opening the workbook store this as a workbook object so you can reference it later on, if needed. Additionally, when setting your worksheet object you can now explicitly reference your workbook because you have stored it (much better coding practice). It is also recommended to explicitly define your sheet object references when setting your ranges. Please see amended code below.
Dim Sheetstarybpm As Worksheet
Dim countrowsoldbpm1 As Long
Dim myWB As Workbook
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select BPM Report for previous month"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
oldbpm = .SelectedItems.Item(1)
End With
Set myWB = Workbooks.Open(oldbpm)
Set Sheetstarybpm = myWB.ActiveSheet
countrowsoldbpm1 = Sheetstarybpm.Range("a10", Range("a10").End(xlDown)).Rows.Count
Set myrange = Sheetstarybpm.Range("A10:CW" & countrowsoldbpm1)
Edit: Removed rng2 from the code because it is the same as myrange, as noted by OP in comments.
By the line
Set srchrange = Workbooks(wipreport).Worksheets("1. WIP report").Range("B15:B")
I got the error Subscript out of range. wipreport is chosen above from the file. The workbook for sure has the Sheet "1. WIP report". I copied it. Workbooks BPM-Tool is already opened but I have no error by this line of code.
I can not even try if vlookup works.
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select WIP Report"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
wipreport = .SelectedItems.Item(1)
End With
Workbooks.Open wipreport
Dim lookFor As Range
Dim srchrange As Range
Set lookFor = Workbooks("BPM-Tool.xlsm").Worksheets("BPM-Report").Cells(10, 2)
Set srchrange = Workbooks(wipreport).Worksheets("1. WIP report").Range("B15:B")
lookFor.Offset(0, 317).Value = Application.VLookup(lookFor, srchrange, 18, False)
Workbooks.Open takes the full file name e.g. C:\temp\myfile.xlsx
Workbooks() takes file name only e.g. myfile.xlsx
You can use this code to get the workbook object and then use it:
' Get the workbook object
Dim wk As Workbook
Set wk = Workbooks.Open(wipreport)
' Use the workbook object
wk.Worksheets("1. WIP report").Range ("B15:B")
Try this:
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select WIP Report"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
wipreport = .SelectedItems.Item(1)
End With
Dim wb As Workbook
Set wb = Workbooks.Open(wipreport)
Dim lookFor As Range
Dim srchrange As Range
Set lookFor = Workbooks("BPM-Tool.xlsm").Worksheets("BPM-Report").Cells(10, 2)
Set srchrange = wb.Worksheets("1. WIP report").Range("B15:B1000")
lookFor.Offset(0, 317).Value = Application.VLookup(lookFor, srchrange, 18, False)
wipreport is a String not a Workbook object, that's why you are getting an error.