VBA Excel - Autofill Method Out of Range Class Error - vba

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

Related

I am unable to use Auto Fill method properly in my vba code

This code gives me error 1004 and autofill method error If I remove first autofill method then 2nd autofill is working fine otherwise It gives an error,
Sub w()
Set ws1 = ActiveSheet
ws1.Columns("A:A").Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAbove
ws1.Range("A1").Value = "S.No"
ws1.Range("A1").Interior.Color = RGB(204, 204, 204)
LastRow = ws1.Cells(Rows.Count, "B").End(xlUp).Row
ws1.Range("A2").Select ws1.Range("A2").FormulaR1C1 = _
"=TEXT(RC[1],""000000000000"")&""-""&TEXT(RC[2],""0000"")"
ws1.Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow), Type:=xlFillDefault
ws1.Range("A2:A" & LastRow).Select
Set ws2 = Workbooks("Closing Asset Listing-Leading Ledger_Aug-17.xlsx").Worksheets(1)
LastRowclosing = ws2.Cells(Rows.Count, "B").End(xlUp).Row
ws2.Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ws2.Range("A2").FormulaR1C1 = _
"=TEXT(RC[6],""000000000000"")&""-""&TEXT(RC[7],""0000"")"
ws2.Range("A2").AutoFill Destination:=Range("A2:A" & LastRowclosing), Type:=xlFillDefault
Range("A2:A" & LastRowclosing).Select
ws1.Range("AB2").Select
ws1.Range("AB2").FormulaR1C1 = _
"=VLOOKUP(RC[-27],'[Closing Asset Listing-Leading Ledger_Aug-17.xlsx]Sheet1'!R2C1:R13C12,12,0)"
ws1.Range("AB2").AutoFill Destination:=Range("L2:L" & LastRow), Type:=xlFillDefault
Range("AB2" & LastRow).Select
End Sub
You are trying to autofill to a range on a different sheet. Your code should be:
ws2.Range("A2").AutoFill Destination:=ws2.Range("A2:A" & LastRowclosing), Type:=xlFillDefault

Add a another constant/column to this macro?

What this does now is take whats inputted Columns A:E and add whatever you list in column F, at the end of it, keeping A:E constant. This makes it much easier rather than copying and pasting but i want to add another row so that A:F is constant, switching the list to column G.
For ex, once it's outputted,
A1,B1,C1,D1,E1,F1
A1,B1,C1,D1,E1,F2
A1,B1,C1,D1,E1,F3
etc.
I just want to add another column to make it
A1,B1,C1,D1,E1,F1,G1
A1,B1,C1,D1,E1,F1,G2
A1,B1,C1,D1,E1,F1,G3
This is what I have so far.
Dim LastRowIput As String
With Sheets("Input")
LastRowInput = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
For I = 2 To LastRowInput
Dim LastRowLoc As String
With Sheets("Output")
LastRowLoc = .Cells(.Rows.Count, "F").End(xlUp).Row + 1
End With
Sheets("Input").Select
Range("F2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Output").Select
Range("F" & LastRowLoc).Select
ActiveSheet.Paste
Sheets("Input").Select
Range("A" & I & ":" & "E" & I).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Output").Select
Dim LastRow As String
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
End With
Range("A" & LastRow).Select
ActiveSheet.Paste
Dim LastRowLoc2 As String
With Sheets("Output")
LastRowLoc2 = .Cells(.Rows.Count, "F").End(xlUp).Row
End With
Application.CutCopyMode = False
Range("A" & LastRow & ":" & "E" & LastRowLoc2).Select
Selection.FillDown
Sheets("Input").Select
Next I
It seems that you want to copy the rows from A:G from Input to Output, expanding A:F in Output for every row in G.
Dim i As Long, lastRowInput As Long, nextRowOutput As Long
Dim wso As Worksheet
Set wso = Worksheets("Output")
With Sheets("Input")
lastRowInput = .Cells(.Rows.Count, "C").End(xlUp).Row
For i = 2 To lastRowInput
nextRowOutput = wso.Cells(.Rows.Count, "G").End(xlUp).Row + 1
.Range(.Cells(2, "G"), .Cells(2, "G").End(xlDown)).Copy _
Destination:=wso.Cells(nextRowOutput, "G")
.Range("A" & i & ":" & "F" & i).Copy _
Destination:=wso.Range(wso.Cells(nextRowOutput, "A"), _
wso.Cells(wso.Cells(.Rows.Count, "G").End(xlUp).Row, "F"))
Next i
End With
I've removed all methods involving the Range .Select and Range .Activate methods in favor of direct referencing.
                             Sample data from Input worksheet
                             Sample results from Output worksheet

Running VBA code in alternate sheet triggers wrong results - despite referencing?

The below code seeks to pull the value from a cell in the the 'Input' sheet, and then display it in the 'Output' sheet. It then shows the difference between the last value recorded and expresses the figure as a percentage.
When I run this code with the Output sheet active it works. However, when I run it from the output sheet it doesn't. Instead, it displays the value I wish to copy in column F in the input sheet and displays the difference and percentage difference in the wrong cells in the Output sheet.
It looks correctly referenced to me, but it obviously isn't. Thoughts on how to correct?
I appreciate that the code could be tidier - i'm very new to this.
Sub Button1_Click()
Dim LastRow As Long
Dim RecentRow As Long
With Sheets("Output")
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
RecentRow = .Cells(.Rows.Count, "F").End(xlUp).Offset(1, 0).Row
Range("F" & LastRow).Select
ActiveCell.Offset(1, 0).Formula = "=Input!B4"
ActiveCell.Offset(1, 0).Copy
ActiveCell.Offset(1, 0).PasteSpecial (xlValues)
End With
ActiveCell.Offset(0, 1).Formula = "=(F" & RecentRow & "-F" & LastRow & ")"
ActiveCell.Offset(0, 2).Formula = "=((F" & RecentRow & "/F" & LastRow & ")-1)"
End Sub
Thanks.
The below code should fix your issue - it's because your Range("F" & LastRow).Select did not have a period before Range.
Sub Button1_Click()
Dim LastRow As Long
Dim RecentRow As Long
With Sheets("Output")
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
RecentRow = .Cells(.Rows.Count, "F").End(xlUp).Offset(1, 0).Row
With .Range("F" & LastRow)
.Offset(1, 0).Formula = "=Input!B4"
.Offset(1, 0).Copy
.Offset(1, 0).PasteSpecial (xlValues)
.Offset(0, 1).Formula = "=(F" & RecentRow & "-F" & LastRow & ")"
.Offset(0, 2).Formula = "=((F" & RecentRow & "/F" & LastRow & ")-1)"
End With
End With
End Sub
Furthermore, you can gain a bit more efficiency in your code with the below:
Sub Button1_Click()
Dim LastRow As Long
With ThisWorkbook.Sheets("Output") 'Allow for code to work even if in another workbook.
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
With .Range("F" & LastRow)
.Offset(1, 0).Value2 = ThisWorkbook.Sheets("Input").Range("B4").Value2
.Offset(0, 1).Formula = "=(F" & LastRow + 1 & "-F" & LastRow & ")"
.Offset(0, 2).Formula = "=((F" & LastRow + 1 & "/F" & LastRow & ")-1)"
End With
End With
End Sub

AutoFill using LastRow

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)

AutoFill Macro to Length

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