Populate cells based on x by y by z cell value - excel

I'm trying to populate cells based on n-1 values from three different cells. I was successful in the x by y but I'm having trouble with z
For example, I have input:
x y z
5 2 2
Output should be:
x should have 0, 1, 2, 3, 4; each repeated twice
y should have 0 and 1; each repeated five times
z should have 0 and 1; each repeated five times but not the same output as x or y
x y z
0 1 0
1 0 0
2 1 0
3 0 0
4 1 0
0 0 0
1 1 0
2 0 0
3 1 0
4 0 0
0 1 1
1 0 1
2 1 1
3 0 1
4 1 1
0 0 1
1 1 1
2 0 1
3 1 1
4 0 1
I used:
for x
=IF(ROW()<=1+A$1*A$2*A$3,INT((ROW()-2)/(A$2*A$3)),"0")
for y
=IF(ROW()<=1+A$1*A$2*A$3,MOD(ROW()-1,A$3),"0")
for z
=IF(ROW()<=1+A$1*A$2*A$3,MOD(ROW()-1,A$2),"0")
A1 to A3 has the number of items for xyz. Are there any suggestions on how I can do this?

Sorry for the late answer. If you let yourself use VBA (since you asked for it on your comment), let's say you have the next sheet:
You could place the next Macro on the button GO:
Sub CartesianProduct()
'Constants:
Dim top_x As Integer
top_x = ActiveSheet.Cells(2, 1).Value - 1
Dim top_y As Integer
top_y = ActiveSheet.Cells(2, 2).Value - 1
Dim top_z As Integer
top_z = ActiveSheet.Cells(2, 3).Value - 1
'Coordinates
Dim x As Integer
Dim y As Integer
Dim u As Integer
'Counter (row):
Dim c As Integer
c = 2
'----------
For x = 0 To top_x
For y = 0 To top_y
For z = 0 To top_z
ActiveSheet.Cells(c, 5).FormulaR1C1 = x
ActiveSheet.Cells(c, 6).FormulaR1C1 = y
ActiveSheet.Cells(c, 7).FormulaR1C1 = z
c = c + 1
Next z
Next y
Next x
End Sub
Then, by clicking on GO, you fill the table with the set of the cartesian product of your sets:
If you want to avoid points with coordinates with the same value, just make the following ajust:
For x = 0 To top_x
For y = 0 To top_y
For z = 0 To top_z
If x <> y Or x <> z Or y <> z Then
ActiveSheet.Cells(c, 5).FormulaR1C1 = x
ActiveSheet.Cells(c, 6).FormulaR1C1 = y
ActiveSheet.Cells(c, 7).FormulaR1C1 = z
c = c + 1
End If
Next z
Next y
Next x

Related

Trying to plot a graph using two arrays in Excel VBA

the first 23 columns of the array called arr are 40 rows full of integer data ranging from 1-10. if the data is of value 2 then we keep count of how many there are. they are considered yellow values. the second array is called sheetArr and it's values are strings representing the month's of the year. If the string ends with "03" then that represents the month of March so we set a variable x to be of value 3. We want to plot the amount of yellow values there are in every month, yellow values being on the y-axis and the months being on the x-axis. The count of the yellow values are in the 24th column of the array. and each row in that array is associated to a row in the sheetArr at the same location. So arr(24, 0) is associated with sheetArr(0, 0). Lets say there were 5 yellow values in arr(24, 0) and the month associated to sheetArr(0,0) is 1 (Representing January). The graph would plot the value 5 on the y-axis and the value 1 on the x-axis. I also want the graph to be a plotted graph. Like dots on the graph and then have the dots connect with lines. When I run my code I get nothing plotted on my graph. Someone please help, Thanks.
Sub createGraph()
Dim i As Integer
Dim j As Integer
Dim y As Integer
Dim x As Integer
Dim z As String
Dim sheetCount As Integer
Dim pptText As String
Dim count As Integer
Dim countYellow As Integer
Dim arr(25, 40)
Dim sheetArr(0, 23)
For i = 0 To 23 'iterating through all columns of the array to count the yellows
countYellow = 0
For j = 0 To 40 'iterating through 40 rows
If arr(i, j) = 2 Then 'if its yellow
countYellow = countYellow + 1
End If
Next
arr(24, i) = countYellow
Next
'Plotting the graph and then changing features in the graph
i = 0
Charts.Add
ActiveChart.ChartType = xlXYScatterLinesNoMarkers
With ActiveChart.SeriesCollection
For i = 0 To count - 1
y = arr(24, i) 'The yellow count value
z = sheetArr(0, i) 'The name of the sheet
z = Right(z, 2)
If z = "01" Then 'if the name of the sheet ends with 01 then its january
x = 1 '1 representing January
ElseIf z = "02" Then
x = 2
ElseIf z = "03" Then
x = 3
ElseIf z = "04" Then
x = 4
ElseIf z = "05" Then
x = 5
ElseIf z = "06" Then
x = 6
ElseIf z = "07" Then
x = 7
ElseIf z = "08" Then
x = 8
ElseIf z = "09" Then
x = 9
ElseIf z = "10" Then
x = 10
ElseIf z = "11" Then
x = 11
ElseIf z = "12" Then
x = 12
End If
.Item(1).Values = y 'plot the yellow count
.Item(1).XValues = x 'plot the name of the sheet
Next
ActiveChart.SetElement msoElementPrimaryCategoryAxisShow
ActiveChart.SetElement msoElementPrimaryCategoryAxisTitleHorizontal
ActiveChart.SetElement msoElementPrimaryValueAxisShow
ActiveChart.SetElement msoElementPrimaryValueAxisTitleHorizontal
ActiveChart.ChartArea.Format.Fill.ForeColor.RGB = RGB(253, 242, 227)
End With
ActiveChart.ChartArea.Select
End Sub

