VBA Set Cell value to text containing ' not working - vba

I'm trying to get a macro to type out a heap of formulae for me. The formula goes something like this:
=COUNTIF('other_sheet'!A:A,"hello*")
The user can specify A and other_sheet, so I select the cell and then go
ActiveCell.Value = "=COUNTIF('" & othercell & "'!" & column & ":" & _
column & """,hello*"")"
But it keeps giving me errors like:
1004: Object Defined Error
I have tried using ActiveCell.Text, ActiveCell.Formula etc. and they all don't work.

It needs to be ActiveCell.Formula and ", is the wrong way around next to Hello. It should be:
ActiveCell.Formula= "=COUNTIF('" & othercell & "'!" & column _
& ":" & column & ",""hello*"")"

Apparently you can only put proper, completed answers into a cell.
i.e. if the output turns out to be "=sum(A:A" and a bracket isn't closed, then the application throws an error just as if you typed it in manually.

Related

Create visible formula through VBA

My challenge is to code a visible formula into cells that end-users can read the reference. Highlighted yellow line of code causes error "Application Defined or Order defined error". The requirement is that in cells are simple formula like below, which takes first number from workbook where actual result will be and other one comes from different workbook. Actual code locates in a third excel.
=5/78
Variables
Private AR As New Dimension
Private UR As New Dimension
UR.KeySheet --> Sheet 1
AR.KeySheet --> DivederNumbers
UR.Wb --> myWorkbook.xlxs
AR.Wb --> myOtherWorkbook.xlxs
.
Dim test As String
If AR.Wb.Sheets(AR.KeySheet).Cells(Cell3.Row, Cell2.Column) > 0 Then
test = AR.Wb.Path & "\" & AR.Wb.Name
'**THIS LINE CAUSES ERROR:
UR.Wb.Sheets("RESULT").Cells(Cell1.Row, Cell1.Column).Formula = _
"='" & UR.KeySheet & "'!" & Cells(Cell1.Row, Cell1.Column).Address & "/" _
& "'" & [test] & AR.KeySheet & "'!" & Cells(Cell1.Row, _
Cell1.Column).Address(External:=True)
Exit For
End If
You can't reference a sheet like that. AR.KeySheet is only valid as part of the Sheets() collection.
Here is a solution for referencing the sheet name: Get a worksheet name using Excel VBA
You would need to use:
"='" & Application.Caller.Worksheet.Name & "'!" & Cells(Cell1.row, Cell1.column).Address & "/" _

VBA: Cell designations not being evaluated

I have the following code in VB:
ActiveCell.Formula = " = COMPANYNAME " & " & " & "R[-12]C[-3]" & " & " & "VLOOKUP(RC[-6],R3C7:R22C18,9)"
I want to get a cell that has in it: = COMPANYNAME & D25 & VLOOKUP(A26,$G$3:$R$22,9)
Instead, I get a cell with = COMPANYNAME & R[-12]C[-3] & VLOOKUP(RC[-6],R3C7:R22C18,9)"
Basically, the cell designations are not being evaluated.
What am I doing wrong?
Change ActiveCell.Formula to ActiveCell.FormulaR1C1
By using ".Formula", it expects cells to be referenced in "A1" fashion, and therefore doesn't know how to compute the R/C references, and appears to see the whole thing as just a string instead of a formula (also you may need to remove the spaces from between the &'s).

VBA Offset within Vlookup?

I'm trying to get a Vlookup for a row which is just left of the Lookup_value. I can't do a Table_array of "-1" (or -2) so I'm wondering if I can do an Offset(0, -1) within that line of code.
The line in question:
wCell.FormulaR1C1 = "=VLOOKUP(RC[1],'[" & filename & "]" & ws.Name & "'!R8C4:R" & lastrow & "C5,-1,FALSE)"
The entire code block:
Range("$C$8:$C$" & lastrow).Select
For Each wCell In Range("$C$8:$C$" & lastrow)
wCell.Select
If wCell.FormulaR1C1 = "" Then
wCell.FormulaR1C1 = "=VLOOKUP(RC[1],'[" & filename & "]" & ws.Name & "'!R8C4:R" & lastrow & "C5,-1,FALSE)"
End If
Next
If you were looking to find information to the left of the reference point in excel, using a Match-Index lookup would be they way for you to go. Not only is this method able to look to the left or right of your reference, but it is also a faster process.
VLookup looks like this:
=VLookup([Value to find],[Where to find the value],[Column to return],[range lookup])
Where as using Match-Index looks like this:
=Index([Range to look in for return],Match([Value to find],[Range to look in for value],[Exact match or partial]))
So, while a bit more complicated to write, using the second method really increases the flexibility of what you can look up and where that information is.
Now, if you were to apply this method to your code, it should look something like this:
wCell.FormulaR1C1 = "=Index([Range of Value to Find],Match(RC[1],'[" & filename & "]" & ws.Name & "'!R8C4:R" & lastrow & "C5, [0 for exact match]))"
(Just change out the bits I added in brackets for the information that they need, and that should address that problem your are running into)

Run-time error '1004' when applying formula to Range.FormulaR1C1 in VBA

I'm trying to apply a big nested formula to a range using the code below. Basically, if the value in cell A of the active row exists in the column A of another workbook and if the cell in column E of the active row is not empty, I want the active cell to display the cells to display the value of the equivalent cell in a separate workbook.
This needs to be applied to several worksheets so I'm using the variables lrow (which is an int with the last row of the active worksheet in workbook#1) and tlrow (which is an int equal to the last row of the active worksheet in workbook#2). When I step through the sub, these variables both return the numbers I would expect them to.
Likewise, this is inside of a for loop so I also use Worksheets(i).Name where I is an int.
When I run the code, I get the run-time error "'1004': Application-defined or object-defined error".
I'm assuming it's a syntax issue.
Code:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & _
"'!A15:D" & tlrow & ",3,FALSE)),""0"",VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & "'!A15:D" & tlrow & ",3,FALSE))))"
Try using this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1," & _
Worksheets(i).Range("A1:D" & lrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)),""0"",VLOOKUP(RC1," & _
Worksheets(i)..Range("A1:D" & tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)))"
What version of Excel are you running? In more recent versions you can use the Iferror function in this formula to really chop down the size.
It would be something like this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IFERROR(VLOOKUP(RC1," & " & Worksheets(i).Range("A1:D" & _
tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & ",3,0),""0"")"
Thanks for your help. I was able to resolve the problem by defining my vlookup range in a Range variable and then inputting the variable name in L42's equation in place of
worksheets(i).Range("A1:D" & lrow)
Really apprecaite the responses! Thanks again.

application.worksheetfunction.sum not evaluating string

I have the following code in my procedure built up to be evaluated within a much larger loop
CalculationHoldArray(Loopcount) = "'[" & Usefile1.Name & "]" & _
Worksheet1 & "'!" & Cells1 & ",'[" & Usefile2.Name & "]" & _
Worksheet2 & "'!" & Cells2
Sheets("ECAP PARAMETER INPUTS").Cells(31, "F").Value =
Application.WorksheetFunction.Sum(CalculationHoldArray(Loopcount))
The string returned for the array is:
'[Control Model v1.35- nonfunctional.xlsm]Input'!E2, _
'[Control Model v1.35- nonfunctional.xlsm]Input'!E3
But the application.worksheetfunction.sum is returning an error 1004. "Unable to get the sum property of the worksheet class". Anyone know what the issue is that it won't let me use the sum property here?
Instead of using the Application.Worksheet.Function command, simply use the Evaluate command, which requires adding in the "sum" and brackets to make it a valid formulae.
But I have found a solution- instead of using the application.worksheet.function command, I can simply use the "EVALUATE" command, just required adding in the "sum" and brackets to make it a full formulae –