Creating a range from several ranges in excel vba - vba

I have a worksheet with a matrix, which is divided diagonally with a cell that is black. The matrix contains the same header, both vertically and horisontally.
The idea is that you show the relation between the items in the bottom area, below the diagonal.
I want to create a Range (MatrixRange) that is the bottom part of the area.
What I'm trying to do is create a range for each column that I use, and add it to the MatrixRange. The different ranges im trying to combine is therefore of different height.
Dim MatrixRange As Range
Private Sub Workbook_Open()
Dim n As Integer
Dim NextRange As Range
Dim OldRange As Range
Dim ws As Worksheet
Set ws = Sheets("Systemmatrise")
With ws
For n = 1 To 68
Set OldRange = MatrixRange
Set NextRange = .Range(Range("B9").Offset(n + 1, n), Cells(78, n + 2))
Set MatrixRange = Union(OldRange, NextRange)
Next n
End With
Debug.Print MatrixRange
End Sub
I get a "Runtime error 5" after
Set MatrixRange = Union(OldRange, NextRange)
Is there an easier way to create this Range, or somehow to fix this problem?
Thanks in advance

You don't seem to set an initial range for OldRange, so it'll be Nothing during the first iteration - the likely cause of your error.
Delete this line:
Set OldRange = MatrixRange
Correct this line to include a dot in front of Cells:
Set NextRange = .Range(Range("B9").Offset(n + 1, n), .Cells(78, n + 2))
and before the For Loop, add this (extra dot noted, thanks Jeeped):
Set OldRange = .Range(.Range("B9").Offset(1,0), .Cells(78, 2))

Related

Looping CountColour

Some simple code below. I think I'm having trouble with referencing and looping within a loop.
I'd like to count the number of green cells in a row,then move to the next row. I'm getting an error here:
If RowRange.DisplayFormat.Interior.colour = SourceColour.DisplayFormat.Interior.colour Then
Run-time error 91 - object not set....
Any ideas?
Full code:
Sub countcolourloop1()
Dim rng As Range
Dim RowRange As Range
Dim SourceColour As Range
Dim xBackColour As Integer
'count green cells
Set SourceColour = ActiveWorkbook.ActiveSheet.Range("BN2:BN2")
'cyle through each row, add 1 to the counter xBackColour every time you find a cell in the range matching the SourceColour
For I = 4 To 300
Set RowRange = ActiveWorkbook.ActiveSheet.Range("B" & I & ":BK" & I)
If RowRange.DisplayFormat.Interior.colour = SourceColour.DisplayFormat.Interior.colour Then
xBackColour = xBackColour + 1
End If
Next
End Sub

Set a range to an entire column with index number

I'm building a function that takes a column number as an input to return the highest value in the column.
I need to work with an entire column with a column index number. The following code works but I need an index number instead of using "B:B" to reference the column.
Set rng = Range("B:B")
When I use the following code, everything seems to break down. It always returns a 0 even though the highest number is 44. Can you see what I am doing wrong? It worked perfectly when I used "B:B" as a range.
Set rng = Range(Columns(2),Columns(2))
It doesn't seem to recognize anything in the range. Any help would be awesome!
Try this
Sub Sample()
Dim Rng As Range
'~~> This will let you work with Col B
Set Rng = Columns(2)
'~~> This will give you the Max in that column
MsgBox Application.WorksheetFunction.Max(Rng)
End Sub
This is from a solution I found a while back:
Dim x, y As Int
Dim rng As Range
x = 1 ' For A
y = 4 ' For D
Set rng = Columns(Chr(64 + x) & ":" & Chr(64 + y))
Source: http://www.mrexcel.com/forum/excel-questions/114025-visual-basic-applications-select-multiple-columns-using-column-number.html

EXCEL-VBA Change a range with a loop

Set rng = Worksheets("Sheet1").Range("B2")
I have the above and would like to run a loop that changes the range I use/evaluate. I have tried variations of the following.
for x = 2 to 10
Set rng = Worksheets("No-Funding").Range(x & "2")
Next X
I have done some investigating and found this other Stack Overflow: Excel VBA Looping formula to change Range I can not make sense of it in this situation though.
My code will not work if it uses cells either, I have tried that and can only make it work with Range. Thanks for any help provided!
For a strictly numerical increment try,
Set rng = Worksheets("No-Funding").Cells(x, 2)
An xlA1 style reference can be achieved by factoring in the ASCII character.
Set rng = Worksheets("No-Funding").Range(Chr(64 + x) & 2)
Try this:
For x = 2 To 10
Set rng = Worksheets("No-Funding").Cells(2, x)
Next x
As far as I know it is no matter for VBA if you use function .Range or .Cells, since both of them return object or Range type.
Use
Set rng = Worksheets("Sheet1").Range("B2").Offset(0,x)
to move to a different column. Better yet
Dim values as Variant, rng as Range
Set rng = Worksheets("Sheet1").Range("B2").Resize(10,1)
values = rng.Values2
For i=1 to 10
values(i,1) = 2*values(i,1)
Next i
rng.Values2 = values

