Display formula in cell vba - vba

I'd like to make the cell to appear as follows()
Desired outcome-- as I hover over cell
'E15'=(1+9.27%)*(1+C15)-1" and as I hover away the equation is evaluated
What I don't want E15 = (0.0000007)-1
I have two formulas that I was working on but I just can't seem to get it right.
I'd appreciate any help. Thanks in advance.
Range("E15").Formula = "=(" & 1 + "(" & Range("D15").Value & )"") * (" & 1 + "(" & Range("C15") & "))" - 1"
Range("E15").Formula = "=(" & 1 + "(" & Range("D15").Value & ")" & ") * (" & 1 + "(" & Range("C15") & ") & ") - 1"

Running:
Sub Macro1()
With Range("E15")
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Chr(39) & .Address(0, 0) & Chr(39) & " " & .Formula
End With
End Sub
produces:
NOTE:
It is equally easy to place the Comment in an adjacent cell.

Related

Quotes for String and Cell Reference

I need to convert an excel formula into VBA codes, however I tried many times but no luck.
Below is the formula which I need to convert.
=IF(G7959<0, "Delivered to end customer " & E7959, "To be delivered to end customer " & E7959)
Below is what I have tried but did not success,the parameter 'IC_inventory_new_row' represent a variable row index. Could anyone help to take a look and advise?
IC_inventory_WS.Cells(IC_inventory_new_row, 15).Formula = "=IF(G" & IC_inventory_new_row + 1 & "<0, " & "Delivered to end customer" & "E" & IC_inventory_new_row & "," & "To be delivered to end customer " & "E" & IC_inventory_new_row & ")"
Try, please:
IC_inventory_WS.Cells(IC_inventory_new_row, 15).formula = "=IF(G" & IC_inventory_new_row + 1 & _
"<0, ""Delivered to end customer ""&E" & IC_inventory_new_row & _
", ""To be delivered to end customer ""&E" & IC_inventory_new_row & ")"
But it can be written in a way to not needing iteration. I mean, to drop it in all the range at once. Try this way which should do what you need, if i correctly understood what is needed:
IC_inventory_WS.Range("O2:O" & lastRow).formula = "=IF(" & Range("G2").address(0, 0) & _
"<0, ""Delivered to end customer ""&" & Range("E2").address(0, 0) & _
", ""To be delivered to end customer ""&" & Range("E2").address(0, 0) & ")"
If not, please specify what does it against what you really need.

Invalid qualifier Error Message in vba code

This code is designed to detect the columns of start and finish of a shape which is used and displayed onto the caption of the shape itself. The following code is the problematic code:
Sub Take_Baseline()
Dim forcast_weeksStart() As String
Dim forcast_weeksEnd() As String
Dim forcastDate As String
Dim shp As Shape
Dim split_text() As String
'cycle through all the shapes in the worsheet and enter the forcast date for all the projects into their respective boxes
For Each shp In ActiveSheet.Shapes
'initialize forcast date by parsing
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
forcast_weeksEnd = Split(shp.BottomRightCell.Column.Text, " ")
forcastDate = forcast_weeksStart(1) & "-" & forcast_weeksEnd(1)
temp = shp.OLEFormat.Object.Object.Caption
If InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") Then
split_text = Split(shp.OLEFormat.Object.Caption, " ")
For i = 0 To (i = 3)
shp.TextFrame.Characters.Caption = split_text(i) & vbNewLine
Next i
ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "/" & "actualDate"
' ElseIf InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") = 0 Then
'split_text = Split(shp.OLEFormat.Object.Object.Caption, " ")
' For i = 0 To (i = 2)
' shp.OLEFormat.Object.Caption = split_text(i) & vbNewLine
' Next i
'ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & "In Prog" & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "actualDate"
End If
Next shp
'For testing purposes
Sheet4.Range("A20").Value = forcast_weeksStart(1)
Sheet4.Range("A21").Value = forcast_weeksEnd(1) End Sub
The error is an
"invalid qualifier"
message which occurs on line
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
Right on the "column" word. I don't get why this is happening since the actual drop down menu has the column operation which i can select. I have tried everything from changing it to the OLEformat.Object.Caption etc etc. But nothing has worked. I am still relatively new to vba so any help will be appreciated. Thanks

How to Implement Formula into Excel cells using VBA

