英文:
VBA Macro that can find and scroll to cell with selected value in a ComboBox
问题
我是VBA新手,无法弄清楚如何编写Excel VBA代码,以便根据我从下拉框中选择的内容,在我的工作表中查找并滚动到一个单元格。
为了提供背景,我有一个包含Sheet1和Sheet2的工作簿。Sheet1包含多个不同的品牌(行),我在Sheet1的单元格A2中有一个下拉框,用于选择我想要的品牌。我希望我的宏根据我在下拉框中选择的品牌,在Sheet1中滚动到我的品牌单元格,方法是在Sheet1的B列(我的品牌单元格)中找到所选的品牌。
我之前尝试过编写这段代码,但无法提取我在下拉框中选择的值,所以我做的是在Sheet2中创建一个品牌及其相应的单元格链接值的表格,并通过在表格中找到单元格链接值来提取下拉框的值。
```vba
Sub Test()
Dim celllinkval
Dim brand As String
celllinkval = ThisWorkbook.Sheets("Sheet2").Range("C1")
brand = Application.WorksheetFunction.VLookup(celllinkval, Sheet2.Range("A1:B3"), 1, False)
Dim cell As Range
Dim Rng As Range
Set Rng = ThisWorkbook.Sheets("Sheet1").Columns("B:B")
Set cell = Rng.Find(What:=brand, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not cell Is Nothing Then Application.Goto cell, True
End Sub
这是我当前的代码,但无法使其工作,真的很感谢任何帮助!谢谢
<details>
<summary>英文:</summary>
I'm new to VBA and cannot figure out how to code for Excel VBA to find and scroll to a cell in my worksheet based on what I have selected from my ComboBox (or Drop down).
For context, I have a Workbook with Sheet1 and Sheet2. Sheet1 contains multiple different brands (rows) and I have a ComboBox in Sheet1 Cell A2 to select what brand I want. I want my Macro to scroll to my brand cell in Sheet1 based on the brand I've selected in my ComboBox, by finding the brand selected in Column B (my brand cell) of Sheet1.
I've tried coding this before but could not extract the value selected in my ComboBox, so what I did instead is to create a table of brands and its corresponding cellink value in Sheet2 and extract the value of the ComboBox by finding the cell link value in the table instead.
Sub Test()
Dim celllinkval
Dim brand As String
celllinkval = ThisWorkbook.Sheets("Sheet2").Range("C1")
brand = Application.WorksheetFunction.VLookup(celllinkval, Sheet2.Range("A1:B3"), 1, False)
Dim cell As Range
Dim Rng As Range
Set Rng = ThisWorkbook.Sheets("Sheet1").Columns("B:B")
Set cell = Rng.Find(What:=brand, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not cell Is Nothing Then Application.Goto cell, True
End Sub
this is my current code which I can't get to work, would really appreciate any help! Thank you
</details>
# 答案1
**得分**: 0
以下是您要翻译的代码部分:
```vba
Not sure if you made a typo when trying to get the value of your combobox but this worked fine for me with the combobox named `ComboBox1`:
Sub Test()
Dim brRow As Long
Dim cbValue As String
Dim cell As Range
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
With ws
cbValue = ComboBox1.Value
brRow = Application.WorksheetFunction.Match(cbValue, .Range("B:B"), 0)
'find the row to set the cell to
Set cell = .Range("B" & brRow)
End With
If Not cell Is Nothing Then Application.Goto cell, True
End Sub
Hope this helps :)
英文:
Not sure if you made a typo when trying to get the value of your combobox but this worked fine for me with the combobox named ComboBox1
:
Sub Test()
Dim brRow As Long
Dim cbValue As String
Dim cell As Range
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
With ws
cbValue = ComboBox1.Value
brRow = Application.WorksheetFunction.Match(cbValue, .Range("B:B"), 0)
'find the row to set the cell to
Set cell = .Range("B" & brRow)
End With
If Not cell Is Nothing Then Application.Goto cell, True
End Sub
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论