Inserting Formula in Formula Bar - vba

Anyone knows what's the problem with my code?
Sub reFormat()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Admin")
ws.Range("C21").Formula = "=""S4&""AA5&""AA6&""AA7&""AA8&""AA9&""AA10" 'returns applica-
tion defined or object-defined error
End Sub
And I want the output of this code to be: =S4&(AA5&AA6&AA7&AA8&AA9&AA10)
Thanks for the help!

Your formula is fundamentally invalid. The string evaluates to this:
="S4&"AA5&"AA6&"AA7&"AA8&"AA9&"AA10
Which isn't a valid Excel formula.
You have too many quotes. If these are cell references and your formula intends to concatenate them, and you want your string to evaluate to this:
=S4&(AA5&AA6&AA7&AA8&AA9&AA10)
Then you could just do this:
.Formula = "=S4&(AA5&AA6&AA7&AA8&AA9&AA10)"

Related

Set range via Range object -> Range object fails?

Can someone help me debug this piece of code?
....
Dim searchRng as Range
With Sheets("Database")
Set searchRng = Range("pointer.address:.cells(pointer.End(xlDown).Row,pointer.Column).address")
End with
I keep getting error 1004: Method 'Range' of object '_Global' failed
"pointer" is a previously defined range of one cell (B6).
After various debugging attempts, the problem seems to be connected to .address... Thats as far as I've been able to trace it back.
Thanks in advance!
Leo
The issue here is that the quotation marks are for when you are passing the name of a range to the .Range() object, but you are wanting to pass it the results of calling the .address method. If you put those method calls in quotation marks VBA wont run them and will instead try to interpret what you have in them as the name of the range you are referring to. You need to construct the range name string using these methods and pass the result to the .Range() object.
There are several ways you could do this. This first one separates out the construction of the range name and assigns that to a variable which can then be passed to .Range().
Sub test()
Dim searchRng As Range
Dim CllNameA As String
Dim CllNameB As String
Dim CllRange As String
With Sheets("Database")
CllNameA = .Range("pointer").Address
CllNameB = .Range("pointer").End(xlDown).Address
CllRange = CllNameA & ":" & CllNameB
Set searchRng = .Range(CllRange)
End With
End Sub
This next subroutine condenses the same methodology into one line. It's still doing the same thing, but as its doing it all at once its slightly more difficult for a human reader to follow.
Sub test2()
Dim searchRng As Range
With Sheets("Database")
Set searchRng = .Range(.Range("pointer").Address & ":" & .Range("pointer").End(xlDown).Address)
End With
End Sub
There are other ways of achieving the same goal, but I hope this at least sets you on the right path.

Insert Array formula in Excel VBA

