提示用户select工作表
我有一个macros,提示用户select一个Excel文件,如下所示:
Dim thisBook As Workbook, newBook As Workbook Dim fd As FileDialog Dim oFD As Variant Dim fileName As String Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .ButtonName = "Select" .AllowMultiSelect = False .Filters.Add "Excel Files", "*.xlsx; *.xls", 1 .Title = "Choose the Report" .InitialView = msoFileDialogViewDetails .Show For Each oFD In .SelectedItems fileName = oFD Next oFD On Error GoTo 0 End With If fd.SelectedItems.Count = 0 Then Exit Sub End If Set thisBook = ActiveWorkbook Set newBook = Workbooks.Open(fileName)
这工作正常,我现在想做什么,我没有在互联网上find以下内容:
我想提示用户从新手册中select一个工作表,因为表格名称将来可能不会相同。
我想出了这个,但是我不是很满意,因为让用户input表名是相当不方便的:
Function WorksheetExists(WSName As String) As Boolean On Error Resume Next WorksheetExists = Worksheets(WSName).Name = WSName On Error GoTo 0 End Function Function q() As String Dim shname As String Do Until WorksheetExists(shname) shname = InputBox("Enter sheet name") If Not WorksheetExists(shname) Then MsgBox shname & " doesn't exist!", vbExclamation Loop q = shname End Sub
有没有办法,也许让用户从所有图纸名称中select图纸名称? (我没有使用用户窗体,macros启动,如果用户点击一个button)
用一个空白的ListBox做一个用户窗体,并在用户窗体模块中使用这个代码
Private Sub UserForm_Initialize() Dim sh As Worksheet For Each sh In ActiveWorkbook.Sheets ListBox1.AddItem sh.Name Next sh End Sub Private Sub ListBox1_Click() Sheets(ListBox1.Value).Activate Unload Me End Sub