Using variable worksheet as formula destination in vba - vba

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

Related

VBA lookup refer to first sheet in workbook not "Sheet1"

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

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

Calling Cell Reference from Different Workbook

I am trying to multiply two variable of different workbook in another workbook and below code look like everything is fine but its not working... appreciated if someone solving the issue
'PTC is WB1 cell reference and Outstval is WB2 Cell reference
wkb.Activate
With wkb.Worksheets(1)
.Range("C4").Formula = "= ( " & PTC.Address(True, True, xlR1C1, xlExternal) & "*" & Outstval.Address(False, False, xlR1C1, xlExternal) & " ) "
lr = .Range("A" & Rows.count).End(xlUp).Row
.Range("C4").Resize(lr - 1, 5).FillDown
.Calculate
End With

How to apply vlookup on the different workbook range?

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

MS Excel VBA - Copying SpecialCells while keeping the destination formatting

I would like to copy these special cells to a destination, while keeping the destination's formatting. I'm thinking I have to put .values somewhere in the lines. The code is as follows:
GRBReportSheet.Range("C" & GRBReportSheetFR & ":E" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy Destination:=GRB.Range("C30")
GRBReportSheet.Range("F" & GRBReportSheetFR & ":J" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy Destination:=GRB.Range("H30")
Thank you in advance for your time!
Try this.
GRBReportSheet.Range("C" & GRBReportSheetFR & ":E" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy
GRB.Range("C30").PasteSpecial xlValues
GRBReportSheet.Range("F" & GRBReportSheetFR & ":J" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy
GRB.Range("H30").PasteSpecial xlValues