VBA宏,可以查找并滚动到具有所选值的ComboBox中的单元格。

huangapple go评论104阅读模式
英文:

VBA Macro that can find and scroll to cell with selected value in a ComboBox

问题

  1. 我是VBA新手,无法弄清楚如何编写Excel VBA代码,以便根据我从下拉框中选择的内容,在我的工作表中查找并滚动到一个单元格。
  2. 为了提供背景,我有一个包含Sheet1Sheet2的工作簿。Sheet1包含多个不同的品牌(行),我在Sheet1的单元格A2中有一个下拉框,用于选择我想要的品牌。我希望我的宏根据我在下拉框中选择的品牌,在Sheet1中滚动到我的品牌单元格,方法是在Sheet1B列(我的品牌单元格)中找到所选的品牌。
  3. 我之前尝试过编写这段代码,但无法提取我在下拉框中选择的值,所以我做的是在Sheet2中创建一个品牌及其相应的单元格链接值的表格,并通过在表格中找到单元格链接值来提取下拉框的值。
  4. ```vba
  5. Sub Test()
  6. Dim celllinkval
  7. Dim brand As String
  8. celllinkval = ThisWorkbook.Sheets("Sheet2").Range("C1")
  9. brand = Application.WorksheetFunction.VLookup(celllinkval, Sheet2.Range("A1:B3"), 1, False)
  10. Dim cell As Range
  11. Dim Rng As Range
  12. Set Rng = ThisWorkbook.Sheets("Sheet1").Columns("B:B")
  13. Set cell = Rng.Find(What:=brand, LookIn:=xlFormulas, LookAt:=xlWhole)
  14. If Not cell Is Nothing Then Application.Goto cell, True
  15. End Sub

这是我当前的代码,但无法使其工作,真的很感谢任何帮助!谢谢

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;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).
  4. 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&#39;ve selected in my ComboBox, by finding the brand selected in Column B (my brand cell) of Sheet1.
  5. I&#39;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

  1. this is my current code which I can&#39;t get to work, would really appreciate any help! Thank you
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 以下是您要翻译的代码部分:
  6. ```vba
  7. 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`:
  8. Sub Test()
  9. Dim brRow As Long
  10. Dim cbValue As String
  11. Dim cell As Range
  12. Dim ws As Worksheet
  13. Set ws = ActiveWorkbook.Sheets("Sheet1")
  14. With ws
  15. cbValue = ComboBox1.Value
  16. brRow = Application.WorksheetFunction.Match(cbValue, .Range("B:B"), 0)
  17. 'find the row to set the cell to
  18. Set cell = .Range("B" & brRow)
  19. End With
  20. If Not cell Is Nothing Then Application.Goto cell, True
  21. End Sub
  22. 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:

  1. Sub Test()
  2. Dim brRow As Long
  3. Dim cbValue As String
  4. Dim cell As Range
  5. Dim ws As Worksheet
  6. Set ws = ActiveWorkbook.Sheets(&quot;Sheet1&quot;)
  7. With ws
  8. cbValue = ComboBox1.Value
  9. brRow = Application.WorksheetFunction.Match(cbValue, .Range(&quot;B:B&quot;), 0)
  10. &#39;find the row to set the cell to
  11. Set cell = .Range(&quot;B&quot; &amp; brRow)
  12. End With
  13. If Not cell Is Nothing Then Application.Goto cell, True
  14. End Sub

Hope this helps VBA宏,可以查找并滚动到具有所选值的ComboBox中的单元格。

huangapple
  • 本文由 发表于 2023年3月1日 16:09:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定