Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Apply cell formatting using VBA

I am producing CSV files (in a seprerate app) and importing these into
Excel then saving them out as xls files - that all works well.

I have a need to format the data to make it look better and I would like to
auttomate this using VBA.

I will want to set column widths for my columns (mostly less than 10) and
apply formatting to the columns containing numbers.

I also need to center and bold various header rows that occur throughout
and bold some individual columns.

There are not usually more that 100 rows so speed concerns probably do not
apply here.

Although there are a number of different formats of sheets (requiring
different groups of the formatting mentioned above) - I will be able
identify what these sheets are based on the identification of a single cell
in the topr right (or somewhere).

I have done a fair amount of messing with actual data values ion cells
using VBA code like this;

'For Each lCell In Cells.SpecialCells(xlTextValues)
For Each lCell In Range("A2:N100").SpecialCells(xlTextValues)
lCell.Formula = StrConv(lCell.Formula, vbProperCase)
Next
Next lws


but I have not done any formatting of attributes like bold and number
formatting etc.

I am not sure how to bold and center a whole row in code and how that would
fit with other code that I am using too format numeric formats etc.

I am NOT in any way asking someone to do all this for me, I am happy to put
the work in if anyone can give me a few pointers and start me on the way -
I can always come back if I get stuck !

Any help appreciated.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Apply cell formatting using VBA

One thing that I usually do is record a macro where I apply all of the formatting I want and then adapt that macro to take a range argument. For example, I turned on the macro recorder and then applied a bold font, centered the text, and used autofit to adjust the column size (all without changing the selected cells). After a few tweaks, I changed the recorded macro to take a range argument and called it FormatHeader. Now, I can feed the macro any range using the syntax:

FormatHeader Range("A1:H1")

You could record similar macros for any other types of formatting that you need. Then it becomes a simple matter of feeding the correct range to the macros.

One note of caution, even though you do not have a lot of rows of data, I'd be careful about applying formatting to an entire row or column as it can cause performance issues. Rather than using

FormatHeader Rows(1)

you might try

FormatHeader Range(Cells(1, 1), Cells(1, Sheet1.UsedRange.Columns.Count))

Here is the code I mentioned above:

Sub FormatHeader(rHeaders As Range)

With rHeaders
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.EntireColumn.AutoFit
End With

End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Apply cell formatting using VBA

On 19 Dec 2012 14:17:01 GMT, Isis wrote:

I am producing CSV files (in a seprerate app) and importing these into
Excel then saving them out as xls files - that all works well.

I have a need to format the data to make it look better and I would like to
auttomate this using VBA.

I will want to set column widths for my columns (mostly less than 10) and
apply formatting to the columns containing numbers.

I also need to center and bold various header rows that occur throughout
and bold some individual columns.

There are not usually more that 100 rows so speed concerns probably do not
apply here.

Although there are a number of different formats of sheets (requiring
different groups of the formatting mentioned above) - I will be able
identify what these sheets are based on the identification of a single cell
in the topr right (or somewhere).

I have done a fair amount of messing with actual data values ion cells
using VBA code like this;

'For Each lCell In Cells.SpecialCells(xlTextValues)
For Each lCell In Range("A2:N100").SpecialCells(xlTextValues)
lCell.Formula = StrConv(lCell.Formula, vbProperCase)
Next
Next lws


but I have not done any formatting of attributes like bold and number
formatting etc.

I am not sure how to bold and center a whole row in code and how that would
fit with other code that I am using too format numeric formats etc.

I am NOT in any way asking someone to do all this for me, I am happy to put
the work in if anyone can give me a few pointers and start me on the way -
I can always come back if I get stuck !

Any help appreciated.

Thanks


Column widths and various attributes can be set on ranges.
Column width

example:

=========================
Option Explicit
Sub foo()
Dim r As Range, c As Range
Set r = Sheet1.UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each c In r
c.Value = StrConv(c.Text, vbProperCase)
Next c
With r
.Font.Bold = True
.EntireColumn.AutoFit
End With

End Sub
=========================
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apply Conditional formatting if cell contains formula Haritha Kolla Excel Programming 6 August 3rd 09 06:43 AM
Conditional Formatting -- Apply to Row if Cell Value does not Matc SteveC Excel Programming 2 June 21st 06 06:17 PM
Why won't excel apply my custom cell formatting? Scott Setting up and Configuration of Excel 1 May 30th 06 07:44 PM
formatting won't apply to my cell with a formula Adrienne Excel Worksheet Functions 9 November 27th 05 05:44 PM
cell formatting - apply borders David Excel Programming 4 June 6th 05 08:01 AM


All times are GMT +1. The time now is 05:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"