Domanda

I try to learn VBA. This code:

    Dim i As Integer
    Dim damage As String
    i = 1
    Do While 1
        damage = CStr(Worksheets("charakters").Range("d14").Value)
        you_min_damage = CInt(Left(damage, i))

        If Right(i, 0) = "-" Then
            Trim (you_min_damage)
            Exit Do
        End If
        i = i + 1
    Loop

cause this problem (in 4 iteration):

error

In cell D14 I have "4 - 11". I want to separate first number nad change it to integer.

You_min_damage is integer.

È stato utile?

Soluzione

Try this one:

Dim you_min_damage As Integer, you_max_damage As Integer
Dim arr
'store all values in array
arr = Split(Worksheets("charakters").Range("d14").Value, "-")

'get first value
you_min_damage = CInt(arr(0))
'get last value
you_max_damage = CInt(arr(UBound(arr)))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top