Pretty new to VBA. I've been trying to automate a template that my office uses quite frequently, right now the template is just a bunch of coloured cells with copy pasted formulas that constantly get messed up as people try to adjust the size of it themselves. So far I've managed to set it up so that they just enter how many stations they need for the project (which determines how many rows they'll need). Then they press create and the code I've got set up fully creates the table for them. Usually we need more than one table for each project, so I have 5 sheets loaded into the template so it's ready to go for anyone to use as not everyone would be able to assign the macro's if I only gave them one and they were to copy paste the sheets.
So now I need to have the first page provide a total of each material from every sheet's table (stripping, road cut, etc). The main reason I'm struggling with this is because the cells that need to be summed could be in any row depending on how many stations they set up in each table.
Any help on this would be great, also please don't judge my coding too harshly since I've only got about 8 hours total trying to teach myself how to use it as coding isn't something I went to school for or is in my job description...just trying to help the office out with a side project! :)
Sub Test()
Dim x As Integer
x = Range("Y3").Value
Range("c8:e8").Copy Range("C9:E" & (x + 8 - 2))
Range("g8:i8").Copy Range("g9:i" & (x + 8 - 2))
Range("k8:m8").Copy Range("k9:m" & (x + 8 - 2))
Range("o8:q8").Copy Range("o9:q" & (x + 8 - 2))
Range("s8:u8").Copy Range("s9:u" & (x + 8 - 2))
Range("C" & (x + 8 - 1), "E" & (x + 8 - 1)).Value = "-"
Range("g" & (x + 8 - 1), "i" & (x + 8 - 1)).Value = "-"
Range("k" & (x + 8 - 1), "m" & (x + 8 - 1)).Value = "-"
Range("o" & (x + 8 - 1), "q" & (x + 8 - 1)).Value = "-"
Range("s" & (x + 8 - 1), "u" & (x + 8 - 1)).Value = "-"
Range("B" & (x + 8), "D" & (x + 8)).Value = "sum row"
Range("F" & (x + 8), "H" & (x + 8)).Value = "sum row"
Range("J" & (x + 8), "L" & (x + 8)).Value = "sum row"
Range("N" & (x + 8), "P" & (x + 8)).Value = "sum row"
Range("R" & (x + 8), "T" & (x + 8)).Value = "sum row"
'lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
Range("E" & (x + 8)).Value = "=SUM(E8:E" & (x + 6) & ")"
Range("I" & (x + 8)).Value = "=SUM(I8:I" & (x + 6) & ")"
Range("M" & (x + 8)).Value = "=SUM(M8:M" & (x + 6) & ")"
Range("Q" & (x + 8)).Value = "=SUM(Q8:Q" & (x + 6) & ")"
Range("U" & (x + 8)).Value = "=SUM(U8:U" & (x + 6) & ")"
Range("E" & (x + 9)).Value = "Border"
Range("I" & (x + 9)).Value = "Border"
Range("M" & (x + 9)).Value = "Border"
Range("Q" & (x + 9)).Value = "Border"
Range("U" & (x + 9)).Value = "Border"
Range("A" & (x + 8)).Value = "TOTALS"
End Sub
Sorry for the super low quality GIF, hopefully it helps show what I've done at a basic level at least.
Related
I am having trouble figuring out why my IF formula in VBA is not returning any of the criteria values. All I can get is the True or False value. This is what I currently have:
Cells(y, 11).Formula = "=IF((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & "))" > "($J$7*20),(((C" & y & ")-(C" & y - 1 & ")) * $J$7),(((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))"
Im sure its just a type-o or I am missing some quotations somewhere but if anyone can take a look at this and find what I am missing, that would be great.
This is my whole code for better context.
Sub make_new_entry()
Dim Layer_Increment, i As Integer
Set Layer_Increment = Cells(7, 2)
y = 10 'Set this to the first row number that gets a time entered.
i = 0
Do While i = 0
If Cells(y, 1) = "" Then
add_row y
Cells(y, 1) = Format(Now(), "m/d/yy")
Cells(y, 2) = Format(Now(), "h:mm")
Cells(y, 3) = Cells(y - 1, 2) - ((Cells(y - 1, 2) - Layer_Increment * (y - 10)))
Cells(y, 8).Formula = "=($H$7*(C" & y & "))+((0.5*'Data Reference'!F2)+(0.4*'Data Reference'!F3)+(0.3*'Data Reference'!F4)+(0.2*'Data Reference'!F5)+(0.1*'Data Reference'!F6)+(0.05*'Data Reference'!F7)+(0.01*'Data Reference'!F8)+(0.001*'Data Reference'!F9))+ $H$10+$N$7"
Cells(y, 9).Formula = "=$I$10-(0.05*(C" & y & "-$C$10))"
Cells(y, 10).Formula = "=(((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))/((C" & y & ")-(C" & y - 1 & "))"
Cells(y, 11).Formula = "=IF(A" & y & " + B" & y & ")-(A" & y - 1 & " + B" & y - 1 & ") > $J$7*20, (C" & y & " - C" & y - 1 & ") * $J$7, (A" & y & " + B" & y & ")-(A" & y - 1 & " + B" & y - 1 & ")"
Cells(7, 6).Formula = "=((130-(H" & y & "))/$H$7)-('Build Information'!$F$6-(C" & y & "))"
Cells(7, 4).Formula = "=Max(C:C)"
Cells(7, 10).Formula = "=AverageIf(J11:J" & y & ",""<00:05:00"")"
Cells(8, 11).Formula = "=Sum($K$10:(K" & y & ")"
i = 1
Else
y = y + 1
End If
Loop
End Sub
I suggest that you:
fully qualify your ranges
check your parenthesis and the order the calculations are made in Excel
use variable names that are easy to understand (for example, instead of y use counter) .
Regarding your formula, here is the way I'd fix it
Cells(y, 11).Formula = "=IF((A" & y & "+B" & y & ")-(A" & y - 1 & "+B" & y - 1 & ") > $J$7*20,((C" & y & "-C" & y - 1 & ") * $J$7),((A" & y & "+B" & y & ")-(A" & y - 1 & "+B" & y - 1 & ")))"
PS To fully qualify a range use this syntax
Thisworkbook.Worksheets("Sheet1").Cells(y,11).Formula =
Im trying to use the AverageIf function in my code that uses a cell as criteria. This cell is also in the same macro. When I do this I get "False" returned instead of a value. This is what I have. {Sub make_new_entry()
Dim Layer_Increment, i As Integer
Set Layer_Increment = Cells(7, 2)
y = 10 'Set this to the first row number that gets a time entered.
i = 0
Do While i = 0
If Cells(y, 1) = "" Then
add_row y
Cells(y, 1) = Format(Now(), "m/d/yy")
Cells(y, 2) = Format(Now(), "h:mm")
Cells(y, 3) = Cells(y - 1, 2) - ((Cells(y - 1, 2) - Layer_Increment * (y - 10)))
Cells(y, 8).Formula = "=($H$7*(C" & y & "))+((0.5*'Data Reference'!F2)+(0.4*'Data Reference'!F3)+(0.3*'Data Reference'!F4)+(0.2*'Data Reference'!F5)+(0.1*'Data Reference'!F6)+(0.05*'Data Reference'!F7)+(0.01*'Data Reference'!F8)+(0.001*'Data Reference'!F9))+ $H$10+$N$7"
Cells(y, 9).Formula = "=$I$10-(0.05*(C" & y & "-$C$10))"
Cells(y, 10).Formula = "=((((A" & y & ") + (B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))*1440)/((C" & y & ") - (C" & y - 1 & "))"
Cells(y, 11).Formula = "=((B" & y & ") + (A" & y & ")) - ($B$10+$A$10)"
Cells(7, 6).Formula = "=((130-(H" & y & "))/$H$7)-('Build Information'!$F$6-(C" & y & "))"
Cells(7, 4).Formula = "=Max(C:C)"
Cells(1, 15).Formula = "=(J" & y - 1 & ")+1.5"
Cells(7, 10).Formula = "=AverageIf(J11:(J" & y & ")," < " &O1)"
i = 1
Else
y = y + 1
End If
Loop
End Sub}
Can anyone help me figure out what Im doing wrong.
You need to double up the quotes around the < to actually include it in the string (it is currently outside the quotes and thus a comparison operator). Then, your parentheses are off:
Cells(7, 10).Formula = "=AverageIf(J11:J" & y & ",""<""&O1)"
After the second round of my code the while wend loop stops because the field seems empty, even if I look after it in the watch window. But the Excel table is full with stuff from Cells 1:1 to 12:92...
x = 1
y = 2
While Cells(x & 1) <> ""
If Cells(x & 1) = cmb_spalte Then
While Cells(x & y) <> ""
If Cells(x & y) = txt_suchwert Then
meineDatei.WriteLine (Range("C" & y) & vbTab & vbTab & vbTab & Range("G" & y) &
vbTab & vbTab & Range("K" & y))
y = y + 1
End If
Wend
End If
x = x + 1
Wend
I am doing an average of data (VBA Excel) as below:
If n < 8 Then
Sheet2.Cells(i, 20).Value = "=SUM(E" & i & ":S" & i & ")/" & n
Else
Sheet2.Cells(i, 20).Value = "=AVERAGE(LARGE(E" & i & ":S" & i & ", {1,2,3,4,5,6,7,8}))"
End If
n = 0
This code is working, because I want the best 8 values of 15 values.
My question is how to do the same with x values (x will be introduced via a userform, 0<x<16).
Of course, I could use a Select Case with 15 lines depending of x, but it does not seem to me a good coding.
Any ideas?
If AVERAGEIF is available:
Sheet2.Cells(i, 20).Value = "=AVERAGEIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))"
For i = 2 and x = 8, the formula is:=AVERAGEIF(E2:S2,">="&LARGE(E2:S2,8))
If not:
Sheet2.Cells(i, 20).Value = "=SUMIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))/" & x
For i = 2 and x = 8, the formula is:=SUMIF(E2:S2,">="&LARGE(E2:S2,8))/8
Can someone help me get the correct formula?
I want to calculate the following:
SQRT((Rng2^2) + (Rng4^2))
Here is my code but it is not working
Cells(LastRow + 1, LastCol + 4) = "=SQRT((Rng2.Address)^2 & " + " & (Rng4.Address)^2)"
Your concatenation is in the wrong places:
Cells(LastRow + 1, LastCol + 4) = "=SQRT((" & Rng2.Address & ")^2 + (" & Rng4.Address & ")^2)"