VBA application-defined or object-defined error

JARichard74

Board Regular
Joined
Dec 16, 2019
Messages
114
Office Version
  1. 365
Platform
  1. Windows
Get the error on this code on the first .Range line
VBA Code:
ElseIf scle = "MDB-H" Then
startRow = k * z + 4 - z
endRow = k * z + 2
startCell = k * z + j + 3 - z
Hrngel = WorksheetFunction.Average(Range("B" & startRow & ":B" & endRow)) - WorksheetFunction.StDevP(Range("B" & startRow & ":B" & endRow))
Hrngeh = WorksheetFunction.Average(Range("B" & startRow & ":B" & endRow)) + WorksheetFunction.StDevP(Range("B" & startRow & ":B" & endRow))
OOrnge = WorksheetFunction.CountIf(Range("J" & startRow & ":J" & endRow), "No") - 1
    [COLOR=rgb(184, 49, 47)].Range(.Cells(startCell, 10), .Cells(startCell, 10)).Formula = "=IF(OR(B" & startCell & " < Hrngel,B" & startCell & " > Hrngeh,""No"",""Yes"")"[/COLOR]
    .Range(.Cells(startCell, 6), .Cells(startCell, 6)).Formula = "=IF(OR(B" & startCell & " < Hrngel,B" & startCell & " > Hrngeh,z - 1,RANK.EQ(B" & startCell & ",B" & k * z + 4 - z & ":B" & (k * z) + 2 & ",1) - OOrnge))"
    .Range(.Cells(startCell, 3), .Cells(startCell, 3)).Formula = "=IF(OR(B" & startCell & " < rngel,B" & startCell & " > rngeh),0,VLOOKUP(B" & startCell & "/B" & (k * z + 3 - z) & "*100,Data!$G$3:$I$13,3,TRUE))"
    .Range(.Cells(startCell, 5), .Cells(startCell, 5)).Formula = "=IF(ISBLANK(B" & startCell & "),"""",IF(B" & startCell & " = ""Locked"","""",C" & startCell & " * D" & startCell & "/10))"
    .Range("B" & startCell).NumberFormat = "$#,##0.00"
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
A parenthesis is missing in your formula:
Rich (BB code):
.Range(.Cells(startCell, 10), .Cells(startCell, 10)).Formula = "=IF(OR(B" & startCell & " < Hrngel,B" & startCell & " > Hrngeh,""No"",""Yes""))"
                                                                                                                                              /\
 
Upvote 0
A parenthesis is missing in your formula:
Rich (BB code):
.Range(.Cells(startCell, 10), .Cells(startCell, 10)).Formula = "=IF(OR(B" & startCell & " < Hrngel,B" & startCell & " > Hrngeh,""No"",""Yes""))"
                                                                                                                                              /\
Thanks for pointing in the right direction. Actually the OR bracket was missing. The code works but places something not workable which indicates I am not using the variables properly. This is the formula that gets put in the cells =IF(OR($B24 < 2,$B24 <@ Mrnge - 2,$B24 <@ Mrnge + 2),"No","Yes")
 
Upvote 0
There is no way that line of code can insert the formula you have shown.
What are Hrngel & Hrngeh?
 
Upvote 0
There is no way that line of code can insert the formula you have shown.
What are Hrngel & Hrngeh?
Sorry, wrong cell. This is what it returns =IF(OR(B9 <@ Hrngel,B9 >@ Hrngeh),"No","Yes"). Hrngel and Hrngeh are declared as Long.

VBA Code:
Hrngel = WorksheetFunction.Average(Range("B" & startRow & ":B" & endRow)) - WorksheetFunction.StDevP(Range("B" & startRow & ":B" & endRow))
Hrngeh = WorksheetFunction.Average(Range("B" & startRow & ":B" & endRow)) + WorksheetFunction.StDevP(Range("B" & startRow & ":B" & endRow))
 
Upvote 0
Actually the OR bracket was missing.
You are correct, I wasn't. This again shows how quickly one overlooks something.

The code works but places something not workable which indicates I am not using the variables properly.
It's all about the quotation marks.
Replace this
VBA Code:
.Formula = "=IF(OR(B" & startCell & " < Hrngel,B" & startCell & " > Hrngeh,""No"",""Yes"")"

by this (and change the other lines of code accordingly...).
VBA Code:
.Formula = "=IF(OR(B" & startCell & " < " & Hrngel & ", B" & startCell & " > " & Hrngeh & "), " & """No""" & ", " & """Yes""" & ")"
 
Upvote 0
Solution
You are correct, I wasn't. This again shows how quickly one overlooks something.


It's all about the quotation marks.
Replace this
VBA Code:
.Formula = "=IF(OR(B" & startCell & " < Hrngel,B" & startCell & " > Hrngeh,""No"",""Yes"")"

by this (and change the other lines of code accordingly...).
VBA Code:
.Formula = "=IF(OR(B" & startCell & " < " & Hrngel & ", B" & startCell & " > " & Hrngeh & "), " & """No""" & ", " & """Yes""" & ")"
Thanks. Marked as solution
 
Upvote 0

Forum statistics

Threads
1,215,449
Messages
6,124,911
Members
449,195
Latest member
Stevenciu

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