Object variable or with block variable not set

minmark

New Member
Joined
Jul 18, 2016
Messages
44
Dear Gents,
The following code can run well sometimes,
but after run other macro , following code show run time error 91: Object variable or with block variable not set
Would you please help me to figure out this problem?
Thanks.
Code:
Option Explicit
Public rngA, rngB, cell As Range
Public a, B, C, D, k, VD, VC, PtS, Sx, Vt, PtN, Age, Nat, OH, TT, Re, Inj, IL, DS As Double
Sub SearchCol()
VD = Sheet1.Cells.Find(what:="VisitDay").Column
VC = Sheet1.Cells.Find(what:="VisitCase").Column
PtS = Sheet1.Cells.Find(what:="PtState").Column
Sx = Sheet1.Cells.Find(what:="Sex").Column
Vt = Sheet1.Cells.Find(what:="VisitTime").Column
PtN = Sheet1.Cells.Find(what:="PtName").Column
Age = Sheet1.Cells.Find(what:="Age").Column
Nat = Sheet1.Cells.Find(what:="Nation").Column
OH = Sheet1.Cells.Find(what:="occ").Column
TT = Sheet1.Cells.Find(what:="TreatmentType").Column
Re = Sheet1.Cells.Find(what:="Result").Column
Inj = Sheet1.Cells.Find(what:="InjuryOrNot").Column
IL = Sheet1.Cells.Find(what:="InjuryLocation").Column
DS = Sheet1.Cells.Find(what:="DiseaseSystem").Column
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You'd get that error if your search term isn't found, you can handle it like this

Code:
Dim rVD As Range
Set rVD = Sheet1.Cells.Find(what:="VisitDay")
If Not rVD Is Nothing Then VD = rVD.Column
 
Last edited:
Upvote 0
Hi Sericom,
Thanks for your help.
After testing your code, I still face the same condition... but a thought suddenly came to me , I have a Hidden label,
so I need to unhidden my row to make cells.find happen....
Here are the updating code( along with your idea, can work together after unhidden the row)

Code:
Dim rVD, rVC, rPtS, rSx, rVt, rPtN, rAge, rNat, rOH, rTT, rRe, rInj, rIL, rDS As Range
'Set rVD = Sheet1.Cells.Find(what:="VisitDay")
'If Not rVD Is Nothing Then VD = rVD.Column
'Set rVD = Sheet1.Cells.Find(what:="VisitCase")
'If Not rVD Is Nothing Then VC = rVC.Column
'Set rVD = Sheet1.Cells.Find(what:="PtState")
'If Not rVD Is Nothing Then PtS = rPtS.Column
'Set rVD = Sheet1.Cells.Find(what:="Sex")
'If Not rVD Is Nothing Then Sx = rSx.Column
'Set rVD = Sheet1.Cells.Find(what:="VisitTime")
'If Not rVD Is Nothing Then Vt = rVt.Column
'Set rVD = Sheet1.Cells.Find(what:="PtName")
'If Not rVD Is Nothing Then PtN = rPtN.Column
'Set rVD = Sheet1.Cells.Find(what:="Age")
'If Not rVD Is Nothing Then Age = rAge.Column
'Set rVD = Sheet1.Cells.Find(what:="Nation")
'If Not rVD Is Nothing Then Nat = rNat.Column
'Set rVD = Sheet1.Cells.Find(what:="occ")
'If Not rVD Is Nothing Then OH = rOH.Column
'Set rVD = Sheet1.Cells.Find(what:="TreatmentType")
'If Not rVD Is Nothing Then TT = rTT.Column
'Set rVD = Sheet1.Cells.Find(what:="Result")
'If Not rVD Is Nothing Then Re = rRe.Column
'Set rVD = Sheet1.Cells.Find(what:="InjuryOrNot")
'If Not rVD Is Nothing Then Inj = rInj.Column
'Set rVD = Sheet1.Cells.Find(what:="InjuryLocation")
'If Not rVD Is Nothing Then IL = rIL.Column
'Set rVD = Sheet1.Cells.Find(what:="DiseaseSystem")
'If Not rVD Is Nothing Then DS = rDS.Column
With Sheet1
.Rows("8:8").Hidden = False
VD = .Cells.Find(what:="VisitDay").Column
VC = .Cells.Find(what:="VisitCase").Column
PtS = .Cells.Find(what:="PtState").Column
Sx = .Cells.Find(what:="Sex").Column
Vt = .Cells.Find(what:="VisitTime").Column
PtN = .Cells.Find(what:="PtName").Column
Age = .Cells.Find(what:="Age").Column
Nat = .Cells.Find(what:="Nation").Column
OH = .Cells.Find(what:="occ").Column
TT = .Cells.Find(what:="TreatmentType").Column
Re = .Cells.Find(what:="Result").Column
Inj = .Cells.Find(what:="InjuryOrNot").Column
IL = .Cells.Find(what:="InjuryLocation").Column
DS = .Cells.Find(what:="DiseaseSystem").Column
.Rows("8:8").Hidden = True
End With
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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