Find cell location based on value then do something- User Forms VBA Excel

泪湿孤枕 提交于 2019-12-04 06:24:29

问题


i have experience in programing, however, I am new to VBA. I have a user form that i am working on. This form has a Combo Box that has a list initialized to it. What i am trying to do is:

*Get the ID Number value inputted by the user from the ComboBox *Take the value inputted by the user and find its match using a range of values from a worksheet (i.e. Worksheet.Range("ID_Number_List")) *Once it obtains it's match get the location of the cell that it matches * Off set the location of the cell by one column to get the Name that relates to the ID Number(Same Row) to set it to textBoxName.Value *Off set it two columns to get the telefone number that relates to the ID Number and set it to textboxTele.value

I want this to happen as soon as a value is selected from the Combobox, so my question is does my code go in the combo box or does it go to the next text box? so as soon as the person tabs over to the next text box the code is automatically execute. i would like the code to fully execute without tabing over to the next box.

This code is not complete but here is what i have (i didnt add the off set part i just did a test execution):

Dim ORIValue As String 
'get value from combo_box Set 
ORIValue = COMBO_ORILIST.Value

Dim cLoc As Range 
Dim cORIVal As Range

'worksheet with the ID information Dim ORISheetList As Worksheet 
Set ORISheetList = Worksheets("ORI_LIST")

'
For Each cLoc In ORISheetList.Range("ORI_LIST")
'compare the input string from list- considering using Match function for this
If StrComp(cLoc, ORIValue,  vbTextCompare) Then TextBAgencyName.Value = "test"
Else: Next cLoc
End If

Let me know what you think. If i have to rewrite everything i will.


回答1:


Your code doesn't compile.

If you have a userform with a single combobox called ComboBox1, you need to put your cell-finding code in the form code as follows:

Private Sub ComboBox1_Change()
    MsgBox "yep, this is where the code should go"
End Sub

I suspect using the rowsource property of the combobox in combination with the index of the selected value you probably don't need to actually perform a search for the selected value. Something like this might work:

Private Sub ComboBox1_Change()
    MsgBox Range(ComboBox1.RowSource).Cells(ComboBox1.ListIndex + 1)
End Sub

Hope that helps.



来源:https://stackoverflow.com/questions/11337413/find-cell-location-based-on-value-then-do-something-user-forms-vba-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!