error in code for colouring of a chart

I wanted to use this code
Sub PieMarkers()
Dim chtMarker As Chart
Dim chtMain As Chart
Dim intPoint As Integer
Dim rngRow As Range
Dim lngPointIndex As Long
Dim x As Long
Dim myTheme As String
Application.ScreenUpdating = False
Set chtMarker = ActiveSheet.ChartObjects("chtMarker").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set chtMain = ActiveSheet.ChartObjects("chtMain").Chart
Set rngRow = Range(ThisWorkbook.Names("PieChartValues").RefersTo)
For Each rngRow In Range("PieChartValues").Rows
chtMarker.SeriesCollection(1).Values = rngRow
SetColorScheme chtMarker, x
chtMarker.Parent.CopyPicture xlScreen, xlPicture
lngPointIndex = lngPointIndex + 1
chtMain.SeriesCollection(1).Points(lngPointIndex).Paste
x=x+1
Debug.Print rngColors.address()
Next
lngPointIndex = 0
Application.ScreenUpdating = True
End Sub
Sub SetColorScheme(cht As Chart, i As Long)
Dim y_off As Long, rngColors As Range
Dim x As Long
y_off = i Mod 13
'this is the range of cells which has the colors you want to apply
Set rngColors = ThisWorkbook.Sheets("Basic").Range(ThisWorkbook.Sheets("Basic").Range("A19").Value).Offset(y_off, 0)
With cht.SeriesCollection(1)
'loop though the points and apply the corresponding fill color from the cell
For x = 1 To .Points.Count
.Points(x).Format.Fill.ForeColor.RGB = _
rngColors.Cells(x).Interior.Color
Next x
End With
End Sub
to colour several pie charts with all of them having the same amount of slices (3 each, 8 pie charts) according to specified colours in the workbook (colours used as background colour for a cell in a worksheet).This is the Sub Colour Scheme.
The code compiles without error the problem is just that it only uses the first to specified colours in a range (say A10:Z10, only the colours in A10 and B10 to colour all pieces of the 8 pie charts (24 sclices in total with the two colours from A10 and B10). Could somebody tell me what I would need to change so that the whole colour range from A10 to X10 is used (24 different colours) for the different slices?
It seems the For loop that use cht.SeriesCollection(1).Points.Count as a boundary doesn't take you beyond two iterations.
You should rather use an inner loop specific to the range of cells you want to retrieve the color from and a if condition statement if there are less colors.

reading a range value from a cell

in the following code
Sub SetColorScheme(cht As Chart, i As Long)
Dim y_off As Long, rngColors As Range
Dim x As Long
y_off = i Mod 10
'this is the range of cells which has the colors you want to apply
Set rngColors = ThisWorkbook.Sheets("colors").Range("A1:C1").Offset(y_off, 0)
With cht.SeriesCollection(1)
'loop though the points and apply the
'corresponding fill color from the cell
For x = 1 To .Points.Count
.Points(x).Format.Fill.ForeColor.RGB = _
rngColors.Cells(x).Interior.Color
Next x
End With
End Sub
the range from which the data are read is in th emoment stated in the code. Is there a chance that it is read from asheet in the worksheet? So that a person can enter A1:C1 and it will place it the way it is in the code in the moment?
I'm not sure how you want to handle the user's input, but of course the range can be an incoming variable. I have it below as a string but elegance would be the range object. Sorry if this is too simple, I'm not sure your question.
Sub SetColorScheme(UserRange As String, cht As Chart, i As Long)
...
'this is the range of cells which has the colors you want to apply
Set rngColors = ThisWorkbook.Sheets("colors").Range(UserRange).Offset(y_off, 0)
...
End Sub
If the user enters "A1:C1" in cell D1 then you can make use of this range with:
Set rngColors = ThisWorkbook.Sheets("colors").Range(Range("D1").Value).Offset(y_off, 0)
' but you should refer to the w/sheet as well
Set rngColors = ThisWorkbook.Sheets("colors") _
.Range(ThisWorkbook.Sheets("colors").Range("D1").Value).Offset(y_off, 0)
Range("D1").Value obtains the text "A1:C1" which is then used to identify this Range.