Excel/VBA 以简化对特定 Excel 单元格的导航
Excel/VBA to Simplify Navigation to Specific Excel Cells
我有几个 Excel sheet,每个都包含志愿者的信息卡(约 150 个不同的名字)。我想创建一个仅包含名称的 navigation/summary sheet,它允许我 jump/go 直接到位于不同 sheet 内的特定志愿者名称(特定单元格)同一份文件。
在列表中选择名称或从摘要的下拉菜单中选择 sheet 都是很好的选择。
感谢我能得到的任何帮助。
这是我根据@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
将组合框添加到您的 sheet
1.激活开发人员选项卡
进入文件>选项>自定义功能区>勾选开发者 在列表中。
2。添加一个 ComboBox 到您的 sheet
在开发人员选项卡中进入 插入 > 组合框
然后根据需要调整大小和位置。
3。更改组合框的名称
您可以通过选择组合框并在文本框(屏幕上的红色矩形)中更改其名称来更改组合框的名称。
4。为您的 ComboBox
分配一个宏
右键单击您的组合框并分配宏...
会出现一个window。选择您的函数的名称(屏幕左侧的红色矩形),然后单击 New,如您在屏幕上所见。
您现在可以插入代码以在 ComboBox 的值更改时执行。
为您,在已创建的函数中插入以下代码。
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
用您的名字填充组合框
1.添加 Workbook_Open 函数以在启动工作簿时加载您的姓名
返回您的代码(Alt+F11 或“开发人员选项卡”>“查看代码”)并在我们为 ComboBox 添加的代码之后添加以下函数。
Private Sub Workbook_Open()
End Sub
这是在您的工作簿打开时触发的函数。
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"
中第一个 sheet 的名称。
更改 secondSheet = "Sheet2"
中第二个 sheet 的名称。
更改 colName = "A"
中列的值。
在 controlName = "PickList"
中更改 ComboBox 的名称。
我没有对代码做太多解释,因为它有注释,所以你必须能够理解,但如果有不清楚的地方,请不要介意在评论中提问。
我有几个 Excel sheet,每个都包含志愿者的信息卡(约 150 个不同的名字)。我想创建一个仅包含名称的 navigation/summary sheet,它允许我 jump/go 直接到位于不同 sheet 内的特定志愿者名称(特定单元格)同一份文件。 在列表中选择名称或从摘要的下拉菜单中选择 sheet 都是很好的选择。
感谢我能得到的任何帮助。
这是我根据@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
将组合框添加到您的 sheet
1.激活开发人员选项卡
进入文件>选项>自定义功能区>勾选开发者 在列表中。
2。添加一个 ComboBox 到您的 sheet
在开发人员选项卡中进入 插入 > 组合框
然后根据需要调整大小和位置。
3。更改组合框的名称
您可以通过选择组合框并在文本框(屏幕上的红色矩形)中更改其名称来更改组合框的名称。
4。为您的 ComboBox
分配一个宏右键单击您的组合框并分配宏...
会出现一个window。选择您的函数的名称(屏幕左侧的红色矩形),然后单击 New,如您在屏幕上所见。
您现在可以插入代码以在 ComboBox 的值更改时执行。
为您,在已创建的函数中插入以下代码。
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
用您的名字填充组合框
1.添加 Workbook_Open 函数以在启动工作簿时加载您的姓名
返回您的代码(Alt+F11 或“开发人员选项卡”>“查看代码”)并在我们为 ComboBox 添加的代码之后添加以下函数。
Private Sub Workbook_Open()
End Sub
这是在您的工作簿打开时触发的函数。
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"
中第一个 sheet 的名称。
更改 secondSheet = "Sheet2"
中第二个 sheet 的名称。
更改 colName = "A"
中列的值。
在 controlName = "PickList"
中更改 ComboBox 的名称。
我没有对代码做太多解释,因为它有注释,所以你必须能够理解,但如果有不清楚的地方,请不要介意在评论中提问。