Solved

Formatting Excel Cells using VBA

Posted on 2011-03-06
6
890 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 50

Expert Comment

by:Dave Brett
Comment Utility
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
Comment Utility
>>>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:teylyn
Comment Utility
!!.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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now