I am trying to apply VLOOKUP to get the data from a workbook named LPDwith sheet RAS(Offshore) having my lookup range. I want to look for the column A which is present in a different workbook named fresher with sheet as DestSh.So I want the results after applying VLOOKUP in the sheet DestSh in column z.
I have tried
iSCount = 2
For Each cell In DestSh.Range("Z2:Z" & lstRowofIpSheet)
With DestSh
sFormula = "=VLOOKUP(DestSh!$A" & iSCount & ",'" & sLPDFileName & "]RAS(Offshore)'!$B:$F,5,false)"
.Range("Z" & iSCount).Formula = sFormula
iSCount = iSCount + 1
End With
Next cell
But I am getting the error as: Application defined or Object defined error.
Any help or suggestions shall be highly appreciated.
In this line:
sFormula = "=VLOOKUP(DestSh!$A" & iSCount & _
",'" & sLPDFileName & "]RAS(Offshore)'!$B:$F,5,false)"
Missing an opening square bracket:
sFormula = "=VLOOKUP(DestSh!$A" & iSCount & _
",'[" & sLPDFileName & "]RAS(Offshore)'!$B:$F,5,false)"
Related
How would I change the below lookup to refer to the first sheet in the workbook and not 'Sheet 1'?
.Range("I15:I" & lastRow).FormulaR1C1 = _
"=IF(VLOOKUP(RC[-8],[" & combinedWorkbook.Name & "]Sheet1!C1:C2,1,TRUE)=RC[-8],VLOOKUP(RC[-8],[" & combinedWorkbook.Name & "]Sheet1!C1:C2,2,TRUE),NA())"
Thanks
You are concatenating already the workbook name into the formula. The same way you could concatenating the name of the first worksheet too. The first worksheet is the first sheet in workbook's Worksheets collection.
So combinedWorkbook.Worksheets(1).Name would be the name of the first worksheet in workbook combinedWorkbook.
But names could containing spaces like "My Worksheet Name". Then the reference itself must be within single quotes like 'My Worksheet Name'!A1.
So all together:
.Range("I15:I" & lastRow).FormulaR1C1 = _
"=IF(VLOOKUP(RC[-8],'[" & combinedWorkbook.Name & "]" & combinedWorkbook.Worksheets(1).Name & "'!C1:C2,1,TRUE)=RC[-8],VLOOKUP(RC[-8],'[" & combinedWorkbook.Name & "]" & combinedWorkbook.Worksheets(1).Name & "'!C1:C2,2,TRUE),NA())"
if you are going to re-use it then I would declare:
Dim first_sheet As String
first_sheet = combinedWorkbook.Sheets(1).Name
And then use it in your code like this:
.Range("I15:I" & lastRow).FormulaR1C1 = _
"=IF(VLOOKUP(RC[-8],[" & combinedWorkbook.Name & "] & first_sheet & !C1:C2,1,TRUE)=RC[-8],VLOOKUP(RC[-8],[" & combinedWorkbook.Name & "] & first_sheet & !C1:C2,2,TRUE),NA())"
This is really a small example how to refer to the first worksheet.
Take the name of the first worksheet and save it as a variable, using the .Name property.
Concatenate the variable in the formula:
Public Sub TestMe()
Dim wks1 As String
wks1 = Worksheets(1).Name
'worksheets should not contains spaces! :) left and right
Worksheets(1).Name = Trim(wks1)
Range("I15:I20").FormulaR1C1 = "=" & wks1 & "!R1C1"
End Sub
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)"
I have an excel-workbook containing two worksheets, and I have written code to transfer data from sheet No.1 to sheet No.2.
What I need is to include a condition that checks if the column G does not contain a certain value. In that case I would like a MsgBox to display "Check..".
The interested range in the Sheet 1 is (A3:J50), so the condition would interest cells G3 to G50.
My current code is:
Sub kk()
Dim lastrow As Integer
lastrow = [b50].End(xlUp).Row
Range("b3:J" & lastrow).Copy Sheets("Daily Rec.").Range("b" & Sheets("Daily Rec.").[b1000].End(xlUp).Row + 1)
Range("b3:j" & lastrow).ClearContents
MsgBox ("Date Posted")
Sheets("Daily Rec.").Activate
MsgBox ("Check..")
End Sub
please advice
This should help get you started.
But like others have mentioned, we need more info to help.
Sub Okay()
Dim source As Range
Dim target As Range
Dim found As Range
Dim cell As Range
Set source = ThisWorkbook.Worksheets("Sheet 1").Range("A3:J50")
Set target = ThisWorkbook.Worksheets("Sheet 2").Range("G3:G50")
For Each cell In source.Cells
Set found = target.Find(cell.Value)
If found Is Nothing Then
MsgBox "Check.." & vbNewLine _
& "Cell [" & cell.Address(0, 0) & "] on sheet [" & cell.Parent.Name & "]" _
& vbNewLine _
& "was not found within " & vbNewLine _
& "cell range of [" & target.Address(0, 0) & "] on sheet [" & target.Parent.Name & "]"
End If
Next cell
End Sub
I am writing a fairly complicated macro, but the problem I am having is creating a formula on one sheet, in the code "Input_Sheet", to equate itself to a cell in a newly created worksheet, variably set as "ws". Each iteration of ws is named, so that's not an issue. I figured the correct way to do it was:
ActiveCell.FormulaR1C1 = " & ws.name & !R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
(Don't worry about the totalRowCounter & totalColumnCounter variables, they are defined appropriately). I just don't know why the formula isn't appropriately referencing the new ws sheet. Any thoughts?
You just need to take your ws.name out of the quotes, also adding an apostrophe before and after the sheet name will help with any sheets that may have a space in the name:
ActiveCell.FormulaR1C1 = "='" & ws.name & "'!R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
I think it would be the below:
ActiveCell.FormulaR1C1 = "=" & ws.name & "!R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
This question already has an answer here:
Using VBA to place multiple formulas in one cell
(1 answer)
Closed 6 years ago.
I'm trying to fill cells with a for loop like this:
For i = 1 To Target
Range("C" & i & ":C" & i ).Formula = "='Sheet1'!A" & i & "/" & "'Sheet2'!B" & i"
Next i
And I want to see that in the formula bar:
='Sheet1'!A1 & "/" & 'Sheet2'!B1
='Sheet1'!A2 & "/" & 'Sheet2'!B2
...
Unfortunately it's not working. If I try only the first part like that:
For i = 1 To Target
Range("C" & i & ":C" & i ).Formula = "='Sheet1'!A" & i
Next i
This code results this fine, but this is not enough for me:
='Sheet1'!A1
='Sheet1'!B1
...
What is wrong with my frist code?
You could try
Range("C" & i & ":C" & i).FormulaR1C1 = "=Sheet1!RC1 & ""/"" & Sheet2!RC2"
If you are having trouble with double quotes in a concatenated string, I sugest you remove as many as you can. Single characters can be referred to by their ASCII code number with the Chr function. The / character is 47.
Dim i As Long, target As Long
With ActiveSheet
target = .Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To target
.Range("C" & i).Formula = "='Sheet1'!A" & i & Chr(47) & "'Sheet2'!B" & i
Next i
End With
You do not have to increment through a range is the formula is constructed properly. Either an xlA1 or xlR1C1 reference style can be used (see xlReferenceStyle enumeration).
Dim i As Long, target As Long
With ActiveSheet
target = .Cells(Rows.Count, "A").End(xlUp).Row
'xlA1 style
.Range("C1:C" & target).Formula = "='Sheet1'!A1/'Sheet2'!B1"
'xlR1C1 Style
.Range("C1:C" & target).FormulaR1C1 = "='Sheet1'!RC1/'Sheet2'!RC2"
End With
Note that an xlR1C1 style requires the Range.FormulaR1C1 instead of the Range.Formula.