View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Userform to increment line item numbers with textboxes-spinbut

I'm confused at what you're doing, but maybe...

Option Explicit
Private Sub cmdOK_Click()
Dim startno As Long
Dim iRow As Long
Dim StartCell As Range

'some validation???
If IsNumeric(Me.TextBox1.Value) = False _
Or IsNumeric(Me.TextBox2.Value) = False Then
MsgBox "Non-numbers in textboxes!"
Exit Sub
End If

If CLng(Me.TextBox1.Value) = CLng(Me.TextBox2.Value) Then
MsgBox "Numbers not in right order!"
Exit Sub
End If

Set StartCell = ActiveCell

For iRow = CLng(Me.TextBox1.Value) To CLng(Me.TextBox2.Value)
StartCell.Value = iRow
Set StartCell = StartCell.Offset(1, 0)
Next iRow

End Sub

This just fills a column/range of cells with consecutive values starting with
textbox1 and finishing with textbox2--and places the first number in the
activecell.

If this doesn't help, you may want to describe (in plain old words) what you're
trying to accomplish.



owlnevada wrote:

We'll we tried your solution but ran into other problems. I came up with this
option and it works as long as the first textbox is a "1". If 2 or higher it
adds it and doubles the second item then continues to increment by one. . ..
So with this code, can you find an elegant solution that will work whenever
the first number is 2 or higher?

Private Sub cmdOK_Click()
Dim startno As Integer ', lineno As Integer, newno As Integer
Dim i As Integer

ActiveCell.Activate
ActiveCell.FormulaR1C1 = TextBox1.Value

ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Activate
Selection.ClearContents

For i = TextBox1.Value To (TextBox2.Value - 1)
startno = 0
ActiveCell.Activate
startno = TextBox1.Value + i 'this gets doubled if other than a
one in textbox1

ActiveCell.Offset(RowOffset:=4, ColumnOffset:=0).Activate
ActiveCell.FormulaR1C1 = TextBox1.Value + i
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Activate
Selection.ClearContents

Next i

End Sub

"Joel" wrote:

See if this works, if not, it should be simplier for you to figure out what
is wrong with this code.

Private Sub cmdOK_Click()
Dim H As Integer, lineno As Integer, newno As Integer

ActiveCell.FormulaR1C1 = TextBox1.Value

Set FirstCell = ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0)
H = TextBox1.Value 'from textbox1 on userform1
RowOffset = 0
newno = 1
Do
lineno = H
FirstCell.Offset(RowOffset:=RowOffset, ColumnOffset:=0).ClearContents
FirstCell.Offset(RowOffset:=RowOffset + 4, ColumnOffset:=0) _
.FormulaR1C1 = H + newno
newno = newno + H
RowOffset = RowOffset + 5
Loop While lineno < TextBox2.Value 'value in textbox2 on userform1
Exit Sub


End Sub


"owlnevada" wrote:

I use the following code with a userform to renumber line item numbers that
get changed. My UF has two spinbuttons with two textboxes that update each
other for a starting and ending line item number. I need help with just this
part that looks at those two entries to change and increment the line number
by one from the starting line no. to the ending line number. These can be
something like "1" to "8" or "9" to "16" etc. H becomes the starting line
number and the other variables that should increment but don't. I am
modifying older code that works but is not as efficient that does not use the
loop routine.

Any help is much appreciated.

Private Sub cmdOK_Click()
Dim H As Integer, lineno As Integer, newno As Integer

ActiveCell.Activate
ActiveCell.FormulaR1C1 = TextBox1.Value
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Activate
Selection.ClearContents
H = TextBox1.Value 'from textbox1 on userform1
Do Until lineno = TextBox2.Value 'value in textbox2 on userform1
lineno = H
ActiveCell.Offset(RowOffset:=4, ColumnOffset:=0).Activate
ActiveCell.FormulaR1C1 = H + newno
newno = 1
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Activate
Selection.ClearContents
newno = H + newno
Loop
Exit Sub


End Sub


--

Dave Peterson