Solved

Formatting Excel Cells using VBA

Posted on 2011-03-06
6
898 Views
Last Modified: 2012-05-11
I want to perform the below formatting controls on Excel cells using VBA,

For a particular Row from Column A to I, i want to apply a specific 'Fill Color'.
Change the name of the sheet
Apply 'Merge & Center' for a particular column.

How could i achieve this using vb.
0
Comment
Question by:srikanthv2322
  • 3
  • 2
6 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35053274
This should help you. I have commented the code so that it is easier to understand :)

'~~> This will color Row 2 in Sheet1 Red
Sheets("Sheet1").Rows(2).Interior.ColorIndex = 3
   
'~~> This will Rename "Sheet1" to "NewName"
Sheets("Sheet1").Name = "NewName"

'~~> This will merge D6 and E6
With Sheets("Sheet1").Range("D6:E6")
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Selection.Merge

Open in new window


Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35053281
Here is one link which you will find helpful. It contains the number for color indexes :)

http://www.mvps.org/dmcritchie/excel/colors.htm

Sid
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35053282
something like this

I suggest you use the macro recoder to get the right colour

Cheers

Dave
Sub Macro1()
    With Range("A10:I10").Interior
        .Pattern = xlSolid
        .Color = vbYellow
        ' .Color = 65535
    End With
    ActiveSheet.Name = "test"
    With Columns("D:D")
        .HorizontalAlignment = xlCenter
        .Merge
    End With
End Sub

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 50

Expert Comment

by:Dave Brett
ID: 35053298
Sid

I'd missed your post  on the refresh.

Two comments though
on part 1) It was Column A to I on the row, not the entire column
on part 3) Your usage of Selection.Merge is inconistent with With Sheets("Sheet1").Range("D6:E6")

Cheers

Dave
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35053310
>>>I'd missed your post  on the refresh.

That's Ok Dave ;)

You are right.

srikanthv2322: Amended Code. I have also introduced a variable for the row. :)

'~~> This will color the Row A to I in Sheet1 Red
Dim Rw As Long
Rw = 1
Sheets("Sheet1").Range("A" & Rw & ":I" & Rw).Interior.ColorIndex = 3
   
'~~> This will Rename "Sheet1" to "NewName"
Sheets("Sheet1").Name = "NewName"

'~~> This will merge D6 and E6
With Sheets("Sheet1").Range("D6:E6")
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    .Merge
End With

Open in new window


Sid
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 35054670
!!.Do.Not.Use.Merged.Cells.!!

Use "Center across Selection" instead and draw cell borders or background fills to make the cells appear as one.

    .Merge ' No! not good practise

    .HorizontalAlignment = xlCenterAcrossSelection ' Yes! Cell integrity stays intact

Merged cells are bad, bad, bad, and cause more trouble than they are worth when you want to analyse and manipulate the worksheet further.

Unless, of course, you want to come back here because your other macros/formatting/copy paste/conditional formatting/sort/filter does not work, because of merged cells.

Just a thought.

cheers, teylyn
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question