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) & """," & ...
Related
Keep prompting application defined error, I do no think there is anything wrong
ActiveSheet.Cells(salesNo + 1, 5).Formula = "=CONCATENATE(""Subtotal for ""& Cells(salesNo - 1, 3).Address)"
ActiveSheet.Cells(salesNo + 1, 5).Formula = _
"=""Subtotal for "" & " & Cells(salesNo - 1, 3).Address
ADDRESS in Excel
ActiveSheet.Cells(salesNo + 1, 5).Formula _
= "=""Subtotal for ""&ADDRESS(" & salesNo - 1 & ",3)"
or
ActiveSheet.Cells(salesNo + 1, 5).Formula = "=" & Chr(34) _
& "Subtotal for " & Chr(34) & "&ADDRESS(" & salesNo - 1 & ",3)"
To get rid of the $ signs:
ActiveSheet.Cells(salesNo + 1, 5).Formula _
= "=""Subtotal for ""&ADDRESS(" & salesNo - 1 & ",3,4)"
or
ActiveSheet.Cells(salesNo + 1, 5).Formula = "=" & Chr(34) _
& "Subtotal for " & Chr(34) & "&ADDRESS(" & salesNo - 1 & ",3,4)"
EDIT
ActiveSheet.Cells(salesNo + 1, 5).Formula _
= "=""Subtotal for ""&INDIRECT(ADDRESS(" & salesNo - 1 & ",3,4))"
or
ActiveSheet.Cells(salesNo + 1, 5).Formula = "=" & Chr(34) _
& "Subtotal for " & Chr(34) & "&INDIRECT(ADDRESS(" & salesNo - 1 & ",3,4))"
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 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'm trying to write VBA code to sum up cells, in the process i calculate the latest updated column using the statement:
clncnt1 = wb.Worksheets("Sheet1").Range("XFD16").End(-4159).Column
Suppose I capture the clncnt1 as 20 which is column P. Now how do I use:
wb.Worksheets("sheet1").Range("A1").Offset(16, clncnt).Formula = "=sum(*P12:P13*)"
using the column number clncnt1 that I just captured?
replace your sum with below
"=sum(" & Cells(12, clncnt1 ).Address & ":" & Cells(13, clncnt1 ).Address & ")"
you are actually getting the addresses of the range and then concatenating it all together.
Full code:
clncnt1 = Worksheets("sheet1").Range("XFD16").End(-4159).Column
Worksheets("sheet1").Range("A1").Offset(16, clncnt1).Formula = "=sum(" & Cells(12, clncnt1).Address & ":" & Cells(13, clncnt1).Address & ")"
this worked wb.Worksheets("sheet1").Range("A1").Offset(11, clncnt + 3).Formula = "=sum(" & _ wb.Worksheets("sheet1").Range("A1").Offset(8, clncnt + 3).Address & ":" & _ wb.Worksheets("sheet1").Range("A1").Offset(9, clncnt + 3).Address & ")"
I need to write in the cells from Cells(1,1) to Cells(501,1) the formula
=FDSC("-",$C$3,"FG_PRICE(D1)") for line 1
=FDSC("-",$C$3,"FG_PRICE(D2)") for line 2
=FDSC("-",$C$3,"FG_PRICE(D3)") for line 3
Now my vba code is the following one :
Range(Cells(1, 1), Cells(1 + 500, 1)).FormulaR1C1 = "=FDSC(""" & "-" & """,R1C3,""" & " FG_PRICE(RC[3]) " & """)"
I've tested everypart of the code to determine that the last part (FG_PRICE...) is the one that is not working!
The formule that is written in Excel is :
=FDSC("-",$C$1,"FG_PRICE(RC[3])")
How can i do to have D1,D2,etc... and not RC[3]??? I've tried a few things but i can't find it!
Thank you very much for your time and your help!
Use chr(34) to represent quotation marks when you are already working inside quotation marks. For example, change your forumula above to the following:
Range(Cells(1, 1), Cells(1 + 500, 1)).FormulaR1C1 = "=FDSC(" & chr(34) & "-" & chr(34) & ",$C$1," & chr(34) & "FG_PRICE(D1)" & chr(34) & ")"
To put it in a loop, just do the following:
Sub writeFormulas()
Dim i as Integer
For i = 0 to 2
Range(Cells(1, 1), Cells(1 + 500, 1)).FormulaR1C1 = "=FDSC(" & chr(34) & "-" & chr(34) & ",$C$1," & chr(34) & "FG_PRICE(D" & i+1 & ")" & chr(34) & ")"
next i
End Sub