Inserting vlookup via formular1c1 in a Macro - vba

I'm trying to fill a Vlookup into a range of cells using .FormulaR1C1 and get an error 1004. I have almost this exact code elsewhere in my macro and it works fine so I'm not sure what's wrong. It's probably a simple fix and I'm just not seeing it...
Here is the code:
Range("W2").Select
Range(Selection, Selection.End(xlDown)).FormulaR1C1 = "=VLOOKUP(RC[-2],$AA$2:$AC$35,3,TRUE)"

If you are going to use R1C1 then all references must be in that format.
Range(Range("W2"), Range("W2").End(xlDown)).FormulaR1C1 = "=VLOOKUP(RC[-2],R2C27:R35C29,3,TRUE)"
But you should always apply the parentage of the sheet to each range object:
With Worksheets("Sheet1") 'Change to your sheet
.Range(.Range("W2"), .Range("W2").End(xlDown)).FormulaR1C1 = "=VLOOKUP(RC[-2],R2C27:R35C29,3,TRUE)"
End With

Related

VBA Runtime Error 1004 “Application-defined or Object-defined error” when setting Range in macro

I'm going to preface this by saying that I am very new to VBA and this is my first project with it however, I'm trying quite hard because otherwise it is manual copy paste ~200 times.
Unfortunately, for a first project it has been difficult.
EDITED FOR CLARITY (HOPEFULLY): The main idea is that I need to start at the beginning of a drop down list, copy the first string listed, then paste that string down the column. This changes the numerical data adjacent to the right. I then want to select this newly changed numerical data and copy and paste it to a different sheet in the same workbook in the first blank space in column F. I then want the code to iterate through the drop down list and do this for all 51 strings in the drop down. However it needs to paste offset by 3 columns for each iteration to copy the data to the correct column in the other sheet.
Here is my code thus far
Option Explicit
Sub PtComp()
'
' PtComp Macro
'
'
Dim List1 As String
Dim Range1 As Range
Dim Line1 As Range
Dim i As Integer
Dim Begin As Range
ActiveWorkbook.Sheets("Sample Data Summary").Activate
List1 = Selection
Set Range1 = Evaluate(ActiveSheet.Range(List1).Validation.Formula1)
For Each Line1 In Range1
Selection.Copy
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveSheet.Selection.Copy
ActiveWorkbook.Sheets("Pt Comparison").Activate
Begin = ActiveSheet.Range("F1").End(xlDown).Offset(-1, 0)
For i = 0 To 148 Step 3
Begin.Offset(0, i).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues
Next i
Next Line1
End Sub
It is highlighting this line
Set Range1 = Evaluate(ActiveSheet.Range(List1).Validation.Formula1)
Any help would be greatly appreciated. Sorry if my code is trash, like I said, first timer hoping to get better.
EDIT: Also, I looked back at older questions with the same error and thought that it was maybe because it wasn't clear what worksheet I was trying to define the range in, hence why my code is full of 'ActiveSheet' but still no luck.
Your code assumes List1 contains a valid range address, and thus that the active cell on that "Sample Data Summary" worksheet, contains a valid range address.
Apparently that isn't always the case. Search for more details on the On Error statement for ideas about how you could go about handling that situation.
You need to read up on How to avoid using Select in Excel VBA macros, and know that clipboard operations in a tight loop is pretty much the single slowest thing you can do in Excel-VBA.
Seems you want something like this:
Set Range1 = Evaluate(Selection.Validation.Formula1)
Your code blows up on Range(List1) because List1 does not contain a valid range address.

GoalSeek only within VBA, without depending on a WorkSheet Cell

