StrComp function in excel vba is giving wrong result. - excel

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.

Related

Deleting chr160 characters in excel using vba

In the following code I would like to delete the char 160 and 34 characters at the end of the text string.
For Column = 12 To 13
For i = 16 To last_row
'sString = ("=CODE(160)")
'Range(Cells(i, Column), Cells(i, Column)).Select
'Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Value = Replace(Range(Cells(i, Column), Cells(i, Column)).Value, sString, "", 1, -1)
Set current_cell = Range(Cells(i, Column), Cells(i, Column))
current_cell.Select
text_to_change = Range(Cells(i, Column), Cells(i, Column)).Value
Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Select
Selection.NumberFormat = "General"
With Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column))
.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")"
.Value = .Value
End With
Next i
Next Column
I'm getting an error at this line:
.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")".
The error message indicates:
Run-time error '1004': Application-defined or object-defined error.
Could you help me what is wrong here?
You need to escape your quote marks:
.Formula = "=SUBSTITUTE(""" & text_to_change & """;CODE(160);""" & Chr(34) & Chr(34) & """)"

What is wrong with this code? Keeps getting application defined error

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))"

VBA, NumberFormat when pasting an array

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

SQRT formula in VBA

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)"

Excel VBA Formula variable find data not working

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) & """," & ...

Resources