Creating Border on Offset Range (Static)

stoutbn

Board Regular
Joined
Aug 3, 2016
Messages
52
Hi, I've been searching all day for answers to how I can pull this off. I have two tables of data where records match up when the tables are placed side by side. I have a code that currently highlights the entire row of the sheet for which cell I have active. Works great. But I also want to have the cell that is offset 13 cells to the right of my active cell to have a border around it. I believe the code needs to be declared as static because when I move to a new cell I want the border to disappear and reappear in the new active cell's relative 13 cells to the right. Here is my code below. I get an error saying "object required" when I try to run. PLEASE help. Thanks.



Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rr
Static cc
Static qq


If cc <> "" Then
With Columns(cc).Interior
.ColorIndex = xlNone
End With
With Rows(rr).Interior
.ColorIndex = xlNone
End With
With qq.Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone
End With

End If

r = Selection.Row
c = Selection.Column
rr = r
cc = c
q = ActiveCell.Offset(0, 13)
qq = q

With Rows(r).Interior
.ColorIndex = 35
.Pattern = xlAutomatic


With ActiveCell.Offset(0, 13).BorderAround(xlDouble)

End With
End With
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hello stoutbn,

This works...

Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)


    Dim n As Integer
    
    Static LastCell As Range


        If Target.Cells.Count > 1 Then Exit Sub
        
        Application.EnableEvents = False
        
            If Not LastCell Is Nothing Then
                LastCell.EntireRow.Interior.ColorIndex = xlNone
                For n = 7 To 11
                    LastCell.Borders(n).LineStyle = xlLineStyleNone
                Next n
            End If
        
            With Target
                .EntireRow.Interior.ColorIndex = 35
                .Offset(0, 13).BorderAround LineStyle:=xlDouble
            End With
        
            Set LastCell = Target.Offset(0, 13)
        
        Application.EnableEvents = True


End Sub
 
Upvote 0
Leith,

Thank you for the code! But I was unable to get this to work. I copy/pasted the code into VBA but nothing happened when I clicked from cell to cell.
 
Upvote 0
Leith,

Thank you for the code! But I was unable to get this to work. I copy/pasted the code into VBA but nothing happened when I clicked from cell to cell.
Leith posted event code (so it would work automatically when you double-click a cell), not a macro (which requires a human to run it). Here are instruction on how to implement it...

HOW TO INSTALL Event Code
------------------------------------
If you are new to event code procedures, they are easy to install. To install it, right-click the name tab at the bottom of the worksheet that is to have the functionality to be provided by the event code and select "View Code" from the popup menu that appears. This will open up the code window for that worksheet. Copy/Paste the event code into that code window. That's it... the code will now operate automatically when its particular event procedure is raised by an action you take on the worksheet itself. Note... if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

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