Placing values generated from one sheet, into a new sheet - vba

I have a nested for loop that is looking to take values in one row, generate a value based on an equation, then do the same for many rows following the first one, all while adding the value together.
Essentially, if row one has a value of 15, and row 2 and 3 return values of 10 and 12, the variable storing the total value (named genCost) will be 37.
I want to place the summed total values of genCost in a new sheet, separated by day, but when I run the code I get a Run-time 1004 error. I realize that this has something to do with the sheet that I am working on, and the sheet that I am trying to place the values into (the 2nd to last line in the code).
I understand my code may be ugly and simple, but can somebody help me troubleshoot this?
'Nested For loop for ALL OTHER DAYS of genCost...only 1 formula
For j = 2 To dayNumber
For i = 1 To increments
'IF(AND(U7=1,U6=0),R7,0)
If Cells(rowValue, 21) = 1 And Cells(rowValue - 1, 21) = 0 Then
ifValue = Cells(rowValue, 18)
Else
ifValue = 0
End If
'calculate value variable with second half of equation
value = (((Cells(rowValue, 23) * Cells(rowValue, 16) * (1 / 6)) + (Cells(rowValue, 25) * Cells(rowValue, 17) * (1 / 6)) + (Cells(rowValue, 21) * Cells(rowValue, 19) * (1 / 6)) + ifValue))
genCost = genCost + value
'set value and ifValue back to zero and step down one row and do again
value = 0
ifValue = 0
rowValue = rowValue + 1
Next i
Cells(3, j) = genCost
Dim genCostRefNum As Integer: genCostRefNum = 6
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
genCost = 0
Next j

Instead of this
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
write this
ThisWorkbook.Sheets(1).Cells(genCostRefNum, 4) = genCost
There is a default property of Cells statement, which is Value (same for Range). So your code was exactly
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4).Value).Value = genCost
and after executing Cells something like:
ThisWorkbook.Sheets(1).Range(6).Value = genCost
It's good practice to always write complete statements and don't rely on default properties.

Related

VLookup analogue in VBA, printing two columns based on StrComp with common first column

I'm updating a VBA script and trying to match a 4-digit code with a table and printing the two corresponding columns into my original sheet, plus handling codes missing from the reference table.
jobcodes = sample codes to match.
codematch = reference table, 1st column is reference codes, I want the corresponding values in columns 2 and 3 in K and L of "jobcodes".
At the minute I'm getting blank values in the first two rows, then #N/A errors in the rest of the sample table.
finrow3 = jobs.Cells(Rows.Count, 1).End(xlUp).Row
jobcodes = jobs.Range(("J2"), ("L" & finrow3)).Value
codematch = stat.Range("I2:K143").Value
For i = 1 To finrow3 - 1
For j = 1 To UBound(codematch, 1)
If StrComp(jobcodes(i, 1), codematch(j, 1)) = 0 Then
resulta(z, 1) = codematch(j, 2)
resulta(z, 2) = codematch(j, 3)
Else
resulta(z, 1) = ""
resulta(z, 2) = ""
End If
Next j
Next i
jobs.Range(("K2"), ("L" & finrow3)).Value = Application.Transpose(resulta)

Do loop until 2 things in column do not equal. Pulling information from another sheet

I am trying to pull information from another sheet and then have it check to make sure 2 cells in this sheet are not the same. It is for a randomized meal plan, but on the same day you can not eat the same protein (to give you a little background). I have the code checking to see if it can be used during that meal and if it can be it is put into the next sheet. It does this for lunch and dinner. It is getting caught in the do - loop until loop and I am not sure how to solve this.
If you need anymore information I can provide screenshots if this does not make sense.
Do
RandNumLunch = Int((10 - 2 + 1) * Rnd + 2)
Do Until Sheet2.Cells(RandNumLunch, 6) = "Yes"
If Sheet2.Cells(RandNumLunch, 6) = "Yes" Then
Else
RandNumLunch = Int((10 - 2 + 1) * Rnd + 2)
End If
Loop
If Sheet2.Cells(RandNumLunch, 6) = "Yes" Then
Sheet3.Cells(3, 2) = Sheet2.Cells(RandNumLunch, 1)
End If
RandNumDinner = Int((10 - 2 + 1) * Rnd + 2)
Do Until Sheet2.Cells(RandNumDinner, 7) = "Yes"
If Sheet2.Cells(RandNumDinner, 7) = "Yes" Then
Else
RandNumDinner = Int((10 - 2 + 1) * Rnd + 2)
End If
Loop
If Sheet2.Cells(RandNumDinner, 7) = "Yes" Then
Sheet3.Cells(4, 2) = Sheet2.Cells(RandNumLunch, 1)
End If
Loop Until Sheet3.Cells(4, 2) <> Sheet3.Cells(3, 2)
In your final If..Else, you use RandNumLunch where you should be using RandNumDinner. This means that the whatever happens in between, you set Sheet3.Cells(3, 2) and Sheet3.Cells(4, 2) to the same value, so the final check can never be true.

