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)"
Related
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.
I have the following vba code, but I want to paste it giving the format of dd-mm-yyyy.
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
I've tried:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr.Numberformat = ('dd-mm-yyyy')
I am unsure on the format of this. Where do I put numberformat?
on a different line, two actions:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).NumberFormat = "dd-mm-yyyy"
But we can shorten it a little with With and Resize
With Worksheets("stack").Range("M" & LastRowM + 1).resize(Ubound(pasteArr,1),18)
.Value = PasteArr
.NumberFormat = "dd-mm-yyyy"
End With
I am applying following formula via vba and it throws error.
Cells(MyRow1 + 4, 3).Formula = "=RIGHT(" & Cells(MyRow1 + 2, 2).Address & ",LEN(" & Cells(MyRow1 + 2, 2).Address & ")-FIND(" & Cells(MyRow1 + 3, 3) & "," & Cells(MyRow1 + 2, 2).Address & ")-2)"
The output I get in the cell is
=RIGHT($B$31,LEN($B$31)-FIND(CA,$B$31)-2)
If I apply "" in Find function, it works:
=RIGHT($B$31,LEN($B$31)-FIND("CA",$B$31)-2)
To add the extra " i use the Chr(34).
change your Formula to:
Cells(MyRow1 + 4, 3).Formula = "=RIGHT(" & Cells(MyRow1 + 2, 2).Address & _
",LEN(" & Cells(MyRow1 + 2, 2).Address & ")-FIND(" & Chr(34) & _
Cells(MyRow1 + 3, 3) & Chr(34) & "," & Cells(MyRow1 + 2, 2).Address & ")-2)"
I'm not entirely sure what you're trying to do with this formula, but if you want to include the quotations in the output to the cell you can simply escape them, by using double-quotations.
... FIND(""" & Cells(MyRow1 + 3, 3) & """," & ...
I am copying data to a cell on the basis of some condition.
If (StrComp(Trim(sh333.Cells(i333 + 1, jHistory).Value), Trim(sh333.Cells(i333 + 2, jHistory).Value), vbTextCompare)) = 0 Then
MsgBox "history Comments and Last comments are Same / Equal"
sh44.Cells(counterFinalHistory, 7).Value = Trim(sh333.Cells(i333 + 1, jHistory).Value) '& vbNewLine & sh333.Cells(i333 + 2, 3).Value & ":" & sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value
sh44.Cells(counterFinalHistory, 8).Value = Trim(sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value)
Else
MsgBox "history Comments and Last comments are Not Same / Different"
sh44.Cells(counterFinalHistory, 7).Value = Trim(sh333.Cells(i333 + 1, jHistory).Value & vbNewLine & sh333.Cells(i333 + 2, 3).Value & ":" & sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value)
sh44.Cells(counterFinalHistory, 8).Value = Trim(sh333.Cells(i333 + 2, 3).Value & ":" & sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value)
End If
There are 2 disturbing trends here:
1) The strings in the 2 cells sh333.Cells(i333 + 1, jHistory).Value And sh333.Cells(i333 + 2, jHistory).Value are exactly equal still StrComp goes in the Else part and says that "history Comments and Last comments are Not Same / Different"
2) Also when I am copying the the contents of sh333.Cells(i333 + 1, jHistory) to sh333.Cells(i333 + 2, jHistory) then StrComp behaves correctly and says "history Comments and Last comments are Same / Equal"
Please do let me know the reason for this erratic behaviour of StrComp function of VBA. I have broken my head over this but found nothing to correct the behaviour. Please do help and suggest.
I tried:
Sheet7.Cells(j, 5) = Sheet7.Cells(j, 4) + "," + Sheet7.Cells(j, 1) + "," + Sheet7.Cells(j, 2) + "," & Sheet7.Cells(j, 3)
And my output was:
2016-07-28
,00000152009908151933
,46389789462,789764654523279
But I was expecting to be in 1 line as the following:
2016-07-28,00000152009908151933,46389789462,789764654523279
If you want to break your output into two lines use the formula this way:
Sheet7.Cells(j, 5) = Sheet7.Cells(j, 4) + "," + Sheet7.Cells(j, 1) + "," + vblf + Sheet7.Cells(j, 2) + "," & Sheet7.Cells(j, 3)
You are probably facing a viewing problem. Add another line to your code which will expand the column 5 for viewing entire cell in one row or line.
Sheet7.Cells(j, 5) = Sheet7.Cells(j, 4) + "," + Sheet7.Cells(j, 1) + "," + Sheet7.Cells(j, 2) + "," & Sheet7.Cells(j, 3)
Sheet7.Columns(5).EntireColumn.AutoFit