I recorded the array formula in order to put in VBA. Here is what I have after the recording. However, when I run the Macro, it just doesn't work.
Will it be because of the negative sign?
From Macro
Range("D3").FormulaArray = "=IFERROR(INDEX('RMS
Maint'!R1C[-2]:R3542C[-2],SMALL(IF(('RMS Maint'!R2C27:R3542C27=R1C2)*('RMS
Maint'!R2C13:R3542C13=R2C4)*('RMS Maint'!R2C21:R3542C21=""Late"")*ROW('RMS
Maint'!R2C1:R3542C1)=0,"""",('RMS Maint'!R2C27:R3542C27=R1C2)*('RMS
Maint'!R2C13:R3542C13=R2C4)*('RMS Maint'!R2C21:R3542C21=""Late"")*ROW('RMS
Maint'!R2C1:R3542C1)),ROW('RMS Maint'!R[-2]:R[3538])),1),"""")"
From Excel formula
=IFERROR(INDEX('RMS Maint'!C$1:C$3542,SMALL(IF(('RMS
Maint'!$AA$2:$AA$3542=$B$1)*('RMS Maint'!$M$2:$M$3542=$D$2)*('RMS
Maint'!$U$2:$U$3542="Late")*ROW('RMS Maint'!$A$2:$A$3542)=0,"",('RMS
Maint'!$AA$2:$AA$3542=$B$1)*('RMS Maint'!$M$2:$M$3542=$D$2)*('RMS
Maint'!$U$2:$U$3542="Late")*ROW('RMS Maint'!$A$2:$A$3542)),ROW('RMS
Maint'!1:3541)),1),"")
The error is 1004 - Unable to set the FormulaArray property of the Range class
I'm sorry for the code format. It looked terrible.
Or you may break the long formula into few parts and replace it in the end with the actual formula like below...
Dim LogicalTest As String, FalseValue As String
LogicalTest = "('RMS Maint'!$AA$2:$AA$3542=$B$1)*('RMS Maint'!$M$2:$M$3542=$D$2)*('RMS Maint'!$U$2:$U$3542=""Late"")*ROW('RMS Maint'!$A$2:$A$3542)=0"
FalseValue = "('RMS Maint'!$AA$2:$AA$3542=$B$1)*('RMS Maint'!$M$2:$M$3542=$D$2)*('RMS Maint'!$U$2:$U$3542=""Late"")*ROW('RMS Maint'!$A$2:$A$3542)"
Range("D3").FormulaArray = "=IFERROR(INDEX('RMS Maint'!C$1:C$3542,SMALL(IF(""LogicalTest"","""",""FalseValue""),ROW('RMS Maint'!1:3541)),1),"""")"
Range("D3").Replace """LogicalTest""", LogicalTest, LookAt:=xlPart
Range("D3").Replace """FalseValue""", FalseValue, LookAt:=xlPart
There is an option of last resort: Use a human.
Have the macro place the formula into the cell as a String and have the user complete the process:
Sub pinocchio()
Range("D3") = "'=1+2"
MsgBox "User: Make this into a real array formula"
End Sub

Implement Vlookup formula on VBA, and handle error 1004

I start my adventure with VBA. I would like to create formula on VBA, use vlookup but something is going wrong with this.
Also I would like to implement vlookup for cells, when
cells from deferent column will be filled
( for example if WB_WS_Pricing.Range("A4")<>0 then
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"")"
Sub formula()
Set WB_CMSO_MASS_IBERIA = ThisWorkbook
Set WB = ThisWorkbook
Set WB_WS_PRICING = WB.Sheets("Pricing")
Set WB_WS_HEADER = WB.Sheets("Header")
Set WB_WS_DATA = WB.Sheets("DATA")
Set WB_WS_Extension = WB.Sheets("Extension")
Set WB_WS_DELIVERING = WB.Sheets("Delivering")
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"")"
End Sub
Enyone has idea what is wrong?? For me the formula seems be fine...
You need to escape the double quotes in your formula with an extra quote in front of each (ie """" not "")
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"""")"

Value in cell as variable in select formula

I have programmed the following VBA code:
Sub test()
Dim Variable As Variant
Variable = "R2:R11"
Sheet1.Select
Range(Variable).Select
End Sub
This formula works perfectly. Now I want to replace the "R2:R11" part of the variable and instead referring to a cell (W12) in the spreadsheet. In this cell I have written "R2:R11" and I changed the formula to the following:
Variable = Sheet1.Range("W12")
which leads to the following code:
Sub test()
Dim Variable As Variant
Variable = Sheet1.Range("W12")
Sheet1.Select
Range(Variable).Select
End Sub
However, with this formula I now get the ERROR 1004.
Do you guys have any solution how I can make the variable referring to the cell in the spreadsheet and then use it in the select formula?
Thanks for any help :-)
You need to declare your variables properly and access the actual .Value property of the Range object.
Sub test()
Dim Variable As String
Variable = Sheet1.Range("W12").Value
Sheet1.Select
Range(Variable).Select
End Sub
Which can also be written as:
Sub test()
Sheet1.Activate
Range([W12]).Activate
End Sub
A note on the .Value property - if you omit this, the value will usually be assigned anyway because it's the default property of the range object. That being said there are scenarios where this won't be the case and therefore it's always best practice to explicitly state that you want the value.
you most probably typed "R2:R11" in W12 cell, i.e with double quotes too
in that cell you only have to type R1:R12
moreover you can simply code
Sub test()
Sheet1.Range(Sheet1.Range("W12")).Select
End Sub

Runtime error 424 Compile error, Object Required - VBA Excel

I need a simple bit of VBA code to work, however I keep getting runtime error 424.
I have looked over many other posts but found nothing that could help me
All I want to do is Vlookup with the id "individual" and find it in the ApplySublimits Worksheet.
Sub CommandButton1_Click()
Dim individual As String
Dim individualCap As Single
Dim subRange As Range
Set subRange = ApplySublimits.Range("B:D")
individual = "D02065"
Range("C10").Value = individual
individualCap = Application.WorksheetFunction.VLookup(individual, subRange, 2, False)
End Sub
I keep getting this error but i dont understand why. Im very new to excel and would appreciate some help or guidance.
How can a single (a floating point number) hold something starting with D. It's not 0-9. If it's hex the &hD02065 is the way to do it. Plus numbers aren't enclosed in quotes.
Declare and set applysublimits as the worksheet
Change individualCap to String
e.g.
Sub CommandButton1_Click()
Dim individual As String
Dim individualCap As String
Dim subRange As Range
Dim applysublimits As Worksheet
Set applysublimits = Sheets("Sheet1")
Set subRange = applysublimits.Range("B:D")
individual = "D02065"
Range("C10").Value = individual
individualCap = Application.WorksheetFunction.VLookup(individual, subRange, 2, False)
End Sub