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))"
Related
I am trying to get the sum of column H from each individual sheet. The name of the sheet is given in column A
The way I have been doing it is using =SUM('BSF10003'!H:H), dragging this formula down and changing the last couple digits manually but this is a long process if there are 100+ columns.
I am new to VBA and am hoping there is a way to do this task quicker
Thanks again!
The INDIRECT function is another way. Example below,
=SUM(INDIRECT("'"& A2 &"'!H:H"))
A2 = The sheet name
INDIRECT lets you make references dynamically.
One way could be a UDF
Paste this in a module
Function SumSheets(ByVal SheetName As String) as Double
SumSheets=Application.WorksheetFunction.Sum(Worksheets(SheetName).Range.Columns(8))
End Function
And use it like a standard formula...
=SumSheets(A1)
Will return the sum of column H in the worksheet named in A1
Or as a variation, you could use this along with the UDF. You'd paste it in the same module like the UDF but you don't need to type the formula. Instead, you select a cell in a column and run this macro.
The formulas are added automatically.
Sub FillRangeWithSumSheets()
Dim n As Name: Dim s As String: s = "SumSheetsRange"
With ActiveWorkbook
For Each n In .Names
If n.Name = s Then
n.Delete
Exit For
End If`
Next
Set n = .Names.Add(s, "=OFFSET($A1,0," & ActiveCell.Column - 1 & ",COUNTA(Sheet1!$A:$A),1)")
n.Visible = 1
Range(s).Formula = "=SumSheets(A1)"
End With
End Sub
I have a vba formula that copies a value (which consists in a number) of a cell to another cell. When the number is with decimals (ex: 25,50), the result is ok, but when the number is without decimals (ex: 50), I get an error checking flag suggesting me to convert that cell to number.
I wouldn't have a problem with this error flagging, but I cannot use the result in a formula. For example, if I use SUM(A:A), the formula adds all the numbers, except the flagged ones.
So far, I have tried pasting using the .xlPasteValuesAndNumberFormats property, without any success.
Sub Adauga()
Dim i As Range
Dim cellTaxa As Range
Set cellTaxa = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(1, -2) 'Here I set the range to the value of a cell that is related to the button I click.
n = 2
Set i = Sheets("TJT DETERMINABILA").Cells(n, 10) 'Here I insert the first result
Do While i <> "" 'Here I find the next empty cell in the same column to insert the value.
n = n + 1
Set i = Sheets("TJT DETERMINABILA").Cells(n, 10)
Loop
cellTaxa.Copy 'Here I copy the cell
i.PasteSpecial xlPasteValues 'Here I paste the cell.
End Sub
I would like to be able to use the values that are pasted in the column I mentioned in the code with the SUM formula.
Thank you!
As #urdearboy has stated, an easy trick to convert text to numbers is to add +0.
In my case, I have added cellTaxa = CellTaxa + 0 before the line cellTaxa.Copy and it has solved my issue.
VBA has been giving me an error 1004 for this code:
Sub UpdateCellsFormula()
Dim FormulaRange As Range
Dim i As Integer
Set FormulaCellsRange = Range("J17", "J95")
i = 17
For Each r In FormulaCellsRange.Cells
r.Formula = Replace("=D17*L21+E17*M21+F17*O21+G17*N21+H17*P21+I17*Q21)/(1+0,85+0,6+0,4+0,37)", "17", i)
i = i + 1
Next
End Sub
Can anyone have a look at it?
Formulas assigned with .Formula must be in En-US locale. The decimal dot in numbers must therefore be ., not , (1+0.85+0.6+0.4+0.37).
Also you have a missing opening parenthesis in the beginning, right after =.
You also might want to learn about absolute references in Excel. That way you can copy the same formula into all cells in FormulaCellsRange without replacing anything:
Sub UpdateCellsFormula()
ActiveSheet.Range("J17", "J95").Formula = "=(D17*$L$21+E17*$M$21+F17*$O$21+G17*$N$21+H17*$P$21+I17*$Q$21)/(1+0.85+0.6+0.4+0.37)"
End Sub
Dim test As String
test = CStr(ActiveWorkSheet.VLookup("jpeg_lrg", B, 3))
stupid 424 error help!
for my macro in excel vba
Dim test As String
test = CStr(ActiveWorkSheet.VLookup("jpeg_lrg", B44, 3))
(didnt work as well)
also tried without the CSTR
You can't just pass you ranges as variable names.
Dim test As String
test = Application.WorksheetFunction.VLookup("jpeg_lrg", ActiveSheet.Range("B:B"), 3)
That won't make any sense though, because "B:B" only contains one column, and you ask it for column three. With VLookup you want at least two columns in the range.
I think you are trying to return the value of the cell of E44?
If that is the case, then this should work for you - replace MsgBox with the code you require.
For Each i In Range("B44")
If i = "jpeg_lrg" Then
MsgBox i.Offset(0, 2)
Else
End If
Next i
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.