Multiple Column Reference

lee2smooth03

New Member
Joined
Nov 13, 2011
Messages
20
Hello everyone,

I need some help referencing columns in a range. Below is a sample of the code. In the code, the first column of the range is given a numerical format and some color; likewise, the last column is given a different color and format. My goal is to target the cells in the middle columns.

Code:
Function formatData(theData As Range)
'this function colors the first and last columns of the range
'it also sets the numerical format for the dates as well as centers all of the numbers

With theData.Columns(1)                     'targets the first column
    .Interior.Color = RGB(83, 141, 213)
    .NumberFormat = "mm/dd/yyyy"
End With


With theData.Columns(theData.Columns.Count)  'targets the last column
    .Interior.Color = RGB(255, 255, 102)
    .NumberFormat = "00"
End With

End Function

I want the remaining center columns to have similar formats, but I do not know how to address all of them at once using the "with" statement. Here's what I had in mind:
  • With theData.Columns(2:theData.Columns.Count - 1)
  • With theData.Columns(2 to theData.Columns.Count -1)

Does anyone have any suggestions?

P.S.
Does anyone know how to center-align the cells using with?
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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