Using arrays to plot graph

the first 23 columns of the array called arr are 40 rows full of integer data ranging from 1-10. if the data is of value 2 then we keep count of how many there are. they are considered yellow values. the second array is called sheetArr and it's values are strings representing the month's of the year. If the string ends with "03" then that represents the month of March so we set a variable x to be of value 3. We want to plot the amount of yellow values there are in every month, yellow values being on the y-axis and the months being on the x-axis. The count of the yellow values are in the 24th column of the array. and each row in that array is associated to a row in the sheetArr at the same location. So arr(24, 0) is associated with sheetArr(0, 0). Lets say there were 5 yellow values in arr(24, 0) and the month associated to sheetArr(0,0) is 1 (Representing January). The graph would plot the value 5 on the y-axis and the value 1 on the x-axis. I also want the graph to be a plotted graph. Like dots on the graph and then have the dots connect with lines. When I run my code I get nothing plotted on my graph. Someone please help, Thanks.
Sub createGraph()
Dim i As Integer
Dim j As Integer
Dim y As Integer
Dim x As Integer
Dim z As String
Dim sheetCount As Integer
Dim pptText As String
Dim count As Integer
Dim countYellow As Integer
Dim arr(25, 40)
Dim sheetArr(0, 23)
For i = 0 To 23 'iterating through all columns of the array to count the yellows
countYellow = 0
For j = 0 To 40 'iterating through 40 rows
If arr(i, j) = 2 Then 'if its yellow
countYellow = countYellow + 1
End If
Next
arr(24, i) = countYellow
Next
i = 0
Charts.Add
ActiveChart.ChartType = xlXYScatterLinesNoMarkers
With ActiveChart.SeriesCollection
For i = 0 To count - 1
y = arr(24, i) 'The yellow count value
z = sheetArr(0, i) 'The name of the sheet
z = Right(z, 2)
If z = "01" Then 'if the name of the sheet ends with 01 then its january
x = 1 '1 representing January
ElseIf z = "02" Then
x = 2
ElseIf z = "03" Then
x = 3
ElseIf z = "04" Then
x = 4
ElseIf z = "05" Then
x = 5
ElseIf z = "06" Then
x = 6
ElseIf z = "07" Then
x = 7
ElseIf z = "08" Then
x = 8
ElseIf z = "09" Then
x = 9
ElseIf z = "10" Then
x = 10
ElseIf z = "11" Then
x = 11
ElseIf z = "12" Then
x = 12
End If
.Item(1).Values = y 'plot the yellow count
.Item(1).XValues = x 'plot the name of the sheet
Next
ActiveChart.SetElement msoElementPrimaryCategoryAxisShow
ActiveChart.SetElement msoElementPrimaryCategoryAxisTitleHorizontal
ActiveChart.SetElement msoElementPrimaryValueAxisShow
ActiveChart.SetElement msoElementPrimaryValueAxisTitleHorizontal
ActiveChart.ChartArea.Format.Fill.ForeColor.RGB = RGB(253, 242, 227)
End With
ActiveChart.ChartArea.Select
End Sub

Fill columns randomly by Loop

