Looking for help with Intersect VBA

CrashDDL

Board Regular
Joined
Oct 17, 2016
Messages
66
Hi,

I have this code that I want to use to set every 5th row bottom border medium with. I managed to use it on its own in a test file but when I insert this into my main document, I keep getting: "Run-time error '91: Object variable or With block variable not set"

The first line works and will make the 6th row bottom edge medium at the proper ranges, which in turn means that tableRng is not the problem;
The 2nd row however gives the run-time error, suggesting that (i) is someone responsible. I also tried changing (i) to other letters not used anywhere in the VBA with no luck.
Anyone have any ideas why this might be?

Code:
    Dim i As Long
    
    For i = 2 To 47 Step 5


        'Intersect(Rows(6), tableRng).Borders(xlEdgeBottom).Weight = xlMedium
        Intersect(Rows(i), tableRng).Borders(xlEdgeBottom).Weight = xlMedium
        
    Next i


Thank you for looking! :)
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,181
Office Version
  1. 365
Platform
  1. Windows
Obviously, row 2 does not have any cell that is withing tableRng. When using Intersect() it is best to Set it to a range variable and then check if Not Nothing. I can show you how if you really need that.
 
Upvote 0

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
This worked for me:-
Code:
Dim i As Long
  Dim tablerng As Range
   Set tablerng = Range("A1:c47") [B][COLOR=#008000]' Perhaps some problem with the range size and loop size 47[/COLOR][/B]
    For i = 2 To 47 Step 5
         Intersect(Rows(i), tablerng).Borders(xlEdgeBottom).Weight = xlMedium
 
Upvote 0

CrashDDL

Board Regular
Joined
Oct 17, 2016
Messages
66
Obviously, row 2 does not have any cell that is withing tableRng. When using Intersect() it is best to Set it to a range variable and then check if Not Nothing. I can show you how if you really need that.

Of course it doesn't... I can't believe I missed that :D

Thank you for the help!
 
Upvote 0

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,181
Office Version
  1. 365
Platform
  1. Windows
Sounds like you got it now. For others that might not, here I skipped row 2 on purpose...
Code:
Sub Main()
  Dim i As Long, tableRng, r As Range
    Set tableRng = [A3:A100]
    For i = 2 To 47 Step 5
      Set r = Intersect(Rows(i), tableRng)
      If Not r Is Nothing Then r.Borders(xlEdgeBottom).Weight = xlMedium
    Next i
End Sub
 
Upvote 0

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
How about something like

Code:
With tableRange
    For i = 1 to .Rows.Count Step 5
        .Rows(i).Borders(xlEdgeBottom).Weight = xlMedium
    Next i
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,191,120
Messages
5,984,762
Members
439,909
Latest member
daigoku

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
Top