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.