Excel 2003 VBA
The code works as is.
But I want to write a function or sub routine that will clean up the code:
Private Sub cmdAdd_Click()
Dim ws As Worksheet, MyYear As Integer, MyMonth As String
Dim MyRow As Long, MyCol As Integer
Dim response As Integer
Set ws = Worksheets("Data2")
MyMonth = Me.MyMonth.Value
MyYear = Me.MyYear.Value
MyRow = ws.Range("C4:C22").Find(what:=MyYear).Row
MyCol = ws.Range("C4:O4").Find(what:=MyMonth).Column
'First IF Statement
If ws.Cells(MyRow, MyCol) <> "" Then
Select Case MsgBox("Are you sure you want to overwrite the data?", vbYesNo, "Are you sure")
Case vbYes
Case vbNo
GoTo Last
End Select
End If
'Second IF Statement
If Not Me.Number < 0 And Me.Number > 100 Then
MsgBox "Number must be between 0 and 100", vbYes, "Number "
GoTo Last
Else
ws.Cells(MyRow, MyCol) = Me.Number.Value
End If
MsgBox "Finished Inputting Data", vbYes, "Finished"
Unload Me
Last:
End Sub
If the First If statement is true then I want it to run the Second IF statement.
I want to call the First IF Statement in a function so that I don't have to copy and paste this into the 200 values that will be entered into the sheet.
In the second If statement the number value changes, and the range changes for each entry I have.
Any Ideas?
Thanks in advance for the help.
Chilly442
The code works as is.
But I want to write a function or sub routine that will clean up the code:
Private Sub cmdAdd_Click()
Dim ws As Worksheet, MyYear As Integer, MyMonth As String
Dim MyRow As Long, MyCol As Integer
Dim response As Integer
Set ws = Worksheets("Data2")
MyMonth = Me.MyMonth.Value
MyYear = Me.MyYear.Value
MyRow = ws.Range("C4:C22").Find(what:=MyYear).Row
MyCol = ws.Range("C4:O4").Find(what:=MyMonth).Column
'First IF Statement
If ws.Cells(MyRow, MyCol) <> "" Then
Select Case MsgBox("Are you sure you want to overwrite the data?", vbYesNo, "Are you sure")
Case vbYes
Case vbNo
GoTo Last
End Select
End If
'Second IF Statement
If Not Me.Number < 0 And Me.Number > 100 Then
MsgBox "Number must be between 0 and 100", vbYes, "Number "
GoTo Last
Else
ws.Cells(MyRow, MyCol) = Me.Number.Value
End If
MsgBox "Finished Inputting Data", vbYes, "Finished"
Unload Me
Last:
End Sub
If the First If statement is true then I want it to run the Second IF statement.
I want to call the First IF Statement in a function so that I don't have to copy and paste this into the 200 values that will be entered into the sheet.
In the second If statement the number value changes, and the range changes for each entry I have.
Any Ideas?
Thanks in advance for the help.
Chilly442