Error while selecting a range of cells - vba

I want to select the range described below to format it. However, it says that there is an Wrong number of arguments or invalid property assignment.
With ws3.Range("C8", "C12", "L14:P16", "L20:P20", "L22:Q23", "L" & lastrow5 & ":" & "Q" & lastrow5)

Try it like this...
With ws3.Range("C8, C12, L14:P16, L20:P20, L22:Q23, L" & lastrow5 & ":Q" & lastrow5)

As IntelliSense is telling you (assuming ws3 is declared As Worksheet), Worksheet.Range takes up to 2 arguments: [Cell1] and [Cell2].
In VBA you separate arguments using a comma. So this:
ws3.Range("C8", "C12", "L14:P16", "L20:P20", "L22:Q23", "L" & lastrow5 & ":" & "Q" & lastrow5)
Is attempting to invoke Worksheet.Range with 6 arguments, and VBA doesn't know what to do with it, hence "wrong number of arguments".
If you mean to give it a union'd range string, then give it a single string argument.

Related

Writing formula in VBA results in Compile Error - Double Quotes

I'm working on an Excel VBA Macro.
I must write a formula inside a cell.
The formula I'm referring to is CONTA.SE(). It behaves in the same way as COUNTIF() does. I guess that it is the COUNTIF() version for Excel in italian.
To be more precise the formula I want to implement is:
=CONTA.SE(Report!E:E;"<20")
so that it works on cells values from E2 to the last non-empty cell.
I guess I'm having trouble because of the double quotes.
I've tried many solutions as, for example:
Cells(5, 2).Formula = "=CONTA.SE(Report!E2:E" & rowCount & ";" & Chr(34) & "<20" & Chr(34) & ")"
or:
Cells(5, 2).Formula = "=CONTA.SE(Report!E2:E" & rowCount & ";" & """<20""" & ")"
The error message I got is:
run-time Error 1004:
Error defined by the Object or the Application.
Can anyone, please, tell my how write it correctly?
.Formula uses the english version of the formulas. If you want to use the localized formula (eg italian) you must use .FormulaLocal.
So either localized:
Cells(5, 2).FormulaLocal = "=CONTA.SE(Report!E2:E" & rowCount & ";" & Chr(34) & "<20" & Chr(34) & ")"
or english
Cells(5, 2).Formula = "=COUNTIF(Report!E2:E" & rowCount & "," & Chr(34) & "<20" & Chr(34) & ")"
Note that the localized (italian) form only works when used in an italian Excel, but the english version would work on any Excel. So using the english version in VBA is recommended. Using the english formula in VBA will automatically convert it into the correct localization form in the cell (the user will not notice any difference).
Also note that the english separator is , while the italian (and most european) separator is ;.

Linking cells to different worksheet by using vba to insert dynamic formulas

I am creating a new worksheet called varRef and am trying to link cells of an existing worksheet to this new worksheet by using VBA. I have been trying to solve it for a couple of hours but cannot get it right and cannot find a similar case on the web either.
The code is:
Dim varRef as Variant 'name of the new sheet
varRef = Inputbox("xyz") 'this is how I define the name
Dim intRow As Integer 'row that corresponds to the appropriate postion in the existing sheet
intRow = ActiveCell.Row 'see above
Cells(intRow, 22).Formula = "=IF(varRef & ""!I148""="""","""",varRef & _
""!I148"")" 'trying to link the contents within the same workbook
The result in the cell I get is
=IF(varRef & "!I148"="","",varRef & "!I148"
which is obviously not working.
Issue 1) is that VBA does not recognize my variable in the formula. It is working for naming the sheet however.
Issue 2) is the quotation marks, that are not working as intended. One is supposed to use double quotation marks to not end the string. However the second marks will not disappear in the final code of the cell.
Any help is very much appreciated and hopefully valuable for other users as well!
You had some count issues with hoe many " you have before and after the varRef variable.
Also, I prefer to use Chr(34) to have " inside the Formula string, this way I don't get confused with how many " I need to use.
Try the code below:
Cells(intRow, 22).Formula = "=IF(" & varRef & "!I148=" & Chr(34) & Chr(34) & "," _
& Chr(34) & Chr(34) & "," & varRef & "!I148)"

Excel custom hyperlink for each cell in range using VBA

I have a module that removes all formulas from an entire sheet and then, it should, create a hyperlink formula on each cell using the cell value.
Sub Test()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Test")
ws1.Range("B33:F533").Value = ws1.Range("B33:F533").Value
For Each i In ws1.Range("B33:B533")
i.Formula = "=HYPERLINK(""https://somelocation/" & i.Value & """,""" & i.Value & """)"
Next
End Sub
EDIT: It's now working perfectly. Thanks For all the help!
i.Formula = "=HYPERLINK('https://somelocation/"&i.Value&","& i.Value"')"
Has an error in the formula. At the i.Value"') you are missing &, i.Value & "'. The correct one, that 'compiles', is added below:
i.Formula = "=HYPERLINK('https://somelocation/" & i.Value & "," & i.Value & "')"
Also it is worth noting that instead of writing "&i.Value&" and let the VBA IDE add the spaces, it is better to do it yourself, eg: " & i.Value & ".
You're code has 2 flaws, you should add the last & to get a syntax correct formula, and add spaces between the " and &, or else VBA won't see it is right.
Edit; a third flaw:
The line to create the formula is wrong too. Let's break it down:
"=HYPERLINK('https://somelocation/" & i.Value & "," & i.Value & "')"
The parameter in the formula would be:
'https://somelocation/" & i.Value & "," & i.Value & "' so an output example of this line could be 'https://somelocation/1,1'. At first, I thought you are writing an URL with a comma, alright..? But then I looked at the HYPERLINK function in Excel and it requires two parameters. You are only giving one parameter. Excel formulas also expect a double quote instead of a single quote. You can escape a double quote in VBA like this "".
The correct line should be:
i.Cells(1, 1).Formula = "=HYPERLINK(""https://somelocation/" & i.Value & """,""" & i.Value & """)"

VBA Set Cell value to text containing ' not working

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.

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 –