
UserForm or not to UserForm?
Hi Brad,
Quote:
> I am having a problem figuring out how to
> identify the cells to the code of the user form. If I go with the insert
> rows method, the row and column references are dynamic and I am not able to
> hard-code
> them. I have considered a Offset method from the current selection but that
> becomes clumsy and prone to errors and requires the user to put the cursor
> in a specific column or the whole thing goes whacky. I would like to
> identify a range of blank cells approx. 9 columns by 3 or 4 rows, then
> refer to those cells like A1 as the upper left cell in my range.
It is a little unclear how your sheet(s) are structured, or what data you
want where. That said, a general technique is to define named ranges to
refer to the position on the sheet where the data is stored, then incorporate
that range name into a name you give the control.
For example, let's assume you have an edit box for Time on the userform which
needs to go into column D on the sheet. You could select column D and give
it a name of colTime and you could name the edit box ebTime. You could then
locate a blank row in the sheet using something like:
Set oBlank = Range("A1").End(xlDown).Offset(1,0).EntireRow
to give you the first row in which column a is empty (assuming A1 and A2 are
filled).
Your code could then do something like:
Intersect(oBlank, Range("colTime")).Value = ebTime
Once you get this far, you could either have lots of these rows, or use
something like:
On Error Resume Next 'Ignore unmatched controls
For i = 0 To Form1.Control.Count
Set oCtl = Form1.Controls(i)
Intersect(oBlank, Range("col" & Mid(oCtl.Name, 3))).Value = oCtl
Next
On Error Goto 0
which will loop through all the controls, putting them in the correct
columns on the destination sheet. This assumes that each control name has 2
letters to identify the control type, then the remainder of the name is the
part to be used to identify the column where its data is to be stored.
Note that the code examples above have not been tested!
Regards
Stephen Bullen
Microsoft MVP - Excel
http://ourworld.compuserve.com/homepages/Stephen_Bullen