Home » excel » excel – Variables will not convert to integers

excel – Variables will not convert to integers

Posted by: admin May 14, 2020 Leave a comment


Currently messing around with macros in excel. One that generates a range that fills 3 columns of cells with 1-9.
Another that plots these numbers kind of like battle ship with x,y and v for the value.

I’ve gotten the number generation part working and I’m stuck on the plotting bit.

Currently the issue it that I’m getting an error “13” which means that my variables don’t match up.
But i’m using a CInt to convert the variant to a int.
Debugging it seems like the for loop is getting all the values properly but just not converting.

Here is the code I have so far and a screenshot of the whole thing.

enter image description here

Sub random()
Dim MyRange As Range
Dim c As Integer, r As Integer
Set MyRange = Workbooks("test random gen").Sheets("Sheet1").Range("G16:I30")
For c = 1 To MyRange.Columns.Count
For r = 1 To MyRange.Rows.Count
MyRange.Cells(r, c) = Int((9 - 1 + 1) * Rnd + 1)
Next r
Next c
End Sub

Sub Button6_Click()
Dim Board As Range
Dim Table As Range
Dim c As Integer, r As Integer
Dim Xboard As Integer, Yboard As Integer, Vboard As Integer
Dim Xboardv As Variant, Yboardv As Variant, Vboardv As Variant

Set Table = Workbooks("test random gen").Sheets("Sheet1").Range("G16:G30")
Set Board = Workbooks("test random gen").Sheets("Sheet1").Range("M16:U24")

For r = 1 To Table.Rows.Count
    Xboardv = Table.Cells.Value
    Yboardv = Table.Cells.Offset(columnOffset:=1).Value
    Vboardv = Table.Cells.Offset(columnOffset:=2).Value

    Xboard = CInt(Xboardv)
    Yboard = CInt(Yboardv)
    Vboard = CInt(Vboardv)

    Board.Cells(Xboard, Yboard).Value = (Vboard)

Next r

End Sub
How to&Answers:

Hm, oke I’ll recap what I mentioned in my comments above:

“It is getting stuck on the Xboard = CInt(Xboardv) line with the error “13””

You are creating an array of values with Xboardv = Table.Cells.Value. The array is sized 1 To 15, 1 To 1 and you need to use these index numbers as row and column parameters when you refer to any element in the array. So basically: Xboard = CInt(Xboardv(<X>,<Y>)).

“That did it but now it only does it for the first value of the array due to it being called out as (1,1)”

That is because you are constantly refering to the same element. However, you have created a loop with r variable allready. So you can use that to call different elements: Xboard = CInt(Xboardv(r,1))

I’ve neglected the fact that your structure is somewhat strange and you are creating the same array in a loop. So move that outside your loop and possibly use a For R = Lbound(Xboard) to Ubound(Xboard) loop instead. And you can just address values without having to convert them too.

Btw, no need for Integer variables at all. Use Long instead.


What’s the reason for doing a conversion to begin with?
In cell ‘D4’, I’ve put the value 325, and I’ve run following piece of code:

Dim b As Integer

b = Range("D4").Value

Value b is 325, no problem. No conversion needed.