I am doing a vlookup in cell J1 of Sheet2 on a table in Sheet1. After that, I want to extend the formula down to the last row of column J in Sheet2. I'm having trouble with the AutoFill part on column J. Also, I'm using another LastRow statement for the vlookup in Sheet1.
Here's what I have (starting on Sheet1):
*SOME OTHER CODE*
LASTROW = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Select
LASTROW2 = Range("A" & Rows.Count).End(xlUp).Row
ActiveWindow.SmallScroll Down:=-51
Range("J1").Select
Application.CutCopyMode = False
ActiveCell.FormulaLocal = "=VLOOKUP(F1,Sheet1!$F$2:$G$" & LASTROW & ",2,false)"
Selection.AutoFill Destination:=Range("J2:J" & LASTROW2)
Thanks for your help!
Maybe your Destination should include the starting cell J1:
Selection.AutoFill Destination:=Range("J1:J" & LASTROW2)
Related
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("inbd")
Dim wsDestination As Worksheet: Set wsDestination = Sheets("test")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("A1:N" & LastRow).AutoFilter Field:=1,
Criteria1:=Worksheets("test").Cells(1, 26).Value
ws.Range("f2:f" & LastRow).SpecialCells(xlCellTypeVisible).Copy Range("C6")
DestinationRow = wsDestination.Cells(wsDestination.Rows.Count, "C").End(xlUp).Row + 1
wsDestination.Range("C" & DestinationRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
ws.Range("A1:N" & LastRow).AutoFilter Field:=1
End Sub
Good evening, Paste values part is giving me an object defined error and I don't know why
You want a test to ensure there are visible cells to copy from:
If Application.WorksheetFunction.Countif(ws.Range("A1:N" & LastRow),Worksheets("test").Cells(1, 26).Value) > 0
. A test that you have more than one cell for your filter
If LastRow > 1
and you also have already pasted with this line:
ws.Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).Copy Range("C6")
You pasted to C6 and now have an empty clipboard so cannot paste again.
Perhaps you wanted:
ws.Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).Copy
DestinationRow = wsDestination.Cells(wsDestination.Rows.Count, "C").End(xlUp).Row + 1
wsDestination.Range("C" & DestinationRow).PasteSpecial xlPasteValues
You have already pasted the copied cells into the inbd sheet C6.
Your code does not make much sense. You determine the last row of data before you filter, you copy and paste, then you paste again. I suggest you first filter, then determine the last row in both sheets, then do the copy and paste special right after one another without the wrong paste to C6 step.
I have Autofill Method Out of Range Class Error from some reason and I can't find why. I have lots of formulas in my code and I use it a lot and it is stops my code every time. this is the relavant part of my sub:
'U means union, M means main
'Advanced filter for visual worksheet
UnionWB.Worksheets("Union").ShowAllData
Dim ULR As Long, ULC As Long, MLR As Long
ULR = Cells(Rows.Count, "A").End(xlUp).Row
ULC = Cells(1, Columns.Count).End(xlToLeft).Column
With MainWB.Worksheets(sheet1)
MLR = .Cells(Rows.Count, "A").End(xlUp).Row
End With
MainWB.Worksheets("aheet2").Columns("A:Z").Clear
UnionWB.Worksheets("Union").Range(Cells(1, 1), Cells(ULR, ULC)).AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=MainWB.Worksheets("sheet1").Range("A1", "A" & MLR), Unique:=False
UnionWB.Worksheets("Union").Activate
Range("A1", "Y" & ULR).Select
Selection.Copy
MainWB.Worksheets("sheet2").Activate
Range("A1").PasteSpecial xlPasteValues
Range("AB2").Select
Selection.AutoFill Destination:=Range("AB2", "AB" & MLR)
If MLR > 2 Then
With MainWB.Worksheets("sheet1).Range("N2")
ActiveCell.FormulaR1C1 = _
"=IF(ISNUMBER(SEARCH(""sheet2"",RC[-9])),""yes"",""no"")"
Selection.AutoFill Destination:=Range("N2", "N" & MLR)
End With
End If
Selection.AutoFill Destination:=Range("AB2", "AB" & MLR)
Selection.AutoFill Destination:=Range("N2", "N" & MLR)
Change the 2 lines below:
Range("AB2").Select
Selection.AutoFill Destination:=Range("AB2", "AB" & MLR)
To:
Range("AB2").AutoFill Destination:=Range("AB2:AB" & MLR), Type:=xlFillDefault
I have a spreadsheet with a column of values that I would like to divide by a fixed cell (say C3), and have the results in an adjacent column.
I would like this code to run to the last available row (with values) as well.
Would greatly appreciate any help! Thanks!
If your source values were in, for instance, A1:A7 and you want to copy them to B1:B7 and divide by C3 at the same time, you could:
With ActiveSheet
'Determine last row
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Copy the original values from column A to column B
.Range("B1:B" & lastRow).Value = .Range("A1:A" & lastRow).Value
'Copy / Pastespecial Divide using cell C3
.Range("C3").Copy
.Range("B1:B" & lastRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlDivide, _
SkipBlanks:=False, _
Transpose:=False
Application.CutCopyMode = False
End With
You can use Do While
Do While Cells(iCol, 3).Value <> ""
'Do some thing
iCol = iCol + 1
Loop
I am running into issues with the Paste Special part of the following code
Sub Copy_Filter1()
Sheets("MASTER PLACEMENT").Select
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Sheets("MASTER PLACEMENT").Range("A1").CurrentRegion.AutoFilter
Selection.AutoFilter Field:=52, Criteria1:=">=104"
Columns("AG:AS").EntireColumn.Hidden = True
Rows("1:1").EntireRow.Hidden = True
If (Range("A" & Rows.Count).End(xlUp).Row <= LastRow) Then
Range("A2").CurrentRegion.Copy
Sheets("Sheet1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
End If
End Sub
PasteSpecial is a method of the Range object, not the Worksheet object (which is where you are currently using it).
For example, your call should look like:
' Paste the current clipboard contents to cell B2 on Sheet1.
Sheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
use something like below
Sub Copy_Filter1()
Sheets("Sheet1").Range("A1:A1000").Select
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet1").Range("A1").CurrentRegion.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=">=104"
If (Range("A" & Rows.Count).End(xlUp).Row <= LastRow) Then
Range("A2").CurrentRegion.Copy
Sheets("Sheet1").Range("C3").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
End If
End Sub
I am trying to Fill a formula that I have in D1 and fill down D to the length of C. I am using the follwing macro and I am getting the following error - Compile Error: Expected end with
Sub Macro3()
Macro3 Macro
Range("D1").Select
ActiveCell.FormulaR1C1 = "=RC[-2]*(-1)+RC[-1]"
Range("D1").Select
Dim LastRow As Long
With Sheets("Sheet2")
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("D1").AutoFill Destination:=Range("D2:D" & LastRow)
End Sub
Your problem was a simple one. I used the macro recorder to AutoFill a Formula Range and found that the Destination Range starts with the Formula Range, so
Range("D1").AutoFill Destination:=Range("D2:D" & LastRow)
Should be:
Range("D1").AutoFill Destination:=Range("D1:D" & LastRow)
Here is working code, both fixed and cleaned up a bit :)
Sub Macro3()
With Sheets("Sheet1")
Dim LastRow As Long
LastRow = Range("C" & Rows.Count).End(xlUp).Row
With Range("D1")
.FormulaR1C1 = "=RC[-2]*(-1)+RC[-1]"
.AutoFill Destination:=Range("D1:D" & LastRow)
End With
End With
End Sub