Recently I was helped with a code that fill down randomnly the cells based on Row#1 values. Answered_Post. (Thanks to #JvdV and #Scott Craner for assist me before.)
What I need to do now is almost the same, but the code will fill the cells leaping the columns as per random value (x) in a total of 10 rows. The repeatable values remain on Row#1.
Below the code provided on that post to fill down rows. I need now, as per picture, fill the columns.
Dim x As Long, y As Long, z As Long
With Sheet1 'Change accordingly
For y = 1 To 15
z = 0
Do While z < 4
x = Int((7 - 2 + 1) * Rnd + 2)
If .Cells(x, y) <> .Cells(1, y) Then
.Cells(x, y) = .Cells(1, y)
z = z + 1
End If
Loop
Next y
End With
Table_With_Sample_Values
Sub FillColumns01()
For y = 1 To 15
z = 0
j = 1
Do While z < 4
x = Int((7 - 2 + 1) * Rnd + 2)
If Cells(x, y) <> Cells(1, y) Then
Cells(y + 1, j) = Cells(1, y)
z = z + 1
j = j + 1
End If
Loop
Next y
End Sub
Sub FillColumns02()
'Using a 3rd Loop
Dim x As Integer, y As Integer, z As Integer, j As Integer
For y = 1 To 10
z = 0
Do While z < 4
For j = 1 To 15
x = Int((7 - 2 + 1) * Rnd + 2)
If Cells(x, y) <> Cells(1, y) Then
Cells(j, x) = Cells(1, y)
z = z + 1
End If
Next j
Loop
Next y
End Sub

Unable to fetch values of range of cells in custom function

I was working on a problem which requires input an array and output is also an array. I wrote the VBA code (after I had done workings of logic on notebook) but I don't know why this code is unable to fetch values of input.
I have already written the code. Just help me with the syntax!
As I am not a professional coder, please ignore the naming of variables. Also, I am new to this platform, so I am not aware of many rules. Any help will really be appreciated. Thanks!
Test cases and error:
Public Function Get1_GEM(vector As Range)
Dim i As Long, j As Long, i1 As Long, j1 As Long
Dim a As Long, b As Long, c As Long
Dim GEM() As Double
ReDim GEM(1 To 3, 1 To 2) As Double
Dim res() As Double
ReDim res(1 To 3) As Double
Dim row() As Double
ReDim row(1 To 3) As Double
For i = 1 To 3 Step 1
For j = 1 To 2 Step 1
GEM(i, j) = 2 * i + j - 2
row(i) = 0
Next j
Next i
Dim inp() As Double
ReDim inp(1 To 3) As Double
i = 0
j = 0
Dim x() As Double
ReDim x(1 To 3) As Double
Dim y() As Double
ReDim y(1 To 3) As Double
i = 0
For i = 1 To 3 Step 1
inp(i) = vector.Cells(i, 1)
Next i
For i = 1 To 3 Step 1
For j = 1 To 2 Step 1
If GEM(i, j) = inp(1) Then
x(1) = i
y(1) = j
row(i) = row(i) + 1
ElseIf GEM(i, j) = inp(2) Then
x(2) = i
y(2) = j
row(i) = row(i) + 1
ElseIf GEM(i, j) = inp(3) Then
x(3) = i
y(3) = j
row(i) = row(i) + 1
End If
Next j
Next i
i = 0
j = 0
If row(1) > 0 And row(2) > 0 And row(3) > 0 Then
For i = 1 To 3 Step 1
res(i) = GEM(x(i), 2 - y(i))
Next i
Else
If row(1) = 0 Then
a = 0
ElseIf row(2) = 0 Then
a = 1
ElseIf row(3) = 0 Then
a = 2
End If
If row(1) = 1 Then
b = 0
ElseIf row(2) = 1 Then
b = 1
ElseIf row(3) = 1 Then
b = 2
End If
c = 3 - a - b
Dim d As Double
If x(1) = b Then
d = 0
ElseIf x(2) = b Then
d = 1
ElseIf x(3) = b Then
d = 2
End If
i = 0
For i = 1 To 3 Step 1
If i = d Then
res(i) = GEM(x(i), 2 - y(i))
Else
res(i) = GEM(a, y(i))
End If
Next i
End If
Get1_GEM = res
End Function

VBA Runtime error 1004 range of object_global failed

If the cell has a value not equal to 0 then it will add up, X = X +1 so then I can see how many pages I need to print. I'm not sure if this is the best way to do it, but here is what I have so far.
All help is needed! This is just a side project for work, so time is not an issue. I am creating this in excel also.
Sub Find_Pages_to_Print()
Sheets("Invoice").Select
Dim X As Integer
If Not Range("D9").Value = 0 Then
X = X + 1
If Not Range("D41").Value = 0 Then
X = X + 1
If Not Range("D70").Value = 0 Then
X = X + 1
If Not Range("D100").Value = 0 Then
X = X + 1
Else:
X = X + 0
If Not Range("D131").Value = 0 Then
X = X + 1
If Not Range("D161").Value = 0 Then
X = X + 1
If Not Range("191").Value = 0 Then
X = X + 1
Else:
X = X + 0
If Not Range("D221").Value = 0 Then
X = X + 1
End If
End If
End If
End If
End If
End If
End If
End If
MsgBox "Sets to Print: " & X
End Sub
The problem is at line:
If Not Range("191").Value = 0 Then
you probably want
If Not Range("D191").Value = 0 Then

Resources