using Variable in control names on user form

Florence1550

New Member
Joined
May 2, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I have a user Form and I want to cycle through the controls and only put the values to the sheet that are not empty but can I use for example below

for I = 1 to 8
If Not Me.Combox(i).value = vbNullString Then
next i
ws.Cells(lastrow, 1).value = Date
ws.Cells(lastrow, 2).value = me.txtname.Value
ws.Cells(lastrow, 4).value = me.txtWBS.Value
ws.Cells(lastrow, 5).value = me.txtRef.Value
ws.Cells(lastrow, 6).value = Out
ws.Cells(lastrow, 7).value = me.combox(i).value
ws.Cells(lastrow, 8).value = me.txtQty(i).Value
ws.Cells(lastrow, 9).value = me.txtRef.Value
End If

Many thanks for the time.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You need to use the controls collection:

VBA Code:
If Me.Controls("Combobox" & i).value <> vbNullString Then
 
Upvote 0
You need to use the controls collection:

VBA Code:
If Me.Controls("Combobox" & i).value <> vbNullString Then
Sorry to be a pain would it be as below I think i have the loops wrong? or a next or else missing

For i = 1 To 8
If Me.Controls("Combobox" & i).Value <> vbNullString Then
ws.Cells(lastRow, 1).Value = Date
ws.Cells(lastRow, 2).Value = Me.txtName.Value
ws.Cells(lastRow, 4).Value = Me.txtWBS.Value
ws.Cells(lastRow, 5).Value = Me.txtRef.Value
ws.Cells(lastRow, 6).Value = Out
ws.Cells(lastRow, 7).Value = Me.Controls("Combobox" & i).Value
ws.Cells(lastRow, 8).Value = Me.Controls("Textbox" & i).Value
ws.Cells(lastRow, 9).Value = Me.txtRef.Value
Next i
End If
 
Upvote 0
End If should be before Next i
 
Upvote 0
Many thanks
Many thanks
Coming up with error on
1714567225492.png
1714567275795.png
 
Upvote 0
What are the actual names of the controls? I just noticed you had Combox1 in your original code while I assumed Combobox1 etc.
 
Upvote 0

Forum statistics

Threads
1,216,222
Messages
6,129,586
Members
449,520
Latest member
TBFrieds

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