VBA variable object names

nick1408

Board Regular
Joined
Jan 18, 2010
Messages
82
Not sure if this is possible.

I have a userform with several line items. The text fields on each line carry names like Title1, Edition1, Title2, Edition2 etc. I want to call sub that will change the number in the field name. I tried the following:

VBA Code:
dim lineNumber as string
lineNumber = 1
Call CreateForm

lineNumber = 2
CallCreateForm

sub CreateForm()

if Title & lineNumber.Value <> "" then

' code here

end if

end sub

I also tried "Title" & lineNumber but had the issue that the object name isn't seen as Title1, Title2 etc. Is there a way to have variable field names to run the VBA code?

Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
All good now - I have this working another way:

VBA Code:
dim lineNumber as string
lineNumber = 1
Call CreateForm

lineNumber = 2
CallCreateForm

sub CreateForm()

fieldName = "Title" & lineNumber
nextField = "Edition" & lineNumber

if Title & fieldName.Value <> "" then

' code here

end if

end sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,474
Messages
6,125,024
Members
449,204
Latest member
LKN2GO

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