0

I am trying to write a VBA script that will list all the possible combinations of truth values for a truth table in an excel spreadsheet.

I am taking a philosophy class and I thought it would be fun to write a code to first list all combinations of truth values possible for any number of propositional constants and then to find which combinations satisfy a the compound proposition. As with all code when you get stuck, I was wrong about the fun part.

Here is my code so far: (its pretty useless)

    Sub TruthTable()

Dim ws As Worksheet, Constants As Double, Possibilities As Double, bRow As Double, bCol As Double, tableArea As Double, eRow As Double, eCol As Double, tableArray As Range
Dim col As Integer, rw As Integer, a As String, b As String, c As String, aR As Range, bR As Range, cR As Range, rows As Integer

Set ws = ThisWorkbook.Worksheets("Table")


Constants = ws.Range("B1").Value
Possibilities = ws.Range("B2").Value

'Beginning
bRow = 5
bCol = 1

'End
eRow = bRow + Possibilities - 1
eCol = bCol + Constants - 1

tableArea = Constants * Possibilities

Set tableArray = ws.Range(Cells(bRow, bCol), Cells(eRow, eCol))

tableArray = "x"

rw = bRow
col = bCol


For rw = bRow To eRow
    For col = bCol To eCol
    ws.Cells(rw, col) = 1
        If col = 1 Then
           a = ws.Cells(rw, col)
           Set aR = ws.Cells(rw, col)
        ElseIf col = 2 Then
           b = ws.Cells(rw, col)
           Set bR = ws.Cells(rw, col)
        ElseIf col = 3 Then
           c = ws.Cells(rw, col)
           Set cR = ws.Cells(rw, col)
        End If
    Next col
 
    
Next rw
    

End Sub

Cell B1 counts the number of constants and cell b2 calculates the number of combinations using base 2 since a truth value can only be 1 or 0 or true or false.

I am stuck at the hard part of course, which is figuring out how to get the numbers to change and recursively check if this is a new combination and alter it if not.

I was going to write a function after the End if to check but i am not good enough at vba or coding in general to figure it out yet

I want the code to be dynamic enough to handle any number of constants (columns) as well as rows as these will be a function of the number of columns using base 2 i.e. (2^number of columns).

Here is what a 3 constant truth table should look like:

p | q | r
1   1   1
1   1   0
1   0   0
0   1   0
0   0   0
0   0   1
0   1   1
1   0   1

Can anyone help me with this or give me a nudge in the right direction? Would really appreciate it.

  • An easy way: You are counting from `2^n-1 to 0` in binary. Just use the worksheet function `Base` for that in a `For` loop. In VBA: `Application.WorksheetFunction.Base(...` Once you get that string, you can use `Mid` to pull off the individual digits. Once you see that, you don't even need VBA for this. – John Coleman Jul 20 '20 at 19:45
  • Thanks for the reply John. I'm such a novice I really am not sure how I would implement that. Would you mind explaining a bit more? –  Jul 20 '20 at 19:53
  • If you have Office 365 put this in P1: `=MID(TEXT(DEC2BIN(SEQUENCE(2^$A$1,,2^$A$1-1,-1)),REPT(0,A1)),SEQUENCE(,A1),1)` Where A1 is where you put the number of columns desired. It will work up to 9. – Scott Craner Jul 20 '20 at 20:31
  • Quite a succinct approach. Really appreciate it Scott. –  Jul 20 '20 at 21:21

0 Answers0