PDA

View Full Version : [SOLVED:] For/Next for Blanking Fields



CdyMan
10-09-2013, 12:06 PM
In an effort to eliminate 60 some [txbXXX.Value = ""] statements to blank the fields on a userform, I added .Value to each item in a list of controls I already had, then put the for/next in to step through and clear them. Loop is fine, no errors, but the blanking is not happening. Does anyone see the obvious that i'm missing?? I've tried this version with object when a version with ctl as a string and set as a simple assignment didn't work either. Thanks a lot for pointing out the flaw. MsgBox in there just to confirm items coming through--all are what I want.


Private Sub Clear_Click()
Dim wks As Worksheet
Dim idx As Range
Dim ctl As Object
Set wks = Sheets("ControlsList")
For Each idx In wks.Range("ctlsInventory").Rows
Set ctl = idx
'MsgBox ctl
ctl = ""
Next

Aflatoon
10-09-2013, 01:10 PM
If the range contains the names of the controls, you need to use something like:
Private Sub Clear_Click()
Dim wks As Worksheet
Dim idx As Range
Dim ctl As Object
Set wks = Sheets("ControlsList")
For Each idx In wks.Range("ctlsInventory").Rows
Set ctl = me.controls(idx.value)
'MsgBox ctl
ctl.value = ""
Next idx
end sub

CdyMan
10-09-2013, 01:27 PM
That did the trick--thank you so much!!!

Aflatoon
10-09-2013, 02:39 PM
You're welcome. :)