Very shortly: Instead of ActiveCell of ActiveCell.GoalSeek Goal:=0, ChangingCell:=Range("GM")formula, how can I use a variable? So I want to use something like GoalSickVariable.GoalSeek Goal:=0, ChangingCell:=Range("GM")
.
As a detail:
I am trying to shorten my current GoalSeek formulation and I'm stucked on the last line.
So far, I was running my codes within WorkSheet but as I can't assume which part of the Worksheet will be empty, I was going to a cell which is away from the reference cell by .Offset(0,25) but as user can also use that cell, I would like not to use any range within WorkSheet - if it's possible.
...
ActiveCell.Offset(0, 25).Select
ActiveCell.Formula = "=ROUNDUP(SellPrice,-3)"
RoundedCellAddress = ActiveCell.Address
ActiveCell.Offset(0, 1).Select
ActiveCell.Formula = "=(" & RoundedCellAddress & "-" & "SellPrice" & ")"
'SellPrice is a NamedRange from this current Worksheet
ActiveCell.GoalSeek Goal:=0, ChangingCell:=Range("GM")
'GM is also a NamedRange from this WS and it changes SellPrice's value.
...
While it's a very bad practice with many ActiveCell and a Assuming Cell Reference, this above code is working without any problem. Anyway as I explained above, I don't want to assume this .Offset(0,25) cell and don't want to use these ActiveCells.
So here below how I transformed my code into a good practice:
...
RoundedSellPrice = Application.WorksheetFunction.RoundUp(ws4.Range("SellPrice"), -3)
GoalSick = RoundedSellPrice - ws4.Range("SellPrice")
GoalSick.GoalSeek Goal:=0, ChangingCell:=ws4.Range("GM")
...
But now as you can easily guess, it gives me Invalid Qualifier error on GoalSick.GoalSeek line because GM and SellPrice are variables defined within WorkSheet so they are (I think) not able to run GoalSeek formula from VBA module without exactly referring to any cell(contains formula) within the WorkSheet.
If somehow I can not do this formulation without referring to a cell within the WorkSheet, how should I suppose to do without assuming a dummy cell like .Offset(50,150) or without creating a new empty WorkSheet?

VBA Code to copy and paste active column with merged cells inside

I'm trying to copy the active column and paste it next to it but the code selects the entire worksheet because it has merged cells in.
Sub CopyPaste()
Columns(ActiveCell.Column).Selection
Selection.Copy
ActiveCell.Offset(0,1).PasteSpecial Paste:=xlPasteAll
End Sub
Could you please help me adding the missing code to ignore merged cells?
This is yet another reason to avoid using Select in VBA for Excel. Your selection will expand with the merged cells. You can try this:
ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 1).EntireColumn
And again, you should find some way to avoid counting on the ActiveCell in your code, and use some fully qualified range.

Excel IF cell reference formula being changed when recorded in VBA

I have a macro that works fine. I'm adding a formula to it that describes the status of the workbook for someone else to review. When I record the macro of implementing the formula into the sheet, VBA records it using relative references even though I do not have the relative references button selected. The relative references do not point correctly so I need to fix it. I checked this post (adding a dynamic cell reference in vba) and am now thinking that I should adjust the formula with some VBA reference code but I'm not sure if what the post is using is suitable for me. Am I going in the right direction?
Excel Formula:
=IF(Selections!K2="","Not prepped","Prepped")
When recorded into VBA:
ActiveCell.FormulaR1C1 = _
"=IF(Selections!R[-41]C[-1]="""",""Not prepped"",""Prepped"")"
What I need in VBA code:
ActiveCell.FormulaR1C1 = _
=IF(Selections!K2="""",""Not prepped"",""Prepped"")
ok, then is this your answer:
dim rng as range
set rng = thisworkbook.sheets("Sheet1").range("L5")
rng.Formula = "=IF(Selections!K2="""",""Not prepped"",""Prepped"")"

Run time Error - 438

I have the following code in which I am trying to copy data from one sheet to another in same workbook. When I run the code I get Runtime error -438
Sub Copy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet2").Activate
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Try the following code. You should not rely on Activate and Select.
Sub ZCopy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet1").Paste Destination:=Worksheets("Sheet2").Range("E1")
Application.CutCopyMode = False
End Sub
Interesting Reads
MSDN
How to avoid using Select in Excel VBA macros
Do you have a particular need for copy and paste? This can be slow and inefficient. If you're just copying data from one sheet to another, you can set the values of one range equal to the values of another range and avoid the whole thing.
Sheets("Sheet2").Range("E1:H20").Value = Sheets("Sheet1").Range("A1:D20").Value
This will set the range from cells E1:H20 on Sheet2 to the same values as those in the range A1:D20 on Sheet1, which is effectively a copy and paste. I should add that this will work only for the values themselves.
If there is specific formatting (or formulas) that you need copied and pasted, this method won't work.