"If Me.ComboBox1.Value =" - Problem

Young Grasshopper

Board Regular
Joined
Dec 9, 2022
Messages
58
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello world,

I have this code to populate a ComboBox-list in a userform;
VBA Code:
Private Sub UserForm_Initialize()
Dim WS As Worksheet
Dim LastRow As Long
Dim aCell As Range

Set WS = ActiveSheet

With WS
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    
    For Each aCell In Worksheets("MacroData").Range("A1:A" & LastRow)
        If aCell.Value <> "" Then
            Me.ComboBox1.AddItem aCell.Value
        End If
    Next
End With
End Sub

And I am trying to write a code where VBA is calling a macro if the value in the combobox equals the value in specific cell, and this is what I have;
VBA Code:
Private Sub Testbtn_Click()
        If Me.ComboBox1.Value = Worksheets("Sheet1").Range("B11").Value Then
        Range("A1:X20").Calculate
        Call A_preview4
         End If
    If Me.ComboBox1.Value = Worksheets("Sheet1").Range("B12").Value Then
        Range("A1:X20").Calculate
        Call A_preview5
        End If


End Sub

But this produces nothing.. Any ideas to make it work or do I need to formulate it another way?
Would appreciate any help:)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
What is the value of Me.ComboBox1.Value when you execute Sub Testbtn_Click? What are the values in cells B11 and B12 at this time?

It looks like you have a button to run this. I assume you are expecting it to run only when you click the button, not some other time.
 
Upvote 0
Hi Jeff,

the value of Me.ComboBox1.Value is whatever I choose from the dropdownlist, which is populated from every nonblank cell in B11:B Range.

The content for B11 and B12 is just "Test1" and "test2" right now, but these are cells that is going to be changed all the time, and that's why want the values from B11:B to populate the combobox, and when I choose "test2" for example in the dropdown and hit the button (testbtn), Vba checks if the value of dropdown (test2) equals the value of b12 (test2) it calls the macro A_preview5. Right now i don't get any feedback from this.

If I change the code to
Excel Formula:
If Me.ComboBox1.Value = "test2"
it works well, but doesn't help if the value in b12 is something else next time I use the workbook.
 
Upvote 0
Found the solution..
If anyone else looks for the same answer, simply change ComboBox1.Value to ComboBox1.Text
 
Upvote 0
Solution

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top