ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   looping code (https://www.excelbanter.com/excel-programming/422893-looping-code.html)

Ksenija

looping code
 
hi!

I have this code and I want to have it inside a loop. Am I supposed to
somhow empty MyRange and MyRange1 for every loop??

Sheets("Blad3").Select
Columns(1).Select

Dim MyRange As Range, MyRange1 As Range
Set MyRange = ActiveSheet.UsedRange
For Each c In MyRange
If c.Interior.ColorIndex = 40 Then
If c.Interior.Pattern = xlSolid Then
If MyRange1 Is Nothing Then
Set MyRange1 = c
Else
Set MyRange1 = Union(MyRange1, c)
End If
End If
End If
Next
average_färg = WorksheetFunction.Average(MyRange1)
maxvalue_färg = WorksheetFunction.max(MyRange1)
medianvalue_färg = WorksheetFunction.Median(MyRange1)



Mike H

looping code
 
Hi,

What is it you want to loop through? It currently loops through every cell
in the active sheet used range and checks the colour. What do you want it to
do?

Columns(1).Select

The above line is erroneous, it adds nothing to what is going on.



Mike

"Ksenija" wrote:

hi!

I have this code and I want to have it inside a loop. Am I supposed to
somhow empty MyRange and MyRange1 for every loop??

Sheets("Blad3").Select
Columns(1).Select

Dim MyRange As Range, MyRange1 As Range
Set MyRange = ActiveSheet.UsedRange
For Each c In MyRange
If c.Interior.ColorIndex = 40 Then
If c.Interior.Pattern = xlSolid Then
If MyRange1 Is Nothing Then
Set MyRange1 = c
Else
Set MyRange1 = Union(MyRange1, c)
End If
End If
End If
Next
average_färg = WorksheetFunction.Average(MyRange1)
maxvalue_färg = WorksheetFunction.max(MyRange1)
medianvalue_färg = WorksheetFunction.Median(MyRange1)




All times are GMT +1. The time now is 06:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com