I would appreciate any help that anyone can offer. Please note (disclaimer ) I am new to using forms and to VBA coding in general.
I have to develop a form which will ultimately populate several text boxes from an excel worksheet. In order to test my ability to do this, I made a 'test' excel spreasheet with five columns of data (CustomerID, Name, City, State, and ZipCode).
I have Command buttons Previous, Next, First, and Last to manuever through the rows of data. I also have a RowNumber textbox to dispay the rows (I have text set to 2). I have an Add command button, as well as Save and Close.
I have basically copied code from elsewhere to do this, but I cannot get it to work. I get multiple error messages, including that 'ClearData' is not defined. Can someone take a look at this code and possibly tell me what could be going wrong? I have checked and double-checked that my Column and text box names match.
When I run this it does not pull in the data and i cannot navigate or enter any row number without getting an 'invalid row' message.
Thank you in advance for any help!
Here is the GetData routine:
Private Sub GetData()
Dim r As Long
If IsNumeric(RowNumber.Text) Then
r = CLng(RowNumber.Text)
Else
ClearData
MsgBox "Illegal row number"
Exit Sub
End If
If r > 1 And r <= LastRow Then
CustomerId.Text = FormatNumber(Cells(r, 1), 0)
CustomerName.Text = Cells(r, 2)
City.Text = Cells(r, 3)
State.Text = Cells(r, 4)
Zip.Text = Cells(r, 5)
DisableSave
ElseIf r = 1 Then
ClearData
Else
ClearData
MsgBox "Invalid row number"
End If
End Sub
Private Sub ClearData()
CustomerId.Text = ""
CustomerName.Text = ""
City.Text = ""
State.Text = "AK"
Zip.Text = ""
End Sub
Private Sub DisableSave()
CommandButton5.Enabled = False
CommandButton6.Enabled = False
End Sub
Private Sub RowNumber_Change()
GetData
End Sub
Private Sub CommandButton2_Click()
Dim r As Long
If IsNumeric(RowNumber.Text) Then
r = CLng(RowNumber.Text)
r = r ? 1
If r > 1 And r <= LastRow Then
RowNumber.Text = FormatNumber(r, 0)
End If
End If
End Sub
I have to develop a form which will ultimately populate several text boxes from an excel worksheet. In order to test my ability to do this, I made a 'test' excel spreasheet with five columns of data (CustomerID, Name, City, State, and ZipCode).
I have Command buttons Previous, Next, First, and Last to manuever through the rows of data. I also have a RowNumber textbox to dispay the rows (I have text set to 2). I have an Add command button, as well as Save and Close.
I have basically copied code from elsewhere to do this, but I cannot get it to work. I get multiple error messages, including that 'ClearData' is not defined. Can someone take a look at this code and possibly tell me what could be going wrong? I have checked and double-checked that my Column and text box names match.
When I run this it does not pull in the data and i cannot navigate or enter any row number without getting an 'invalid row' message.
Thank you in advance for any help!
Here is the GetData routine:
Private Sub GetData()
Dim r As Long
If IsNumeric(RowNumber.Text) Then
r = CLng(RowNumber.Text)
Else
ClearData
MsgBox "Illegal row number"
Exit Sub
End If
If r > 1 And r <= LastRow Then
CustomerId.Text = FormatNumber(Cells(r, 1), 0)
CustomerName.Text = Cells(r, 2)
City.Text = Cells(r, 3)
State.Text = Cells(r, 4)
Zip.Text = Cells(r, 5)
DisableSave
ElseIf r = 1 Then
ClearData
Else
ClearData
MsgBox "Invalid row number"
End If
End Sub
Private Sub ClearData()
CustomerId.Text = ""
CustomerName.Text = ""
City.Text = ""
State.Text = "AK"
Zip.Text = ""
End Sub
Private Sub DisableSave()
CommandButton5.Enabled = False
CommandButton6.Enabled = False
End Sub
Private Sub RowNumber_Change()
GetData
End Sub
Private Sub CommandButton2_Click()
Dim r As Long
If IsNumeric(RowNumber.Text) Then
r = CLng(RowNumber.Text)
r = r ? 1
If r > 1 And r <= LastRow Then
RowNumber.Text = FormatNumber(r, 0)
End If
End If
End Sub