Solved

Formatting Excel Cells using VBA

Posted on 2011-03-06
6
902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

734 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