Vlookup with Integer Variables - vba

I am trying to do a Vlookup in my code and I have few integer variables. It gives me the error:
Run-time Error 1004
The code stops in the line of vlookup. All the variables have values. If someone can see where the problem, please let me know.
'Employees
empWS.Range("D1:G" & empLR).Copy
With tmpWB.Worksheets(2)
.Cells(1, 1).PasteSpecial xlPasteValues
.Range("$A$1:$D$" & empLR).AutoFilter Field:=1, Criteria1:=1
.Range("A2:D" & empLR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.ShowAllData
empLR = .Cells(Rows.Count, "A").End(xlUp).Row
.Columns(1).Delete
.Range("D1").Value = "start date"
.Range("D2:D" & empLR).FormulaR1C1 = "=VLOOKUP(RC[-3],SAP!R1C1:R " & SLRow & " C70, StartColumn,0)" 'it stops here
.Columns("A:D").AutoFit
End With

"=VLOOKUP(RC[-3],SAP!R1C1:R " & SLRow & " C70," & StartColumn & ",0)"
You need to put startcolumn outside the speech marks
EDIT
"=VLOOKUP(RC[-3],SAP!R1C1:R" & SLRow & " C70," & StartColumn & ",0)"
(Patrick Honorez spotted that there was an extra space after R in the first bit)

Try removing extra spaces:
.Range("D2:D" & empLR).FormulaR1C1 = "=VLOOKUP(RC[-3],SAP!R1C1:R" & SLRow & "C70, StartColumn,0)"
Space in a range address is an intersection operator.

Related

Concatenate two ranges without looping

I want to concatenate two ranges into one WIHTOUT using a loop.
Below is the code. The lines with comment's is basically the solution I want to avoid.
With ws_AUoM
lCountEntriesInAUoMFile = .Cells(Rows.Count, "B").End(xlUp).Row
.Range("O2:O" & lCountEntriesInAUoMFile).Value = .Range("B2:B" & lCountEntriesInAUoMFile).Value & .Range("F2:F" & lCountEntriesInAUoMFile).Value
' For lLoopCounterAUoM = 2 To lCountEntriesInAUoMFile
'
' .Cells(lLoopCounterAUoM, "O").Value = .Cells(lLoopCounterAUoM, "B").Value & .Cells(lLoopCounterAUoM, "F").Value
'
' Next lLoopCounterAUoM
End With
This line:
.Range("O2:O" & lCountEntriesInAUoMFile).Value = .Range("B2:B" & lCountEntriesInAUoMFile).Value & .Range("F2:F" & lCountEntriesInAUoMFile).Value
returns the error "Type Mismatch". I have double checked the sizes and location of each range. Yet it does not work. What am I missing here?
You can do this:
Dim r As Long
With ws_AUoM
r = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("O2:O" & r).Value = .Evaluate("B2:B" & r & " & F2:F" & r)
End With
Evaluate knows you're giving it an array formula, and will return the resulting array, which you can assign directly to the sheet.

VBA Run-time Error 1004 for Special Characters in Soft-coded Vlookup Function

When I was trying to throw a vertical bar "|" into my vlookup function, I got a 1004 error. I am not sure where it came from.
Initially the data table looks like this:
And my code was like this:
Sheets("Sheet 1").Cells(i, 2).Formula = "=VLOOKUP($A" & i & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Everything worked fine till the format of the data table got changed to:
So I had to throw the vertical bar "|" into my code. My new code was like this:
NewKey = Sheets("Sheet 1").Cells(i, 1).Value & "|" & Sheets("Data").Cells(i, 2).Value
Sheets("Sheet 1").Cells(i, 3).Formula = "=VLOOKUP(" & NewKey & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Apparently Excel 2010 did not like my codes and threw me a 1004 error. I am wondering if anyone knows a solution to this. Thank you in advance for your help.
NewKey needs to be in quotes when the formula is placed on the sheet, so you need to include the quotes in the string:
NewKey = Sheets("Sheet1").Cells(i, 1).Value & "|" & Sheets("Sheet1").Cells(i, 2).Value
Sheets("Sheet1").Cells(i, 3).Formula = "=VLOOKUP(""" & NewKey & """,Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"

Run-time Error Using Formula From Workbook Variable

I am using Vlookup formula from another workbook in my code. The other workbook named as a variable TifuliWB Workbook but I keep getting an error run time error 1004. I am sure that it's such a small mistake of mine that stops the sub but I can't know what.
With MainWB.Worksheets(2)
LR = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("J2:J" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-8]," '"[" & TifuliWB.Worksheets(1) & "]"'"!C1:C71,65,FALSE)"
.Range("J2:J" & LR).NumberFormat = "m/d/yyyy"
.Cells.Copy
End With
Try referencing the columns' full external address instead of concatenating in the workbook and worksheet name.
.Range("J2:J" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-8]," & TifuliWB.Worksheets(1).range("A:BS").address(1, 1, external:=true, referencestyle:=xlr1c1) & ",65,FALSE)"
'alternately in xlA1 style
.Range("J2:J" & LR).Formula = _
"=VLOOKUP(J2," & TifuliWB.Worksheets(1).range("A:BS").address(1, 1, external:=true) & ",65,FALSE)"
Your original should have used the .Name or .FullName property and there were some string concatenation issues.
.Range("J2:J" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-8], '[" & TifuliWB.fullname & "]" & TifuliWB.Worksheets(1).name & "'!C1:C71,65,FALSE)"

Adding a range to an existing PrintArea with VBA fails with error 400

I'm trying to extend the current print area of my Excel sheet but currently, with the following code. It breaks at the third .PageSetup.PrintArea and I don't know why.
The code:
With ActiveSheet
.PageSetup.PrintArea = "$A$1:$A$5"
MsgBox (.PageSetup.PrintArea & "," & .Cells(8, 1).Address & ":" & .Cells(10, 1).Address)
.PageSetup.PrintArea = .PageSetup.PrintArea & "," & .Cells(8, 1).Address & ":" & .Cells(10, 1).Address
MsgBox (.PageSetup.PrintArea & "," & .Cells(15, 1).Address & ":" & .Cells(20, 1).Address)
.PageSetup.PrintArea = .PageSetup.PrintArea & "," & .Cells(15, 1).Address & ":" & .Cells(20, 1).Address
End With
On the last line it puts me an "Error 400" and I don't know why, it's exactly the same line as above. I also tried with ; instead of , but it don't figure it out. If you have any ideas, please tell me, I'll be glad to ear it.
Thanks, Clément
Use .Union (Application.Union Method):
.PageSetup.PrintArea = Union(.Range("$A$1:$A$5"), .Range(.Cells(8, 1), .Cells(10, 1)), .Range(.Cells(15, 1), .Cells(20, 1))).Address
to join all 3 ranges.
Or use
.PageSetup.PrintArea = Union(.Range("Print_Area"), .Range(.Cells(8, 1), .Cells(10, 1))).Address
to add a range to an existing print area.
Here we use the fact that the existing print area is saved as a named range Print_Area which we can access by .Range("Print_Area").
In case anoyne is suspect of how the English name Print_Area would behave in localized Office versions, we won't run into any issues because Print_Area always works even if the name manager shows the localized name of Print_Area.
I expect your Windows is set to use a list separator which is NOT the comma. In my case, the printarea property returns this after the second line of code (note the semi-colon):
$A$1:$A$5;$A$8:$A$10
But in order to set this printarea, the ; must be replaced with a comma:
ActiveSheet.PageSetup.PrintArea = "$A$1:$A$5,$A$8:$A$10"
So -on my system- for your code to work, I needed to use:
.PageSetup.PrintArea = Replace(.PageSetup.PrintArea, ";", ",") & "," & .Cells(15, 1).Address & ":" & .Cells(20, 1).Address

Deploy VBA Cell Reference in Array Formula

So I'm trying to do a couple things with this subroutine but can't get VBA to execute the .FormulaArray function.
Create a named range using offset & lastrow function
Use cell references to insert into the array formula
--
Sub namedrange()
Dim firstrow As Long
Dim LastRow As Long
Dim ColToLetter, absolute, Title, mc, mc1
ActiveCell.Offset(0, -1).Select
absolute = ActiveCell.Address
LastRow = ActiveSheet.Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
firstrow = ActiveCell.Row
ColLetter = Mid(ActiveCell.Address, 2, 1)
ActiveSheet.Range(ColLetter & firstrow & ":" & ColLetter & LastRow).Name = Range(ColLetter & "1").Value
Title = Range(ColLetter & "1").Value
ActiveCell.Offset(0, 1).Select
mc = ActiveCell.Offset(-1, 0).Address
mc = Mid(mc, 2, 3)
mc1 = Replace(mc, "$", "")
ActiveCell.FormulaArray= "=IF(ROWS(mc & "":"" & mc1)>SUM(IF(FREQUENCY(IF(Title<>"""",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),1)),"""",INDEX(Title,SMALL(IF(FREQUENCY(IF(Title<>"""",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),ROW(Title)-ROW(absolute)+1),ROWS(mc & "":"" & mc1))))"
End Sub
The formula bar shows what the vba function is outputting, which is not what I want. I don't know why it won't output the references I've created like mc should be "$A$2" not "mc".
Also when I try to execute the FormulaArray code I get a runtime error 1004 "Unable to set the FormulaArray property of the Range class"
Your .FormulaArray content has some typos. Here's how it should look like (assuming all the above code is fine):
ActiveCell.FormulaArray= "=IF(ROWS(" & mc & ":" & mc1 & ")>SUM(IF(FREQUENCY(IF(Title<>" & chr(34) & chr(34) & ",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),1))," & chr(34) & chr(34) & ",INDEX(Title,SMALL(IF(FREQUENCY(IF(Title<>" & chr(34) & chr(34) & ",MATCH(Title,Title,0)),ROW(Title)-ROW(absolute)+1),ROW(Title)-ROW(absolute)+1),ROWS(" & mc & ":" & mc1 & "))))"
In general, remember that if you want the value of a variable to be printed into a string, you cannot write "a=mc+3" but rather a = " & mc & "+3".