Inserting value into Excel cell instead of formula - excel

I have a VBA script that inserts long strings into Excel cells. In some cases, the string begins with a =. It seems like Excel interprets this as a formula and I get an Out of Memory error due to the memory limitations of formulas.
How do I tell Excel that I am writing a value, not a formula? Currently, I am doing this:
ws.Range("A" & row) = Mid(xml, first, CHUNK_SIZE)
I have tried the following code, but it doesn't work...
ws.Range(...).Value = ....

Append a ' before the = sign:
Sub Test()
'This returns 30 in cell Al
Range("A1").Value = "=SUM(10,10,10)"
'This shows formula as text in cell A2
Range("A2").Value = "'" & "=SUM(10,10,10)"
End Sub

Add an apostrophe ' to the beginning of the string (that excel thinks is a formula) and excel should interpret it as a string instead of a formula.

Related

Using VBA to insert a formula using existing cell reference

Okay, I have a basic understanding of how to record macros in VBA.
I have a bunch of cells, each referencing cells on other sheets. (Ex: =R!$A$1). These cells don't match the destination cells (for example, D3 on the primary sheet contains =R!$A$1, and the next entry is F3 referencing =R!$A$4).
Using VBA, I tried replacing the first instance with =IF(R!$A$1="", "TBA", R!$A$1). This recorded and worked for the cell in question, but when I moved to F3 and ran the macro, it replaced the cell with the exact same formula. How do I get VBA to take the existing text in the cell as replacement text, instead of blindly copying the first example?
My first thought was ActiveCell.R1C1 = "=IF( ActiveCell.R1C1 = "", "TBA", ActiveCell.R1C1) but that failed to compile.
Thanks!
Try something like this (if I understand what it is you're trying to do...)
Sub Test()
Dim frm As String
If ActiveCell.HasFormula Then 'formula in cell?
frm = ActiveCell.Formula 'read the existing formula
frm = Right(frm, Len(frm) - 1) 'remove the `=`
ActiveCell.Formula = _
"=IF(" & frm & "="""",""TBA""," & frm & ")" 'set new formula
End If
End Sub

How to add modification to all formulas from a selected range in excel

I would like to limit all of the formulas in a selected range of cells with an additional if statement.
Is it doable with the use of the VBA?
Let's say:
cell A1 contains =sum(B1;C1) but it can be any formula that a selected cell contains
after applying macro updated formula would be =if(D100>0;sum(B1;C1);0)
=if(D100>0;any formula in a cell ;0)
You can achieve this by using code like this:
Sub ModifyFormulas()
Dim cl As Range, formulas_cells As Range
Const template = "=if(D100>0,#,0)" '# will be replaced by old formula without '='
On Error Resume Next ' handle an error if there are no formulas in the range
Set formulas_cells = Range("A1:A10").SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not formulas_cells Is Nothing Then
For Each cl In formulas_cells
cl.Formula = Replace(template, "#", Mid(cl.Formula, 2))
Next
End If
End Sub
Without vba, I use the following method:
edit / replace the "=" with "xyxy" - this stops excel recalculating
then edit / replace "sum" with "if(D100>0;sum"
then edit / replace ")" with ");0)"
lastly replace "xyxy" with "="
and then it all works, done this often with my sheets when I have to change some calculations...

Converting Recorded VLookUp formula in R1C1 into VBA formula using A1 reference

I have this recorded macro that I want to convert to plain VBA macro code and copy the formula from AE2 to lastrow.
"=VLOOKUP(RC[-22],'[test.xlsx]Sheet3'!C5:C6,2,0)" ' this is from another workbook
Trying to convert to VBA.
dim wbSLW as workbook
dim wbSLWDir as String
wbSLWDir = "C\Documents\test.xlsx" 'this is not the constant directory or file name
set wbSLW = workbooks.open(wbSLWDir)
ThisWorkbook.Activate
With Thisworkbook.Sheets(1)
.Range("AE2") = "=VLOOKUP(I2," & wbSLW & "!E:F,2,0)" ' error line
end with
When I've converted it, it returns Object does not support this property or method.
Change your settings so that it does not use R1C1 format by going to (Excel 2010) Settings / Formulas and then unchecking the R1C1 reference style. Rerecord your macro and then edit it. You should not have any "RC" references, but if you do then change all references that have "RC" in them to the the same style as in "I2".
Cleaned recorded code :
With ActiveSheet.Range("AE2")
.FormulaR1C1 = "=VLOOKUP(RC[-22],Temp!C[-30]:C[-29],2,0)"
.AutoFill Destination:=Range("AE2:AE182"), Type:=xlFillDefault
End With 'ActiveSheet
RC[-22] refers to a cell that is 22 columns before the cell in which you have the formula here AE2 is the initial cell, so RC[-22]=I2
C[-30] and C[-29] refers respectively to columns that are 30 and 29 columns before the cell in which you have the formula
here AE2, so C[-30]=A and C[-29]=B
Changed formula :
Sheets(1).Range(perNum & 2).Formula = "=VLOOKUP(I2,Temp!A:B,2,0)"
or without converting the formula :
Sheets(1).Range(perNum & 2).FormulaR1C1 = "=VLOOKUP(RC[-22],Temp!C[-30]:C[-29],2,0)"
Assuming that Active Sheet name is Sheet1
ActiveCell.Value = Application.WorksheetFunction.VLookup(Sheets("Sheet1").Range("I2"), Sheets("Temp").Range("A:B"), 2, 0)
If i understood correctly, you want to replace name of the workbook in below line in vba code :
=VLOOKUP(RC[-22],'[**test.xlsx**]Sheet3'!C5:C6,2,0)
Simplest way to do that without changing any other code is :
=VLOOKUP(RC[-22],'[**" & wbSLW.Name & "**]Sheet3'!C5:C6,2,0)

#NAME? How to read a formula bar to change the formula of an error - NOT overwritting it

I've been stuck on this problem for a very long time. I've come up with ways around it but each one is bringing more problems to the table.
My macro copys and pastes info from Microsoft Word into Excel and then formats the excel sheet.
Some of the cells however show a #NAME? error. The reason for this is that the cell copies in " =------- List SC". I need to get into this cell and remove the "=------" and leave only the string List SC.
I cannot just over ride it however because each time it changes. It might be "List FL" or "List BP" or any other variation.
I cannot do an InStr or any other function because the VBA code only reads it as an error and doesn't read the formula bar. It only reads "#NAME?" or recognizes an error.
Does anyone have suggestions as to what I can do?
I have included a picture of the issue. The highlights are the error and formula bar.
Thank you!
Jonathan
My macro copys and pastes info from Microsoft Word into Excel...Some of the cells however show a #NAME? error.
You must edit the string you get from Word before you paste it into Excel. You must remove all invalid characters from the string. Then you can paste it as a formula.
If ----List is the value you want in the cell, then precede it with a single quote: '----List so it will be interpreted as text.
In VBA, use the .Formula method to access the actual formula used and do the string replace on it.
With Cells(1,3)
.Formula = Replace(.Formula, "=------ ", "")
End With
So it turns out that I can store the formula into a string and add an " ' " as the first character in the string. I can then replace the existing formula in the cell with the string (below called stfF).
This is my code.
Dim strF As String
For x = 4 To 100
strF = "'" & Cells(x, 1).Formula
Cells(x, 1) = strF
Next

VBA Excel Formula, error 1004

I'm trying to input a formula in a cell after inserting a row. And VBA does not want that and returns an error.
I first copied the formula from the excel sheet, (with ";" as delimitors), and as I have seen on other threads, I replace those with ",".
I don't understand why I get the error.
Sub Borderx()
Dim nboc As Integer
Dim ipaste As Integer
nboc = Worksheets("BDD").Range("IQ2").Value
For ipaste = 1 To nboc - 1
Worksheets("Bordereaux").Range("B14").EntireRow.Insert
Worksheets("Bordereaux").Range("T14").Formula = "=IF(AND(J14="",E14=""),SUM(F14*F14*H14)/1000,IF(O14="",SUM((H14*F14*G14)+(M14*K14*L14))/1000,SUM((H14*F14*G14)+(M14*K14*L14)+(R14*P14*Q14))/1000))"
Next ipaste
End Sub
In this case, nboc = 2, and this is supposed to insert one row.
Before I added the "if" statement, it worked fine when it was just sum()
Change it like this:
Worksheets("Bordereaux").Range("T14").Formula = " =IF(AND(J14="""",E14=""""),SUM(F14*F14*H14)/1000,IF(O14="""",SUM((H14*F14*G14)+(M14*K14*L14))/1000,SUM((H14*F14*G14)+(M14*K14*L14)+(R14*P14*Q14))/1000))"

Resources