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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try

With Range(theData.Columns(2), theData.Columns(theData.Columns.Count-1))
 
Upvote 0

Forum statistics

Threads
1,217,051
Messages
6,134,298
Members
449,864
Latest member
daffyduck1970

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