Excel VBA "Appliction-defined or object-defined error" - vba

I'm trying to clculate a formula that takes number from Column D and calcultes this number minus 14 in column C.
Then, I'm triyng to autofill the range down to the first cell I calculate.
at first it was working, but now it shows me an error:
appliction-defined or object-defined error
to this line of code
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)
If someone would help me to solve this prblem I'll be glad.
OP_wb.Sheets("Optic Main").Activate
Dim FirstRow As Range
Dim lastrow As Range
Set FirstRow = Range("C1").End(xlDown).Offset(1, 0)
Set lastrow = Range("E1").End(xlDown).Offset(0, -2)
Range("E1").End(xlDown).Offset(0, -2).Select
Range(FirstRow, lastrow).FormulaR1C1 = "=(c4-14)"
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)

Instead of
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)
try
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow.Row)

Related

VBA Code to Fill Series down to the next blank cell once

I am trying to do something I imagine is really simple however I am stuck on part of the code.
I need the code to look at last row with a number in in column A and fill the series down once i.e.
A20 = 0019
A21 = 0020
Dim LastRow As Variant
Dim LastBlankRow As Variant
LastRow = Range("A" & Rows.Count).End(xlUp).Offset(0).Select
LastRow2 = Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.AutoFill Destination:=Range(LastRow & LastBlankRow), Type:=xlFillDefault
I started off with this code and developed it to the one above however my range will change each time as more data is entered.
Range("A20").Select
Selection.AutoFill Destination:=Range("A20:A21"), Type:=xlFillDefault
Range("A20:A21").Select
I imagine its something simple I have missed however I cant figure it out.
Thanks!
Don't rely on ActiveSheet, always qualify your Range, Rows objects with your Worksheet.
Code
Option Explicit
Sub FillOneDown()
Dim LastRow As Variant
With Worksheets("Sheet1") ' modify "Sheet1" to your sheet's name
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A" & LastRow).AutoFill Destination:=.Range(.Cells(LastRow, "A"), .Cells(LastRow + 1, "A")), Type:=xlFillDefault
End With
End Sub

VBA Sorting multible rows

DoEvents
Dim lastrow As Long
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Range("A3:H" & lastrow).Sort key1:=Range("H3:H" & lastrow), _
order1:=xlAscending, Header:=xlNo
I want to sort my table by the dates in row H, but it give me the error messages "Run-time error '1004': Sort method of Range class failed" and it points to the last row of code. Thanks in advance
Try changing lastrow = Cells(Rows.Count, 2).End(xlUp).Row to lastrow = Cells(Rows.Count, 8).End(xlUp).Row

VBA - how do I divide a range of cells by a fixed cell

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

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