dropkickweasel
Board Regular
- Joined
- Feb 2, 2014
- Messages
- 70
Hi,
Is there a way to select a range of cells, without selecting some of the cells within that range?
I have a 14x14 range of cells (B2:O15) as a named range "Outside1".
Within that is a 12x12 range of cells (C3:N14) as a named range "Inside1".
(There will be several similar ranges named "OutsideN" and "InsideN" once I get the code working properly. Currently I have an Outside2 and an Inside2, but nothing further).
What I want to happen is once N is defined (currently just writing a 1 or a 2 in cell A1, but eventually will be the outcome of an option button on a userform), the cells that are in OutsideN are filled light blue, but the cells that are in InsideN are left untouched.
Initially, I had a code that changed all of OutsideN to light blue, then removed the fill from InsideN.
This isn't an adequate solution as some of the cells in InsideN will have their own colour fill and setting this is also removed with this solution.
My current solution is to copy all of InsideN to a temporary range ("Temp"), change all of OutsideN to light blue, then paste back from Temp to InsideN.
This works fine, but relies on a 12x12 'helper range'.
Here's the code that I am currently using. I am always open to suggestions for better practice and cleaner coding:
Does anyone have a suggestion for a more efficient method of achieving the same outcome?
To think of it another way, how can I change the colour of the cells in the first and last row and column of a range, without affecting the other cells in that range?
Is there a way to select a range of cells, without selecting some of the cells within that range?
I have a 14x14 range of cells (B2:O15) as a named range "Outside1".
Within that is a 12x12 range of cells (C3:N14) as a named range "Inside1".
(There will be several similar ranges named "OutsideN" and "InsideN" once I get the code working properly. Currently I have an Outside2 and an Inside2, but nothing further).
What I want to happen is once N is defined (currently just writing a 1 or a 2 in cell A1, but eventually will be the outcome of an option button on a userform), the cells that are in OutsideN are filled light blue, but the cells that are in InsideN are left untouched.
Initially, I had a code that changed all of OutsideN to light blue, then removed the fill from InsideN.
This isn't an adequate solution as some of the cells in InsideN will have their own colour fill and setting this is also removed with this solution.
My current solution is to copy all of InsideN to a temporary range ("Temp"), change all of OutsideN to light blue, then paste back from Temp to InsideN.
This works fine, but relies on a 12x12 'helper range'.
Here's the code that I am currently using. I am always open to suggestions for better practice and cleaner coding:
Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
i = Sheets("Sheet1").Range("a1")
Sheets("Sheet1").Range("Inside" & i).Select
Selection.Copy
Sheets("Sheet1").Range("Temp").Select
ActiveSheet.Paste
Sheets("Sheet1").Range("Outside" & i).Interior.Color = 16247773
Sheets("Sheet1").Range("Temp").Select
Selection.Copy
Sheets("Sheet1").Range("Inside" & i).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Sheet1").Range("a1").Select
Application.ScreenUpdating = True
End Sub
Does anyone have a suggestion for a more efficient method of achieving the same outcome?
To think of it another way, how can I change the colour of the cells in the first and last row and column of a range, without affecting the other cells in that range?