programming style - printing 4 columns instead of 1

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
The code below is to print colorindex code (from 1-56). It works fine but I want to print these codes into 4 columns (each has 14 cells). I know I need to break the For-Next look when x=14
but I do not know how and I would like to learn from your style how that can be done. I am thinking to inset If statement to check if x=14 and to exit the loop accordingly. Thank you so much.

Code:
Sub colorindexall()
    Dim x As Integer
    For x = 1 To 56
    Cells(x, 1).Interior.ColorIndex = x
    Cells(x, 1).Value = x
    Next
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try
Code:
Dim x as Integer, j as Integer
For x = 1 to 14 Step 4
    For j = 1 to 4
        With Cells(x, j)
            .Value = 4 * (X-1) + j
            .Interior.ColorIndex = x
        End With
    Next j
Next x
 
Upvote 0
Thank you for your reply. What I was hoping to see is this:
(with each cell has color match the number)

[TABLE="width: 256"]
<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64, align: right"]1[/TD]
[TD="width: 64, align: right"]15[/TD]
[TD="width: 64, align: right"]29[/TD]
[TD="width: 64, align: right"]43[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"]44[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]3[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]31[/TD]
[TD="align: right"]45[/TD]
[/TR]
[TR]
[TD="class: xl64, align: right"]4[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]32[/TD]
[TD="align: right"]46[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]33[/TD]
[TD="align: right"]47[/TD]
[/TR]
[TR]
[TD="align: right"]6[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]34[/TD]
[TD="align: right"]48[/TD]
[/TR]
[TR]
[TD="align: right"]7[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]35[/TD]
[TD="align: right"]49[/TD]
[/TR]
[TR]
[TD="align: right"]8[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]36[/TD]
[TD="align: right"]50[/TD]
[/TR]
[TR]
[TD="align: right"]9[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]37[/TD]
[TD="align: right"]51[/TD]
[/TR]
[TR]
[TD="align: right"]10[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]38[/TD]
[TD="align: right"]52[/TD]
[/TR]
[TR]
[TD="align: right"]11[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]39[/TD]
[TD="align: right"]53[/TD]
[/TR]
[TR]
[TD="align: right"]12[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]54[/TD]
[/TR]
[TR]
[TD="align: right"]13[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]41[/TD]
[TD="align: right"]55[/TD]
[/TR]
[TR]
[TD="align: right"]14[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]42[/TD]
[TD="align: right"]56[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
How about
Code:
Dim x As Integer, j As Integer
For x = 1 To 4
    For j = 1 To 14
        With Cells(j, x)
            .Value = 14 * (x - 1) + j
            .Interior.ColorIndex = .Value
        End With
    Next j
Next x
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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