Excel VBA hlookup function - vba

I am trying to use the excel VBA HLookup function. I have tried it two ways and both produce error. Could anyone explain me what is that I am doing wrong?
First try:
lookupValue = Worksheets(1).Name & "!A1"
tableArray = Worksheets(3).Name & "!$A$1:$" & Col_letter & "$1"
Worksheets("Comparison").Cells(1, 2).Value = "=HLookup(" & lookupValue _
& ";" & tableArray & ";1;FALSE)"
Second try:
tda = Worksheets(1).Cells(1, 1).Value ' I also tried using tda without .Value
Table = Worksheets(3).Range(Cells(1, 1))
Worksheets("Comparison").Cells(1, 2).Value = WorksheetFunction. _
HLookup(tda, Table, 1, False)

For your first one, you need to use US regional settings, so a comma separator, and you should really enclose the sheet names in single quotes in case they contain spaces or look like special names (e.g. dates):
lookupValue = "'" & Worksheets(1).Name & "'!A1"
tableArray = "'" & Worksheets(3).Name & "'!$A$1:$" & Col_letter & "$1"
Worksheets("Comparison").Cells(1, 2).Formula = "=HLookup(" & lookupValue _
& "," & tableArray & ",1,FALSE)"
and for the second you have to use a range object for the table argument:
tda = Worksheets(1).Cells(1, 1).Value ' I also tried using tda without .Value
Set Table = Worksheets(3).Range(Worksheets(3).Cells(1, 1), Worksheets(3).Cells(1, Col_letter))
Worksheets("Comparison").Cells(1, 2).Value = WorksheetFunction. _
HLookup(tda, Table, 1, False)
I have assumed Table is declared as Variant, Object or Range.
Note you will still get a run-time error if there is no match for your lookup value.

what you're doing there usually works, which is creating a string with the function you want to call and inputting it.
I don't think that's the safest way, since it would not work if you run that macro from an Excel with a different language.
the proper way to call an Excel function from VBA call is for example:
cells(1,2) = Application.WorksheetFunction.VLookup(123,Range("A1:C100"),3,FALSE)
anyway if you'd rather use this string approach the problem in the first try is that
"=HLookup(" & lookupValue _
& ";" & tableArray & ";1;FALSE)"
results in the string:
=HLookup(Sheet1!A1;Sheet3!$A$1:$B$1;1;FALSE)
note that you're using semicolons where you're supposed to use commmas.
the problem in the second try is that the .Range property takes a string as input, so you cant Range(Cells(1,1)) you should do something like .Range("A1:A3")
hope it helps !

Related

Is there a Cells().Formula function?

my script compiled fine so I assumed it was ok but couldn't find it and of course it error's out on this line, this is encapsolated in a while loop, I'm just trying to have a simple way to add a formula to a row of data, then do something similar to add it to a row of data. Is there no .Formula or do I have another error?
Cells(arow, acol).Formula = "=COUNTIF(" & wsData.Name & "!" & Cells(fdRow, acol).Address & ":" & Cells(ldRow, acol) & ")"
acol = acol + 1
Just a few little changes needed:
You're missing a .Address on Cells(ldRow, acol)
I also included apostrophes ' around the worksheet name (only necessary if the name has spaces in it).
As #Wayne G. Dunn also pointed out you need the second parameter of the COUNTIF function, which I have put at the end as 1. You'll need to update that.
Here is the updated code:
Cells(arow, acol).Formula = "=COUNTIF('" & wsData.Name & "'!" & Cells(fdRow, acol).Address & ":" & Cells(ldRow, acol).Address & ",1)"

dynamic reference in VBA Index function

I would like to dynamically apply below INDEX function for two ranges where one range should be dependent on an iterator i.
Could anyone help how to write that down instead of my code example (only the Index part with the reference, please)?
With ActiveSheet
For i = 1 to 5
.Range("c" & i & ":I" & i & "") = [INDEX(EURbased!C5:I5 * EURbased!C4:I4,0)]
Next
End With
I would like to use iterator i instead of the "5" in EURbased!C5:I5
Try it like this:
With ActiveSheet
For i = 1 to 5
.Range("c" & i & ":I" & i & "").formula = "=INDEX(EURbased!C" & i & ":I" & i & " * EURbased!C4:I4,0)"
Next
End With
I have not tried it myself, but as far as you mentioned I would like to use iterator i instead of the "5" in EURbased!C5:I5 I think it should work.

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 & """)"

SUMIFS function from other workbook - Error is Type Mismatch

I would like to do SUMIFS function in a range of active sheet from other workbook. Here is where I get my error of Macro:
ActiveSheet.Range(ActiveSheet.Cells(6, lastCol + 1),
ActiveSheet.Cells(lastRow, lastCol + 1)).Formula = Application.WorksheetFunction.SumIfs(" ' " & wbGSD.Name & "'!$F$10:$F$" & wbGSDlastRow & ",'" & wbGSD.Name & "'!$B$10:$B$" & wbGSDlastRow & ",B6,'" & wbGSD.Name & "'!$C$10:$C$" & wbGSDlastRow & ", Total")
The error I got is:
Type Mismatch
I tried Sumif function. It didn't work either.
You're passing SumIfs() one long string. It doesn't take one long string. It takes a number of parameters, the first two of which are Range objects, not strings. If you want to write your formula as one long string, then there's no need for WorksheetFunction.SumIfs(). Just assign the string formula to the range's Formula property:
Range("A1").Formula = "=SUMIFS('" & wbGSD.Name & "'!$F$10:$F$" & ...
If you really do want to use WorksheetFunction.SumIfs(), you'll need to use it properly by passing individual parameters of the right type:
Range("A1") = WorksheetFunction.SumIfs( _
Sheets(wbGSD.Name).Range("$F$10:$F$" & wbGSDlastRow), _
Sheets(wbGSD.Name).Range("$B$10:$B$" & wbGSDlastRow), _
....

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)