View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
dude dude is offline
external usenet poster
 
Posts: 12
Default UserForm code help needed.

I assume that you are trying to add data from your form in
to database format? If so, you could just simply read the
user input from the form into a simple array and then add
data to your table this way.
something like this may work:

Private Sub CommandButton1_Click()

ReDim mydata(4)

For na = 0 To 3
mydata(na) = Controls(na).Text
Controls(na).Text = ""
Next

Dim NewRecordRng As Object
Set NewRecordRng = Worksheets("sheet1").Cells(2,
1).CurrentRegion
'Put new values into worksheet just below those rows.
newrow = NewRecordRng.Rows.Count + 1

With NewRecordRng
For na = 0 To 3
.Cells(newrow, na + 1).Value = mydata(na)
Next
End With
TextBox2.SetFocus
End Sub

I have only shown 4 textboxes but you can modify to meet
your needs. To see if this works for you. create a form &
add 4 textboxes then add a button(in that order) Double
click the button and past above code. You need a sheet
named sheet1. Run the form & enter data. It should add
data to sheet1 & clear textboxes for next entry. further
entry should be placed on next line.

Hope helpful

-----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/

.