Excel VBA Macro not working

mcl48236

New Member
Joined
Jul 7, 2016
Messages
4
Hi All, I am not the best at VBA but am trying to get my macro to work. I have created a combobox drop down list in the developer tab. I want to be able to get the data selected in the combo box into another cell in my workbook, however I am having issues. This is what I have come up with so far

Option Explicit


Sub FilterUniqueData()
Dim Lrow As Long, test As New Collection
Dim Value As Variant, temp() As Variant
ReDim temp(0)

On Error Resume Next
With Worksheets("Sheet1")
Lrow = .Range("A" & Rows.Count).End(xlUp).Row
temp = .Range("A2:A" & Lrow).Value
End With

For Each Value In temp
If Len(Value) > 0 Then test.Add Value, CStr(Value)
Next Value

ReDim temp(0)
Worksheets("Sheet1").Shapes("Drop Down 5").ControlFormat.RemoveAllItems

For Each Value In test
Worksheets("Sheet1").Shapes("Drop Down 5").ControlFormat.AddItem Value
Next Value

Set test = Nothing


End Sub


Sub SelectedValue()
With Worksheets("Sheet1").Shapes("Drop Down 5").ControlFormat
Worksheets("Sheet1").Range("C35") = .List(.Value)
End With
End Sub


but when I go run the macro the cell only populates when I got back into VBA and continue running the selected value part of the code.

Any insights into this would be greatly appreciated!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi mcl48236,

I have the following suggestion to solve your VBA issue.

First, create a ComboBox5 (AxtiveX Controls) instead of DropDown5 (Form Controls).

Then, paste the following code and let me know if this is what you want.

Sub ComboBox5_DropButt*******()
Dim Lrow As Long, test As New Collection
Dim Value As Variant, temp() As Variant
ReDim temp(0)


On Error Resume Next
With Worksheets("Sheet1")
Lrow = .Range("A" & Rows.count).End(xlUp).Row
temp = .Range("A2:A" & Lrow).Value
End With


For Each Value In temp
If Len(Value) > 0 Then test.Add Value, CStr(Value)
Next Value


ComboBox5.Clear
For Each Value In test
ComboBox5.AddItem Value
Next Value


Set test = Nothing


End Sub


Sub ComboBox5_Click()
Worksheets("Sheet1").Range("C35") = ComboBox5.Text
End Sub
 
Upvote 0
Hi mcl48236,

I have the following suggestion to solve your VBA issue.

First, create a ComboBox5 (AxtiveX Controls) instead of DropDown5 (Form Controls).

Then, paste the following code and let me know if this is what you want.

Sub ComboBox5_DropButt*******()
Dim Lrow As Long, test As New Collection
Dim Value As Variant, temp() As Variant
ReDim temp(0)


On Error Resume Next
With Worksheets("Sheet1")
Lrow = .Range("A" & Rows.count).End(xlUp).Row
temp = .Range("A2:A" & Lrow).Value
End With


For Each Value In temp
If Len(Value) > 0 Then test.Add Value, CStr(Value)
Next Value


ComboBox5.Clear
For Each Value In test
ComboBox5.AddItem Value
Next Value


Set test = Nothing


End Sub


Sub ComboBox5_Click()
Worksheets("Sheet1").Range("C35") = ComboBox5.Text
End Sub

Thank you so much for your response!
I now get a compile error: expect end of statement. Any thoughts?
 
Upvote 0
Have you assigned a macro to the dropdown or set the Linked Cell for it?
 
Upvote 0
Remove the asterisks from this line

Code:
Sub ComboBox5_DropButt*******()
 
Upvote 0
Michael

I think it's the board that adds them for some strange reason.

PS Having * in a sub/function name isn't allowed.
 
Upvote 0
Hi Norie
I've never seen the added ******* before !
I was aware of them not being allowed, hence my suggestion.
 
Upvote 0
One of the automatic replacements in the forum is "o n c l i c k" (without the spaces). Presumably the code was
ComboBox5_D r o p B u t t o n c l i c k (without the spaces)
 
Upvote 0
Thanks Peter
I wasn't aware of that, and to be honest, have never seen it before !
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,659
Members
449,462
Latest member
Chislobog

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