I want to implement the following formula in to Excel cells
IF(OR(D12>0,C13=""),"",MAX(SUM($C$12:C13)-$D$9,0))
(once formula is applied I should get following result (formula implemented manually):
so i wrote simple macro as below , but unable to implement formula , formula retuns value as "true"
Sub adjustoldbills()
lastRow_sht4 = Sheet4.Range("A" & Rows.Count).End(xlUp).Row
Sheet4.Cells(12, 11) = ""
For i = 1 To lastRow_sht4 - 10
If Sheet4.Cells(11 + i, 1) <> "" Then
'=MAX(SUM($C$12:C15)-$D$9,0)
Sheet4.Cells(12, 4).Formula = "=MAX(SUM($C$12:C" & 12 & ")-$D$9,0)"
Sheet4.Cells(11 + i, 4).Formula = "=(if(or(D" & 11 + i > 0 & ",C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & Chr(34) & Chr(34) & "," & "MAX(SUM($C$12:C" & 12 & i & ")-$D$9,0)"
End If
Next i
End Sub
i am getting wrong result after implemented formula via vba macro as in this image:
how to implement formula and get value as expected.
When trying to convert long formulas to VBA try using a String variable to help you test it.
Dim FormulaStr As String
FormulaStr = "=IF(OR(D" & 11 + i & ">0,C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & _
Chr(34) & Chr(34) & ",MAX(SUM($C$12:C" & 12 + i & ")-$D$9,0))"
Debug.Print FormulaStr
Then, in the immediate window you will get:
=IF(OR(D12>0,C13=""),"",MAX(SUM($C$12:C13)-$D$9,0))
which is the formula you want to convert to VBA.
Now all you need to do is to add the line below:
Sheet4.Cells(11 + i, 4).Formula = FormulaStr
If you want to skip the String variable, you can just replace your line of
Sheet4.Cells(11 + i, 4).Formula = "=(if(or(D" & 11 + i > 0 & ",C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & Chr(34) & Chr(34) & "," & "MAX(SUM($C$12:C" & 12 & i & ")-$D$9,0)"
to:
Sheet4.Cells(11 + i, 4).Formula = "=IF(OR(D" & 11 + i & ">0,C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & Chr(34) & Chr(34) & ",MAX(SUM($C$12:C" & 12 + i & ")-$D$9,0))"
I'd go another way, with no loops and exploiting SpecialCells() method of Range object
Option Explicit
Sub adjustoldbills()
With Sheet4
.Range("D12").FormulaR1C1 = "=MAX(RC[-1]-R9C4,0)"
With .Range("A13", .Cells(.Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeConstants)
With .Offset(, 2).SpecialCells(xlCellTypeConstants)
.Offset(, 1).FormulaR1C1 = "=IF(R[-1]C>0,"""",MAX(SUM(R12C[-1]:RC[-1])-R9C4,0))"
With .Areas(.Areas.Count)
.Offset(, 1).Cells(.Rows.Count).Offset(1).FormulaR1C1 = "=IF(R[-1]C>0,"""",MAX(SUM(R12C[-1]:RC[-1])-R9C4,0))"
End With
End With
End With
End With
End Sub

sumifs formula in vba

I am trying to populate a column with a SUMIFS formula if the criteria is matched.
cell.Offset(0, 2).Value = "=SUMIFS(PickData!E:E,PickData!A:A, _
" & cell.Address(Rowabsolute:=False, Column:=False) & ", PickData!C:C, _
"Retail",PickData!C:C, PickData!L:L, "Report1.TextBox1.Value")"
I can't see where i'm going wrong with it looking up the specific work Retail in PickData|C:C & the value from TextBox1 (this is date)
Any help would be greatly appreciated.
Thanks
Al
If you want a formula in the cell(s) then try this.
cell.Offset(0, 2).Formula = "=SUMIFS(PickData!E:E, PickData!A:A, " _
& cell.Address(0, 0) & ", PickData!C:C, " & Chr(34) & "Retail" & Chr(34) _
& ", PickData!L:L, DATEVALUE(" & Report1.TextBox1.Value & "))"
That should give you a valid SUMIFS(...) formula.
Addendum: looking at that a second time, the form's textbox value might need to be in quotes.
cell.Offset(0, 2).Formula = "=SUMIFS(PickData!E:E, PickData!A:A, " _
& cell.Address(0, 0) & ", PickData!C:C, " & Chr(34) & "Retail" & Chr(34) _
& ", PickData!L:L, DATEVALUE(" & Chr(34) & Report1.TextBox1.Value & Chr(34) & "))"

How to edit cell value in VB.net - Using .Interop.Excel

This is a simple question. I have this code:
CurrentRow = 3
MyColumn = 2
CurrentCell = (CurrentRow & "," & MyColumn)
WorkingSheet.Cells(CurrentCell).value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
I thought that this would place the data on the 'WorkingSheet' in the position "B3" but it places the data in the cell "AF1".
Why is this?
Thanks,
Josh
Cell is not expected to be used as you are using it; you have to input the row and column indices (as integers) separated by a comma. Thus the right code is:
WorkingSheet.Cells(CurrentRow, MyColumn).Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
Another alternative you have is using Range. Example:
WorkingSheet.Range("B3").Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)