Need help code is not performing

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000</SPAN></SPAN>

Hi,</SPAN></SPAN>

I want to colour C:P columns as per values are set in the column R:Z, the macro were written by MickG, I been able to modified it get it work to colour row 6 only, I am not able to set all range from R6:Z19. After I run code it get stuck and highlight the line below </SPAN></SPAN>

"With Cells(r1 + r, 3 + c).Resize(, Dn.Value)"</SPAN></SPAN>
Code:
Sub MG16Sep23()
Dim Rng As Range, Dn As Range, r As Long, r1 As Long, c As Long, num As Long, Col As Variant
Set Rng = Range(Range("R6"), Range("R" & Columns.Count).End(xlToRight))
Col = Array(10, 3)
r1 = 6
r = 0
c = 0
For Each Dn In Rng
    With Cells(r1 + r, 3 + c).Resize(, Dn.Value)
        .Interior.ColorIndex = Col(num)
        .Font.ColorIndex = 2
    End With
    c = c + Dn.Value
    If c = 14 Then c = 0 & r1 = r1 + 1
    num = num + 1
    num = IIf(num = 2, 0, num)
Next Dn

End Sub

Please need help to make it work or please rewrite new one </SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1
2
3
4
5C1C2C3C4C5C6C7C8C9C10C11C12C13C14v1v2v3v4v5v6v7v8v9
6X1212XX1121X22122234
71X2221XX22XX115522
81X11XX22XX11XX2624
91X1221X11X122123261
10X1X1X11X111XX1121351
11XXX1111X1X212X35321
122X1112XXX11121114341
13X1XX1111111X111382
14211X211X2X222X14441
15X112X12111111X13127
161212X21X12X1112222213
172XX1221112111X12344
18111121112121XX5423
191212111221211X22523
20
21
Sheet8


Thank you in advance</SPAN></SPAN>

Regards,</SPAN>
Kishan</SPAN></SPAN>
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Here's one way:

Code:
Dim Rng As Range
Dim lFirstRow As Long, lNoRows As Long, lMaxCols As Long, Col1 As Long, Col2 As Long
Dim col As Long, r As Long, c As Long

lFirstRow = 6
lMaxCols = 14
lNoRows = Range("R" & Rows.Count).End(xlUp).Row - lFirstRow + 1
Set Rng = Range("R" & lFirstRow).Resize(lNoRows, lMaxCols)

With Range("C" & lFirstRow).Resize(lNoRows, lMaxCols)
    .Font.ColorIndex = 2
    .Interior.ColorIndex = xlNone
    For r = 1 To lNoRows
        With .Rows(r)
            Col1 = 1
            col = 10
            For c = 1 To lMaxCols
                Col2 = Application.Min(lMaxCols + 1, Col1 + Rng(r, c).Value)
                If Col1 = Col2 Then Exit For 'Assume we have run out of values if we encounter 0 or blank
                .Cells(Col1).Resize(, Col2 - Col1).Interior.ColorIndex = col
                If Col2 = lMaxCols + 1 Then Exit For
                Col1 = Col2
                col = IIf(col = 10, 3, 10)
            Next c
        End With
    Next r
End With
 
Upvote 0
Here's one way:

Code:
Dim Rng As Range
Dim lFirstRow As Long, lNoRows As Long, lMaxCols As Long, Col1 As Long, Col2 As Long
Dim col As Long, r As Long, c As Long

lFirstRow = 6
lMaxCols = 14
lNoRows = Range("R" & Rows.Count).End(xlUp).Row - lFirstRow + 1
Set Rng = Range("R" & lFirstRow).Resize(lNoRows, lMaxCols)

With Range("C" & lFirstRow).Resize(lNoRows, lMaxCols)
    .Font.ColorIndex = 2
    .Interior.ColorIndex = xlNone
    For r = 1 To lNoRows
        With .Rows(r)
            Col1 = 1
            col = 10
            For c = 1 To lMaxCols
                Col2 = Application.Min(lMaxCols + 1, Col1 + Rng(r, c).Value)
                If Col1 = Col2 Then Exit For 'Assume we have run out of values if we encounter 0 or blank
                .Cells(Col1).Resize(, Col2 - Col1).Interior.ColorIndex = col
                If Col2 = lMaxCols + 1 Then Exit For
                Col1 = Col2
                col = IIf(col = 10, 3, 10)
            Next c
        End With
    Next r
End With
Amazing!! StephenCrump, thank you very much for rewriting the fully new macro, it is working like magic.</SPAN></SPAN>

I do appreciate your kind help and giving your precious time on it.
</SPAN></SPAN>

Have a beautiful weekend
</SPAN></SPAN>

Good Luck
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Another option:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Aug04
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] RngAc [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("C6"), Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Num = 0
    [COLOR="Navy"]Set[/COLOR] RngAc = Range(Dn.Offset(, 15), Cells(Dn.Row, Columns.Count).End(xlToLeft))
     [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] RngAc
          Dn.Offset(, Num).Resize(, R).Interior.Color = R.Interior.Color
         Num = Num + R
    [COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Another option:-
Code:
[COLOR=navy]Sub[/COLOR] MG03Aug04
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, Num [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] RngAc [COLOR=navy]As[/COLOR] Range, R [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Range(Range("C6"), Range("C" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    Num = 0
    [COLOR=navy]Set[/COLOR] RngAc = Range(Dn.Offset(, 15), Cells(Dn.Row, Columns.Count).End(xlToLeft))
     [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] R [COLOR=navy]In[/COLOR] RngAc
          Dn.Offset(, Num).Resize(, R).Interior.Color = R.Interior.Color
         Num = Num + R
    [COLOR=navy]Next[/COLOR] R
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Wow!! Mick, very nice it fills the colours as per range R:Z columns colours, I liked the colouring trick.</SPAN></SPAN>

Code:
Added line to get font colour below the .Interior.Color</SPAN></SPAN>
Dn.Offset(, Num).Resize(, R).Font.Color = R.Font.Color
</SPAN></SPAN>

I do appreciate your kind help</SPAN></SPAN>

Have a beautiful weekend
</SPAN></SPAN>

Good Luck
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan
:)
</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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