Range Selection error, please help

dhlesg

New Member
Joined
Jul 31, 2011
Messages
14
Hello,
I want to select a (moving) Range of cells so I can change some format on these cells
If my numberofcriteria =2 in this case I want to select Range "I7:K9"
-----------------------------
My code is
Dim numberofcriteria As Integer
numberofcriteria = Range("c7").Value
StartRange = Cells(7, 9)
EndRange = Cells(7, 9).Offset(numberofcriteria, numberofcriteria)
Cells(StartRange, EndRange).Interior.ColorIndex = 19

It got an error mesage when I run the code

-------------------------------

Thanks for your help
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
Dim numberofcriteria As Integer
Dim StartRange As Range

numberofcriteria = Range("c7").Value
Set StartRange = Cells(7, 9)
StartRange.Resize(numberofcriteria, numberofcriteria).Interior.ColorIndex = 19
 
Upvote 0
Were you thinking of something like this?
Code:
Dim numberofcriteria As Integer
numberofcriteria = Range("c7").Value
Cells(7, 9).Resize(numberofcriteria, numberofcriteria).Interior.ColorIndex = 19
Since you didn't declare StartRange and EndRange as variables, might one assume that you weren't interested in them anyway?
 
Upvote 0
Another question.
Now I have square matrix, how do I select the range of the top half of the matrix

Thanks
 
Upvote 0
Another question.
Now I have square matrix, how do I select the range of the top half of the matrix

Thanks
Maybe like this.
Code:
Sub selecttophalf()
Dim matr As Range
Set matr = Range("C10:F13")
matr.Resize(Int(matr.Rows.Count / 2)).Select
End Sub
Assuming your matrix is defined as the ordered elements of a range that is already specified somehow.
 
Upvote 0
Sorry It is a diagonal half
Maybe a bit more complex. But try
Code:
Sub selecttopdiaghalf()
Dim matr As Range, u As Range
Dim i As Long, j As Long
Set matr = Range("C10:F13")
Set u = matr(1, 1)
For i = 1 To matr.Rows.Count
    For j = 1 To i
    Set u = Union(u, matr(j, i))
Next j, i
u.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,245
Members
452,900
Latest member
LisaGo

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