VBA code is not working

Abhishekghorpade

Board Regular
Joined
Oct 3, 2018
Messages
78
Hi,

I am getting "Application-defined or object-defined error" error msg for below codes.. Not able to figure out where i am going wrong. Please help

Sub conditionE()
Dim lastrow As Long
lastrow = WorksheetFunction.CountA(Worksheets("2018_T933").Range("A:A"))
With Worksheets("2018_T933").Range("A6")
.Formula = "=IF(AND(YEAR(I6)<=2016,H6<=0.5,D6>=0.5),""D"",""A"")"
.AutoFill Destination:=Worksheets("2018_T933").Range("A6:A" & lastrow)
End With
End Sub
------------------------------------------------------------------------------------------
Sub copyfromsheet1()
Dim lastrow As Long


lastrow = WorksheetFunction.CountA(Worksheets("Sheet2").Range("A:A"))
With Worksheets("Sheet2").Range("H2")
.Formula = "=IF(A2=""A"",""A"","" "")"
.AutoFill Destination:=Worksheets("Sheet2").Range("H2:H" & lastrow)
End With
With Worksheets("Sheet2").Range("I2")
.Formula = "=IF(A2=""A"",""A"","" "")"
.AutoFill Destination:=Worksheets("Sheet2").Range("I2:I" & lastrow)
End With


With Worksheets("Sheet2").Range("K2")
.Formula = "=IF(A2=""A"",2018_T933!H6,"" "")"
.AutoFill Destination:=Worksheets("Sheet2").Range("K2:K" & lastrow)
End With


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Which line of code is highlighted when you hit "debug" when the error message pops up?
 
Last edited:
Upvote 0
After below line i am getting error

.AutoFill Destination:=Worksheets("2018_T933").Range("A6:A" & lastrow)

and

.AutoFill Destination:=Worksheets("Sheet2").Range("K2:K" & lastrow)
 
Upvote 0
What value is it returning for lastrow? If column A is blank, and it is returning 0, you will get errors.

You seem to be looking at column A in order to find your last row, but then you are placing your formulas in column A.

That does not seem quite right, unless you initially have values in column A and want to overwrite them.

What is in column A initially?
Is column D, H, or I a better column to use to determine where the last row of data really exists?

Let's say it is column D. We can easily assign the formula to all cells at once without having to use AutoFill, like this:
Code:
Sub conditionE()
    Dim lastrow As Long
    lastrow = Worksheets("2018_T933").Cells(Rows.Count, "D").End(xlUp).Row
    Worksheets("2018_T933").Range("A6:A" & lastrow).FormulaR1C1 = _
        "=IF(AND(YEAR(RC[8])<=2016,RC[7]<=0.5,RC[3]>=0.5),""D"",""A"")"
End Sub


 
Last edited:
Upvote 0
You are welcome.
You can apply the same method to your other procedure too.

Here is a little trick. If you are not sure what your formula looks like when in R1C1 format, simply turn on your Macro Recorder, and enter it in the appropriate cell on your worksheet, then stop the Macro Recorder.
Then, if you view the VBA code it recorded, it will show you what that formula looks like in R1C1 format, and you can copy/paste that part into your code.
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,999
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top