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
Related
The code is working for 3 times in Excel VBA but at the forth time it gives an error. When I put a toggle point there, the code cannot pass "Duration = Duration + drtn" and "Type MisMatch" error shows up. Is there any way to fix the code?
Function simul(t)
Dim wk As Worksheet
Set wk = Worksheets("Simulation")
Dim FoundCell As Range
Duration = 0
i = 1
char = "StartStep"
drtn = 0
While i = 1
m = WorksheetFunction.CountIf(Range("A2:A30"), char)
Set FoundCell = wk.Range("A:A").Find(char)
x = FoundCell.Row
y = FoundCell.Row
Dim myarray() As Variant
ReDim myarray(m)
For j = 0 To m - 1
myarray(j) = wk.Range("E" & x)
x = x + 1
Next
If char = "A" Then
drtn = Application.Evaluate("LOGNORM.INV(RAND(),r5,r6)")
ElseIf char = "B" Then
drtn = Application.Evaluate("ABS(NtLogisticInv(RAND(),r29,r30)")
ElseIf char = "C" Then
drtn = Application.Evaluate("r17*(-LN(1-RAND()))^(1/r18)")
Else
i = 0
drtn = 0
End If
Duration = Duration + drtn
r = Rnd()
k = 0
While r > myarray(k)
k = k + 1
Wend
char = wk.Range("B" & y + k)
Wend
wk.Range("U" & t) = Duration
End Function
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
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
Trying to solve an equation using excel-vba,
Equation:
xy = 5(x+y)
I use the below vba code to determine the values of x and y and print it in cell A1
Sub equation()
Dim x As Long, y As Long
For x = 1 To 9
For y = 1 To 9
If ((x * y) = (5 * (x + y))) Then
Range("A1") = x & "," & y
End If
Next y
Next x
End Sub
However this code is not working. I guess I am missing something in the IF condition.
I tried the below and this works perfectly,
Sub equation()
Dim x As Long, y As Long
For x = 1 To 9
For y = 1 To 9
If 5 * (x + y) = 45 Then
Range("A1") = x & "," & y
End If
Next y
Next x
End Sub
I even tried with 2 temp variables, but doesn't works,
Sub equation()
Dim x As Long, y As Long, temp1 As Long, temp2 As Long
For x = 1 To 9
For y = 1 To 9
temp1 = x * y
temp2 = 5 * (x + y)
If temp1 = temp2 Then
Range("A1") = x & "," & y
End If
Next y
Next x
End Sub
Can someone tell me what I am doing wrong with the IF condition.
Consider:
xy = 5(x+y)
xy = 5x+5y
xy-5y = 5x
y(x-5) = 5x
y = 5x/(x-5)
Then just plug in values for x and solve for y
Sub qwerty()
Dim x As Long, y As Long
For x = 6 To 13
y = 5 * x / (x - 5)
Cells(x, 1) = x
Cells(x, 2) = y
Next x
End Sub
Pick any values for x, just avoid x = 5.Declare y as Double if you require the fractional part:
Please help me to fix this,
Requirement:
Pasting data from sheet 1 to sheet x and skip to next page.
Problem:
I am unable to run the loop between 2 integers at a time.
I want to run the loop between x and y every time.But the written code is finishing x first and the going to y.
Please check below code and help me with u r ideas. Thank you.
Sub sbCopyValueToAnotherSheet()
Dim x As Integer
Dim y As Integer
For y = 2 To 11
For x = 2 To 50
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("F6")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P6")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P7")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("F8")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P8")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("F9")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P9")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy
Destination:=ActiveSheet.Range("F10")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy
Destination:=ActiveSheet.Range("P10")
y = y + 1
ActiveSheet.Next.Select
Next x
Next y
End Sub
If you are trying to copy 50 rows from Sheet1 into the same cells on 50 different sheets, try this:
Option Explicit
Public Sub sbCopyValueToAnotherSheet()
Dim x As Long, wsM As Worksheet, wsCount As Long
wsCount = ThisWorkbook.Worksheets.Count
Set wsM = ThisWorkbook.Worksheets("Sheet1")
For x = 2 To 50
With ThisWorkbook.Worksheets(x)
.Range("F6") = wsM.Cells(x, 2)
.Range("F7") = wsM.Cells(x, 3)
.Range("F8") = wsM.Cells(x, 4)
.Range("F9") = wsM.Cells(x, 5)
.Range("F10") = wsM.Cells(x, 6)
.Range("P6") = wsM.Cells(x, 7)
.Range("P7") = wsM.Cells(x, 8)
.Range("P8") = wsM.Cells(x, 9)
.Range("P9") = wsM.Cells(x, 10)
.Range("P10") = wsM.Cells(x, 11)
End With
If x = wsCount Then Exit Sub
Next
End Sub