Excel Macro with Combo Box

Windows XP
Excel 2003

I am trying to create a macro that will take the seletion made in a combobox and add it to whatever is in a cloumn. If the column is empty then nothing happens.

For Example:

ComboBox Entry "Test"

ID (Column A) - Before
1234
4567
7645
9999
empty cell
123456

ID (Column A) - After
1234 Test
4567 Test
7645 Test
9999 Test
empty cell
123456 Test

Please let me know if any further information is needed.
[530 byte] By [computerman29651] at [2007-11-20 11:25:51]
# 1 Re: Excel Macro with Combo Box
This is a very basic example but it should give you an idea of how this can be accomplished. The example limits the update to the first 100 rows of column A.

Private Sub ComboBox1_AfterUpdate()
Dim Add_Text As String
Dim i As Integer

Add_Text = ComboBox1.SelText

For i = 1 To 100
If Sheet1.Cells(i, "A").Text <> "" Then
Sheet1.Cells(i, "A").Value = Sheet1.Cells(i, "A").Text + " " + Add_Text
End If
Next I
End Sub
mnopups at 2007-11-10 3:08:18 >
# 2 Re: Excel Macro with Combo Box
mnopups, that seems to work, but how can I keep the code from doubling up?

For Example,

ComboBox Text = "Test'

ID
12345 Test Test
12345 Test Test
12345 Test Test
computerman29651 at 2007-11-10 3:09:18 >
# 3 Re: Excel Macro with Combo Box
Do you mean that you want to prevent duplicate text? Will you also need to add additional text such as:

12345 Test Passed?

Or will you only be adding a single value?
mnopups at 2007-11-10 3:10:19 >
# 4 Re: Excel Macro with Combo Box
That would be correct...I want to stop duplicate text.

Once I have added the ComboBox Text to the ID, I do not want it to be able to be added again.

I will only be adding a single value.
computerman29651 at 2007-11-10 3:11:24 >
# 5 Re: Excel Macro with Combo Box
Ok, try this:

Public Class Form1
Dim blnUpdated As Boolean = False

Private Sub ComboBox1_AfterUpdate()
Dim Add_Text As String
Dim i As Integer

If blnUpdated Then
Exit Sub
End If

Add_Text = ComboBox1.SelText

For i = 1 To 100
If Sheet1.Cells(i, "A").Text <> "" Then
Sheet1.Cells(i, "A").Value = Sheet1.Cells(i, "A").Text + " " + Add_Text
End If
Next I
blnUpdated = True
End Sub
mnopups at 2007-11-10 3:12:18 >
# 6 Re: Excel Macro with Combo Box
When I try adding the following code:

Public Class Form1
Dim blnUpdated As Boolean = False

It displays in red and gives me an error message "End of Statement Expected."
computerman29651 at 2007-11-10 3:13:17 >
# 7 Re: Excel Macro with Combo Box
Sorry, the "Public Class" statement assumes that you have a form called Form1 in your program. If you do, then add "End Class" at the very last line of the program (after the "End Sub" statement).

However, I am going to assume that you already have a class definition so what you really need to do is just add the Dim statement for blnUpdated to a public area of your program (usually immediately after the first statement in the program).
mnopups at 2007-11-10 3:14:21 >
# 8 Re: Excel Macro with Combo Box
Duplicate values is still happening.

Here is what the code:

Option Explicit

Public Sub cmdAdd_Click()

Dim Add_Date As String
Dim Add_Text As String
Dim i As Integer
Dim blnUpdated As Boolean

blnUpdated = False

If blnUpdated Then
Exit Sub
End If

Add_Text = ComboBox1.SelText
Add_Date = txtdate.Text

For i = 1 To 100
If ActiveSheet.Cells(i, "A").Text <> "" Then
ActiveSheet.Cells(i, "A").Value = ActiveSheet.Cells(i, "A").Text + " " + Add_Text
End If
Next i
blnUpdated = True

Unload Me
End Sub
computerman29651 at 2007-11-10 3:15:24 >
# 9 Re: Excel Macro with Combo Box
Is this the entire piece of code? What confuses me is the "Unload Me" statement which implies that there is a form or a dialog that has been displayed.

The reason that you keep getting duplicate code is that the subroutine begins each time by setting blnUpdate to False. Therefore, the If statement that checks its value will never evaluate to True. This allows the subroutine to alter the cell contents repeatedly. You must declare and set blnUpdate outside of the cmdAdd_Click subroutine. This means that blnUpdate must be declared as a public variable.
mnopups at 2007-11-10 3:16:27 >
# 10 Re: Excel Macro with Combo Box
The ComboBox is located on a UserForm. I will give your advice a shot, and see if I can get it to work.

I appreciate all the help.
computerman29651 at 2007-11-10 3:17:28 >
# 11 Re: Excel Macro with Combo Box
Ahh! If you are closing the form, then declaring the blnUpdate variable publicly will not work either because it will be reset to False each time the form is reloaded. The only thing I can think of in this case would be to use an empty cell (perhaps hidden) in the workbook in place of blnUpdate.

Assuming that cell B1 is empty, you could try something like this:

Option Explicit

Public Sub cmdAdd_Click()

Dim Add_Date As String
Dim Add_Text As String
Dim i As Integer

If Sheet1.Cells(1, "B").Text = "1" Then
Exit Sub
End If

Add_Text = ComboBox1.SelText
Add_Date = txtdate.Text

For i = 1 To 100
If ActiveSheet.Cells(i, "A").Text <> "" Then
ActiveSheet.Cells(i, "A").Value = ActiveSheet.Cells(i, "A").Text + " " + Add_Text
End If
Next i
Sheet1.Cells(1, "B").Value = "1"

Unload Me
End Sub
mnopups at 2007-11-10 3:18:28 >
# 12 Re: Excel Macro with Combo Box
mnopups,

I believe that has solved my problem.

Thank you very much.
computerman29651 at 2007-11-10 3:19:26 >
# 13 Re: Excel Macro with Combo Box
Always happy to help.
mnopups at 2007-11-10 3:20:33 >