Question

I stumbled across a new puzzle called "go figure!" in the local paper. I can't find anything on it currently on the web but it is similar to sudoko but with math operatives. You are given a list of numbers to use and you have to fill in the blanks. For example: The first row would be _ / _ + _ = 10

I have in cell D4 a / and cell F4 a +

My vba code has

Dim OPR11, OPR12, as String

then further down I have

OPR11 = Cells(4, 4).Value
OPR12 = Cells(4, 6).Value

So in this case OPR11 = / and OPR12=+

Next I set up an array with the numbers and do a next for loop as follows:

For Each rng3 In vArray
  For Each rng2 In vArray
    For Each rng1 In vArray
 If rng1 & OPR11 & rng2 & OPR12 & rng3 = R1A Then
  DO CODE
 end if
    next rng1
  next rng2
next rng3

As many fo you can see, my If function isn't working. Evidently, I can't CONC the mathamatical operators into the formula as it stands and have it work.

How should i better do this to make it work? Thanks

Was it helpful?

Solution

I think you need to use Evaluate() to calculate a formula expression.

For example

enter image description here

turn on the Immediate Window with CTRL+G

and run the code

Option Explicit

Sub Main()

    Dim c4 As Range
    Dim d4 As Range
    Dim e4 As Range
    Dim f4 As Range
    Dim g4 As Range

    Set c4 = Cells(4, 3)
    Set d4 = Cells(4, 4)
    Set e4 = Cells(4, 5)
    Set f4 = Cells(4, 6)
    Set g4 = Cells(4, 7)

    Dim answer As Double
    answer = Evaluate("=" & c4 & d4 & e4 & f4 & g4)

    Debug.Print "The formula evaluates to: " & Evaluate("=" & c4 & d4 & e4 & f4 & g4)

    Cells(4, 9) = answer

End Sub

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top