Excel / VBA简化导航到特定的Excel单元格
我有几个Excel表格,每个表格都包含志愿者的信息卡(约150个不同的名字)。 我想创build一个仅包含名称的导航/汇总表,它允许我直接跳转/直接find位于同一文档中不同表单上的特定志愿者姓名(特定单元格)。 在列表中select一个名字或从汇总表的下拉菜单中select两个都是很好的select。
感谢任何帮助,我可以得到。
这里是我使用的基于@Teasel指令的代码:
Sub ComboBox_Change() Dim Sheet1 As String Dim Sheet2 As String Dim A As String Dim PickList As String listNamesSheet = "Name of your sheet where names are" secondSheet = "Name of your sheet, where the control is" colName = "Header of the column where names are" controlName = "Name of your combobox" With ThisWorkbook.Sheets(listNamesSheet) 'Go through the range where names are For Each nameInSheet In .Range(colName & "1:" & colName & .Cells(.Rows.Count, colName).End(xlUp).Row) 'If the name is the one selected in the ComboBox If (nameInSheet = ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).List(ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).Value)) Then 'Activate the first sheet (have to if you want to select a range inside it) .Activate 'Select the case where the name has been found .Range(nameInSheet.Address).Select End If Next nameInSheet End With End Sub Private Sub Workbook_Open() Dim Sheet1 As String Dim Sheet2 As String Dim A As String Dim PickList As String listNamesSheet = "Name of the sheet where names are" secondSheet = "Name of the sheet where you ComboBox is" colName = "Header of the column where names are" 'ex: "A" if names are in column A controlName = "Name of your combobox" With ThisWorkbook.Sheets(listNamesSheet) 'Go through the range where all the names are For Each nameInSheet In .Range(colName & "1:" & colName & .Cells(.Rows.Count, colName).End(xlUp).Row) 'Add it to the ComboBox ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).AddItem nameInSheet Next nameInSheet End With End Sub
将一个combobox添加到您的工作表
1.激活“开发人员”选项卡
转到列表中的文件 > 选项 > 自定义function区 >检查开发人员 。
2.添加一个combobox到你的工作表
在开发工具栏中,进入插入 > combobox
然后resize,然后放置它,但是你想要的。
3.更改您的ComboBox的名称
您可以通过select它并在文本框(屏幕上的红色矩形)中更改他的名称来更改combobox的名称。
4.分配一个macros到你的combobox
右键点击你的ComboBox并指定macros…
将出现一个窗口。 select你的function的名称(左边的红色矩形在屏幕上),然后点击新的 ,你可以看到在屏幕上。
您现在可以插入代码来执行combobox的值更改时执行。
对于你来说,将下面的代码插入已创build的函数中。
Dim listNamesSheet As String Dim secondSheet As String Dim colName As String Dim controlName As String listNamesSheet = "Sheet1" secondSheet = "Sheet2" colName = "A" controlName = "PickList" With ThisWorkbook.Sheets(listNamesSheet) 'Go through the range where names are For Each nameInSheet In .Range(colName & "1:" & colName & .Cells(.Rows.Count, colName).End(xlUp).Row) 'If the name is the one selected in the ComboBox If (nameInSheet = ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).List(ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).Value)) Then 'Activate the first sheet (have to if you want to select a range inside it) .Activate 'Select the case where the name has been found .Range(nameInSheet.Address).Select End If Next nameInSheet End With
用你的名字填充你的ComboBox
1.添加Workbook_Open函数以在启动工作簿时加载您的名称
回到您的代码(Alt + F11或在开发人员标签>查看代码),并添加以下函数的代码,我们添加到combobox。
Private Sub Workbook_Open() End Sub
这是您的工作簿打开时触发的function。
2.填充你的combobox
将此代码添加到该函数。
Dim listNamesSheet As String Dim secondSheet As String Dim controlName As String Dim colName As String listNamesSheet = "Sheet1" secondSheet = "Sheet2" colName = "A" 'ex: "A" if names are in column A controlName = "PickList" With ThisWorkbook.Sheets(listNamesSheet) 'Clear before adding new names For i = ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).ListCount To 1 Step -1 ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).RemoveItem i Next i 'Go through the range where all the names are For Each nameInSheet In .Range(colName & "1:" & colName & .Cells(.Rows.Count, colName).End(xlUp).Row) 'Add it to the ComboBox ThisWorkbook.Sheets(secondSheet).DropDowns(controlName).AddItem nameInSheet Next nameInSheet End With
调整你的代码
在listNamesSheet = "Sheet1"
更改第一张工作表的名称。
在secondSheet = "Sheet2"
更改第二个工作表的名称。
更改colName = "A"
列的值。
在controlName = "PickList"
更改ComboBox的名称。
我没有解释太多的代码,因为它的评论,所以你必须能够理解,但不要介意在评论中询问是否不清楚。