excel vba programme
Point 1:-
Note the following two statements - the first contained
within the option button code and the second that follows.
The second obviously nullifies the first:
LastRow.Offset(1, 1).Value = OptionButton2.Caption
LastRow.Offset(1, 1).Value = TextBox2.Text
Point 2:-
Also note that your use of nested If/Then code is
unnecessary. I suggest using an If/ElseIf/Else construct
instead. It's much simpler. I use this in my code example.
Point 3:-
I assume the following was a typo and does not exist in
your code:
LastRow.Offset(1, 0).Value =
OptionButton.CaptionTextBox1.Text
Point 4:-
You declare the LastRow variable as:
Dim LastRow As Object
Set LastRow = Sheet1.Range("a65536").End(xlUp)
Although not a critical point, since the code sets LastRow
to a cell reference, you should decalare it more precisely
as "Range" rather than "Object". Also, the variable name
seems to imply a row reference (Long or Integer) instead
of a cell (Range). In my code example, I use the variable
name "C" instead of "LastRow" which implies a cell
reference for readability purposes.
Point 5:-
You set the LastRow variable to the last used cell in
Column A and then go on to reference offsets of this
several times. I thought it more efficient to set the
variable reference (I use "C") to the offset of 1 row down
and 1 column right and thus reduce then need for offset
references.
I don't know exactly what you're trying to do so the
example that follows will not do entirely what you want. I
comment out the line "C.Value = TextBox2.Text". This was
the translation of your code that nullified the option
button code. I don't know what you want to do here. Give
it a shot and see where we get.
Private Sub CommandButton1_Click()
Dim C As Range
Dim Msg As String, Resp As Integer
Dim Style As Integer, Title As String
Set C = Sheet1.Range("A65536").End(xlUp).Offset(1, 1)
If OptionButton1 Then
C.Value = OptionButton1.Caption
ElseIf OptionButton2 = True Then
C.Value = OptionButton2.Caption
Else
C.Value = OptionButton3.Caption
End If
C.Offset(0, -1).Value = TextBox1.Text
'C.Value = TextBox2.Text
C.Offset(0, 1).Value = TextBox3.Text
Style = vbInformation
Msg = "One record written to Sheet1 "
Title = " Data entry"
MsgBox Msg, Style, Title
Msg = "Do you want to enter another record? "
Style = vbYesNo + vbQuestion
Resp = MsgBox(Msg, Style, Title)
If Resp = vbYes Then
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
OptionButton1.Value = False
OptionButton2.Value = False
TextBox1.SetFocus
Else
Unload Me
End If
End Sub
Regards,
Greg
|