Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting Excel Cells using VBA

Posted on 2011-03-06
6
Medium Priority
?
906 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

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.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

722 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