All I want to happen is if the user enters incorrect information, use a msgbox to tell them what to do to correct it and then allow them to correct it before proceeding. I have the following code:
Public NumCols As Integer
Public NumRows As Integer
Private Sub Calculate_Click()
Dim Rng As Range
Dim Temp As Range
NumCols = Range(SeriesRange).Columns.Count
NumRows = Range(SeriesRange).Rows.Count
Set Rng = Range(SeriesRange)
Debug.Print Range(SeriesRange).Rows.Count
Debug.Print "Rng = " & Rng.Rows.Count
Debug.Print Rng.Address
'Test the condition of labels checkbox
If LabelsChk.Value = True Then
NumRows = NumRows - 1
Set Rng = Range(SeriesRange).Offset(1, 0).Resize(NumRows, NumCols)
Debug.Print "Rng = " & Rng.Rows.Count
End If
Debug.Print Rng.Address
'Check to see if we have a long enough return series
If NumRows < 36 Then
MsgBox "Please select at least 3 years worth of monthly returns", vbOKOnly
End If
'Check to see if we have enough asset classes
If NumCols < 3 Then
MsgBox "Please select at least 3 asset classes.", vbOKOnly
End If
'Check to make sure all cells have numeric values
For Each Cell In Rng
If IsNumeric(Cell) = False Then
MsgBox "Some of your data set is not numeric. Please select a new Return Series", vbOKOnly
End If
Next
Dim Asset() As Control
Dim EReturn() As Control
Dim ESd() As Control
ReDim Asset(1 To NumCols)
ReDim EReturn(1 To NumCols)
ReDim ESd(1 To NumCols)
Set Temp = Rng.Resize(NumRows, 1)
For i = 1 To NumCols
Set Asset(i) = Me.Controls.Add("Forms.Textbox.1", "Asset" & CStr(i), True)
Asset(i).Top = 132 + ((i - 1) * 23)
Asset(i).Left = 54
Asset(i).Width = 114
If LabelsChk.Value = True Then
Asset(i).Value = Range(SeriesRange).Resize(1, 1).Offset(0, i - 1)
Else
Asset(i).Value = "Asset " & CStr(i)
End If
Set EReturn(i) = Me.Controls.Add("Forms.Textbox.1", "Return" & CStr(i), True)
EReturn(i).Top = 132 + ((i - 1) * 23)
EReturn(i).Left = 210
EReturn(i).Width = 42
EReturn(i).Value = Format(Application.WorksheetFunction.Average(Temp.Offset(0, i - 1)) * 12, "#.##")
Set ESd(i) = Me.Controls.Add("Forms.Textbox.1", "StDev" & CStr(i), True)
ESd(i).Top = 132 + ((i - 1) * 23)
ESd(i).Left = 300
ESd(i).Width = 42
ESd(i).Value = Format((Application.WorksheetFunction.VarP(Temp.Offset(0, i - 1)) * 12) ^ (1 / 2), "#.##")
Next
End Sub
I have 3 conditions that I want to check for:
1. Row count greater than 36
2. Column count greater than 2
3. All values are numeric
If any of those conditions are violated I want the user to be able to go back and correct it and then allow the program to proceed with the proper information. I tried a goto statement with disastrous consequences (endless loop) and I can't figure out what to try next. There is only one thing the user has to input up to the 3 condition check and that is to select a range of cells. How do I get the program execution back to the point where they can select a range of cells again?
Please let me know if this is not making any sense.
Bookmarks