View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Alan Hutchins Alan Hutchins is offline
external usenet poster
 
Posts: 21
Default UserForm code help needed.

There are a few things that would help:

1) you could specify the cell where you want to start
your process, and then select it as the activecell. Then
use Activecell.Offset(0,1).value = x

where x is the value you wish to assign.

You just increase the offset number by 1 to move across
the columns, and it would be better to specifically code
these, rather than increment them.

2) you seem to be relying on Excel determining the active
row to produce the next empty row - this is not a
foolproof method. You would be better off determining the
last used cell in the sheet where you are writing the
details, and then obtaining the rwo, incrementing by one
and selecting the first cell on taht row - the function
below always works.

Call the function like this:

Call Goto_LAst("a1",1,1)

it will return the last cell address and the numeric
column and the number of the row also.

Function Goto_Last(strrange, int_col, int_row)
On Error Resume Next
application.ScreenUpdating = False
Cells(Cells.Find("*", Range("A1"), , , xlByRows,
xlPrevious).Row, _
Cells.Find("*", Range("A1"), , , xlByColumns,
xlPrevious).Column).Select
If Err.Number < 0 Then MsgBox "No data in sheet"
application.ScreenUpdating = True
strrange = ActiveCell.AddressLocal
int_col = ActiveCell.Column
int_row = ActiveCell.Row
End Function


Hope this is of use

Alan
-----Original Message-----
Thanks to all who helped me yesterday. You were so

helpful I thought I'd
throw my other VBA problem into the ring.

What I've got is a Userform for users to input address

details, which
then puts those details into an excel sheet when a

button is clicked.
(The intention is eventually to mailmerge the details

and print a sheet
of address labels)

Unfortunately, it keeps putting the address details on

the same line,
until you try and close the form, when it gets stuck in

a loop.

Hope someone can help. Here's the userform code



Public Sub InputButton_Click()
'set up variables
Dim Check

Dim Activerow As Variant
Dim StartCellRow As Integer
Dim StartCellColumn As Integer

Dim Printbutt As Integer

Dim Title As String
Dim FirstName As String
Dim Surname As String
Dim Address1 As String
Dim Address2 As String
Dim Address3 As String
Dim Address4 As String
Dim Postcode As String
Dim Todaydate As String
Dim ID As String
Dim form As String

'fill variables with info from form
Check = True
Title = Titlebox
FirstName = TxtFirstname
Surname = TxtSurname
Address1 = TxtAddress1
Address2 = TxtAddress2
Address3 = TxtAddress3
Address4 = TxtAddress4
Postcode = TxtPostcode
Todaydate = TxtDate
ID = TxtLoginID
form = Formbox
Today = Format(Date, "d/mm/yyyy")
StartCellRow = "1"
StartCellColumn = "1"
Printbutt = "1"
'check this works
Do
ActiveCell(StartCellRow, StartCellColumn) = Title
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = FirstName
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Surname
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Address1
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Address2
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Address3
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Address4
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Postcode
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = Today
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = ID
StartCellColumn = StartCellColumn + 1
ActiveCell(StartCellRow, StartCellColumn) = form
StartCellRow = StartCellRow + 1
StartCellColumn = "1"

'this bit is to try and clear the form ready for the

next input
Unload UserForm1
UserForm1.Show

Loop Until Check = False


End Sub

'when this button is clicked, check should equal false
'and the form should be closed

Public Sub PrintButton_Click()
Check = False
End Sub


---
Message posted from http://www.ExcelForum.com/

.