Excel/data userform

Advertisement


Question
QUESTION: ok i found the Initialize event, and placed this code exactly as it shown

and have a command to pull up the useform1 and it doesnt pull up unless i put in userform1_Initialize() on the first line, but all that comes up is an error and the first line is hightlighted in yellow...and error says compiled error For without Next.....


yes by the way i will have 18 labels but was testing it to see if it worked with just two and not sure how i will do I5:i10 and J5:j10 will have to repeat this code three different times, i am sure i have done something wrong....


Private Sub Label1_Click()
Label1.Caption = ActiveCell.Text
End Sub

Private Sub Label2_Click()
Label2.Caption = ActiveCell.Text
End Sub

Private Sub UserForm_Initialize()
Dim sh As Worksheet, r As Range, r1 As Range
Dim k As Long, i As Long, cell As Range
Set sh = Worksheets("CustomerData")
Set r = sh.Range("H5:H10")
k = 1
For i = 0 To 2
Set r1 = r.Offset(0, i)
For Each cell In r1
 UserForm1.Controls("Label" & k).Value = cell.Text
 k = k + 1
Next
End Sub

i feel its close...

Userform
Userform  
ANSWER: Anthony,

A couple of typos in the code.  Sorry.

I have put this routine in a userform:

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim sh As Worksheet, r As Range, r1 As Range
Dim k As Long, i As Long, cell As Range
Set sh = Worksheets("CustomerData")
Set r = sh.Range("H5:H10")
k = 1
For i = 0 To 2
Set r1 = r.Offset(0, i)
For Each cell In r1
 UserForm1.Controls("Label" & k).Caption = cell.Text
 k = k + 1
Next
Next
End Sub


the i = 0 to 2  loops over column H, then column I then column J, so you don't need to do anything but show the useform.   I have attached a picture of my results using your sample data (duplicated into column I and J as well.

I have label1 in the upper left of the userform and label18 in the lower right.  layout matches the layout of cells H5:J10



--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Tom

thats exactly how i want it to show up like your pic....i entered the code below as you wrote.....

and i have a macro to unload UserForm
but it comes up and error saying parameters are wrong....


Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim sh As Worksheet, r As Range, r1 As Range
Dim k As Long, i As Long, cell As Range
Set sh = Worksheets("CustomerData")
Set r = sh.Range("H5:H10")
k = 1
For i = 0 To 2
Set r1 = r.Offset(0, i)
For Each cell In r1
UserForm1.Controls("Label" & k).Caption = cell.Text
k = k + 1
Next
Next
End Sub

Private Sub Label1_Click()
Label1.Caption = ActiveCell.Text
End Sub

Private Sub Label2_Click()
Label2.Caption = ActiveCell.Text
End Sub

Then if i rename the Userform to UserForm1 and it shows up but no data, all the labels show but no data.....

does that make sense?

i tried to send a snapshot but dont know how here .....

the above code is all i have in there

Answer
Anthony,

Best I can do is offer to look at your workbook.  I showed you the code worked as I understand the situation so there isn't anything I would say to change it since from my perspective nothing has changed.   Yes the userform has to be named userform1 if you use my code as written.


send it to twogilvy@msn.com

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.