Simple Nested For Loop

I'm trying to paste "Area1" 50 times, and "Area2" 50 time right below "Area1" in the same column. My codes run the first 50 times, and replace the first 50 rows when running Area=2. How do I make "Area2" start on a new row, under "Area1"? Thank you :))
For Area = 1 To 2
For Row = 1 To 50
Sheets("A").Cells(Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
There are many ways to do this. A few of them would be:
For Area = 1 To 2
For Row = 1 To 50
Sheets("A").Cells((Area - 1) * 50 + Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
or
For Area = 1 To 2
For Row = (Area - 1) * 50 + 1 To (Area - 1) * 50 + 50
Sheets("A").Cells(Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
or changing Area to be zero-based
For Area = 0 To 1
For Row = 1 To 50
Sheets("A").Cells(Area * 50 + Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
or avoiding loops
Sheets("A").Cells(1, 2).Resize(50, 1) = Sheets("A").Cells(2, 31)
Sheets("A").Cells(51, 2).Resize(50, 1) = Sheets("A").Cells(2, 31)
or, because you are copying the same value to the destination cells, simply
Sheets("A").Cells(1, 2).Resize(100, 1) = Sheets("A").Cells(2, 31)
One way to do this is creating another variable and increment it, in second loop, each time +1. Like this:
For Area = 1 To 2
For Row = 1 To 50
myRow = myRow + 1
Sheets("A").Cells(myRow, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area

How to setting textbox in userform for fractional value?

I have two problems about the fraction number in user form textbox.
How to retrieve a value from excel sheet and show it in the textbox with fraction. For example 0.5(sheet) will be shown as 1/2 (textbox). not all values are fraction, there are also value for integer
This is the code to retrieve the value from the sheet
Set ctlTXT = Me.SizeFrame.Controls.Add("Forms.TextBox.1")
ctlTXT.name = "OD" & counter
ctlTXT.value = Sheet2.Range("P" & findstart + counter - 1).value
ctlTXT.Left = 72
ctlTXT.Height = 15: ctlTXT.Width = 54
ctlTXT.Top = 45 + ((counter - 1) * 17 + 2)
How to return the fractional value inserted in the user form textbox using case select statement. From the code below, there is no value return when 1/2 or 3/4 inserted in textbox.
This is the code for case select
Select Case X
Case "1 / 2"
Y = 15
Case "3 / 4"
Y = 20
Case 2
Y = 40
End Select
Both of these code is not a complete code.
For the first one, you can try
ctlTXT.Value = WorksheetFunction.Text(Sheet2.Range("P" & findstart + counter - 1).Value, "#?/?")

Returning correct value from VBA script to Excel cell

I have a method I wrote in a VBA module
Public Function calcAscentTime()
IDepth = CInt(Sheets("Fundies").Range("B47"))
T = 0
T = T + 1 ' Add 1 minute for emergency
D = Math.Round((IDepth / 10) * 10) 'Round to Ceiling of nearest 10
half_depth = Math.Round(((D / 2) / 10) * 10, 0) 'Get where our first stop is
T = T + Math.Round((((D - half_depth) / 30) / 2) * 2) ' Ascend to first stop at 30ft/min
T = T + (half_depth / 10) ' 1 minute for every stop thereafter
What this does is take a value from Cell B47 on the "Fundies" worksheet and should return a value based on the calculations. I enter =calcAscentTime() into cell B48, expecting to get a value of 8(B47's value is 100), but get a return value of 0. What am I doing wrong?
ou're function isn't returning anything. Assigning the function name to the calculated variable should return the expected result (assuming you have everything else right). Something like:
Public Function calcAscentTime()
IDepth = CInt(Sheets("Fundies").Range("B47"))
T = 0
T = T + 1 ' Add 1 minute for emergency
D = Math.Round((IDepth / 10) * 10) 'Round to Ceiling of nearest 10
half_depth = Math.Round(((D / 2) / 10) * 10, 0) 'Get where our first stop is
T = T + Math.Round((((D - half_depth) / 30) / 2) * 2) ' Ascend to first stop at 30ft/min
T = T + (half_depth / 10) ' 1 minute for every stop thereafter
calcAscentTime = T
End Function
Keep in mind, you should probably Dim your variables to the proper type to avoid any kind of confusion / miss-casting by the compiler.