Looping

J

Jennifer

I have this code that enter data into my worksheet from textbox and labels.
ws.Cells(iRow, 1) = iRow - 1
ws.Cells(iRow, 2) = Me.txtRFID.Value
ws.Cells(iRow, 3) = Me.DTPicker1.Value
ws.Cells(iRow, 5) = Me.TextBox1.Value
ws.Cells(iRow, 4) = Me.Label1

Problem is i could have as many as 11 textboxes and label with values never
constant. how do you write this to enter so that it enters this code the for
the tb1 and lbl1 then run again if there is tb2 and lb2 and so on. Would it
be a loop?
 
J

Joel

I saw your previous posting last night. I used the result from the previous
posting to make the code below. The code below will vary the number of times
it loop based on the number of Labels it finds.

iRow = 1
For Each lbl In Controls
If Left(lbl.Name, 5) = "Label" Then
If Len(Trim(lbl.Caption)) > 0 Then
labelnumber = Mid(lbl.Name, 6)

ws.Cells(iRow, 1) = iRow - 1
ws.Cells(iRow, 2) = Me.txtRFID.Value
ws.Cells(iRow, 3) = Me.DTPicker1.Value
ws.Cells(iRow, 5) = _
Me.Controls("TextBox" & labelnumber).Value
ws.Cells(iRow, 4) = lbl.Caption
iRow = iRow + 1
End If
End If
Next lbl
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top