Hi, all i am trying to write a roman numeral calculator in vba and man is it taking a long time, i am currently trying to use select case to check whether or not the letters can be replaced by another letter e.g. entering IIIII will display a error message saying change it to a V. I'm not great at explaining things so here is my code
the part in red is where i am having trouble basically if i enter the roman numeral IIIII the arr_repeat counts how many i have fine (it counts 5) but in the second select case i get an error saying mismatch and therefore it will not run.
Any help would be really appreciated
Thanks
Code:
Public Sub main()
Dim tot As Integer
tot = additive_rule + additive_rule
Debug.Print tot
End Sub
Function additive_rule()
Dim arr_rnstr() As String 'string array'
Dim rnstr As String
Dim arr_rnnum() As Variant 'number array'
Dim rntotal1 As Integer
Dim arr_repeat(6) As Variant 'seven roman numerals'
rnstr = InputBox("enter the roman numeral in descending order of value and put a comma between each and make sure in caps")
arr_rnstr() = Split(rnstr, ",") 'splits the string the user entered into single values'
ReDim arr_rnnum(UBound(arr_rnstr)) 'sets the size of the array of numbers to the upper bound of the array of letters the user entered'
For j = LBound(arr_rnstr) To UBound(arr_rnstr) 'this section checks each value of the array of letters and replaces it with a number in a second array'
Select Case arr_rnstr(j)
Case Is = "I"
arr_rnnum(j) = 1
Case Is = "V"
arr_rnnum(j) = 5
Case Is = "X"
arr_rnnum(j) = 10
Case Is = "L"
arr_rnnum(j) = 50
Case Is = "C"
arr_rnnum(j) = 100
Case Is = "D"
arr_rnnum(j) = 500
Case Is = "M"
arr_rnnum(j) = 1000
End Select
Next j
j = 0 'reset j to zero for the next for loop'
For j = LBound(arr_rnnum) To UBound(arr_rnnum) 'this is to check that the numbers are in descedning order if not the prgram will end'
If k < UBound(arr_rnnum) Then
k = j + 1
Else
End If
Select Case arr_rnnum(j)
Case Is < arr_rnnum(k)
MsgBox ("redo numbers in descending order")
End
End Select
Next j
j = 0
For j = LBound(arr_repeat) To UBound(arr_repeat)
arr_repeat(j) = 0
Next j
j = 0
[COLOR=#ff0000]While j <= UBound(arr_rnnum) 'this adds a +1 to a counter for each element in a seperate array'[/COLOR]
[COLOR=#ff0000] Select Case arr_rnnum(j)[/COLOR]
[COLOR=#ff0000] Case Is = 1[/COLOR]
[COLOR=#ff0000] arr_repeat(0) = arr_repeat(0) + 1[/COLOR]
[COLOR=#ff0000] Case Is = 5[/COLOR]
[COLOR=#ff0000] arr_repeat(1) = arr_repeat(1) + 1[/COLOR]
[COLOR=#ff0000] Case Is = 10[/COLOR]
[COLOR=#ff0000] arr_repeat(2) = arr_repeat(2) + 1[/COLOR]
[COLOR=#ff0000] Case Is = 50[/COLOR]
[COLOR=#ff0000] arr_repeat(3) = arr_repeat(3) + 1[/COLOR]
[COLOR=#ff0000] Case Is = 100[/COLOR]
[COLOR=#ff0000] arr_repeat(4) = arr_repeat(4) + 1[/COLOR]
[COLOR=#ff0000] Case Is = 500[/COLOR]
[COLOR=#ff0000] arr_repeat(5) = arr_repeat(5) + 1[/COLOR]
[COLOR=#ff0000] Case Is = 1000[/COLOR]
[COLOR=#ff0000] arr_repeat(6) = arr_repeat(6) + 1[/COLOR]
[COLOR=#ff0000] End Select[/COLOR]
[COLOR=#ff0000] j = j + 1[/COLOR]
[COLOR=#ff0000]Wend[/COLOR]
[COLOR=#ff0000]
[/COLOR]
[COLOR=#ff0000]j = 0[/COLOR]
[COLOR=#ff0000]
[/COLOR]
[COLOR=#ff0000]Select Case arr_repeat 'this now checks if the roman numeral entered has letters that can be replaced by another letter'[/COLOR]
[COLOR=#ff0000] Case arr_repeat(0) < 4[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 4 Is replace with a V")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000] Case arr_repeat(1) > 1[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 1 V replace with a X")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000] Case arr_repeat(2) > 4[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 4 Xs replace with a L")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000] Case arr_repeat(3) > 1[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 1 L replace with a C")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000] Case arr_repeat(4) > 4[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 4 Cs replace with a D")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000] Case arr_repeat(5) > 1[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 1 D replace with a M")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000] Case arr_repeat(6) > 3[/COLOR]
[COLOR=#ff0000] MsgBox ("you have more than 3 Ms")[/COLOR]
[COLOR=#ff0000] End[/COLOR]
[COLOR=#ff0000]End Select[/COLOR]
[COLOR=#ff0000]
[/COLOR]
[COLOR=#ff0000]
[/COLOR]
[COLOR=#ff0000]Debug.Print arr_repeat(0)[/COLOR]
[COLOR=#ff0000]Debug.Print arr_repeat(1)[/COLOR]
[COLOR=#ff0000]Debug.Print arr_repeat(2)[/COLOR]
[COLOR=#ff0000]Debug.Print arr_repeat(3)[/COLOR]
j = 0
For j = LBound(arr_rnnum) To UBound(arr_rnnum)
rntotal1 = rntotal1 + arr_rnnum(j)
Next j
additive_rule = rntotal1
End Function
the part in red is where i am having trouble basically if i enter the roman numeral IIIII the arr_repeat counts how many i have fine (it counts 5) but in the second select case i get an error saying mismatch and therefore it will not run.
Any help would be really appreciated
Thanks