Need VB code to add a formatted line

darkelf26

New Member
Joined
Oct 5, 2006
Messages
37
Hi,

I have a worksheet with 4 tables on it.

Under each table I have several clickbuttons. What I would like is one of those buttons to add a line to the table it is under and format specific cells in it in a specific way.

I can do the scripting for the formatting and adding a line but to not to select the cells within the newly added row so that they can be formatted.

Can anyone help please.
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
54,844
Office Version
  1. 365
Platform
  1. Windows
Welcome to the Board!

Can you identify/select the newly added row?

If not, please explain the logic for how this row is selected.

If so, you can use one of the following methods:

1. If you know which column you are starting in in your new row, you can OFFSET, i.e. IF you start in column "A" and want to put something in column "E", use:
ActiveCell.Offset(0,4)=...

2. If you only know the row, but not the current position, you can use something like:
Cells(ActiveCell.Row,"E")=...
 

darkelf26

New Member
Joined
Oct 5, 2006
Messages
37
The tables are all in the same sheet so the row numbers can change if a row is added to any of the previous tables.

Also the formatting I want to do is to merge 5 cells so would the command be:

Cells(ActiveCell.Row, "D:I").Select
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453
Hello darkelf26, welcome to the board.
. . .the formatting I want to do is to merge 5 cells so would the command be:
Cells(ActiveCell.Row, "D:I").Select
I don't see Joe around so I'll throw in my two cents worth.
First off it is generally felt among 'coders' that "merged cells is the devil" and they are for the
most part avoided if at all possible.

That being said, if you still need to merge them you don't have to select them to do it. (You
very seldom have to actually select an object in your code to work with it.)
To merge the cells in columns D:I of the active row you can use this.
Code:
Range(Cells(ActiveCell.Row, "D"), Cells(ActiveCell.Row, "I")).Merge
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
54,844
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

"merged cells is the devil"
Absolutely! The "Center Across Selection" is usually the much better option, as it gives you the same visual effect without all the problems that merged cells cause.

(Thanks for the pickup HalfAce, I have been busy and have only here intermittenly today).
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453
Yeah, without a doubt Center Across Selection is preferred over actual merging.
I guess you'll have to download ASAP Utilities to get it though.
I thought we had that option in XL '03 but I can't find it now (and certainly can't find it
in XL 2K).
 

Boller

Banned
Joined
Apr 11, 2006
Messages
2,328

ADVERTISEMENT

Yeah, without a doubt Center Across Selection is preferred over actual merging.
I guess you'll have to download ASAP Utilities to get it though.
I thought we had that option in XL '03 but I can't find it now (and certainly can't find it
in XL 2K).

http://support.microsoft.com/kb/212022/en-us
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
54,844
Office Version
  1. 365
Platform
  1. Windows
I didn't even know there was a toolbar button, I always did it the way that Boller referenced. That doesn't require ASAP utilities.
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453
HA! I was pretty sure I'd seen it somewhere!
Just didn't look far enough in the format cells options. - (And we even have it in XL 2K!) Cool.

Thanks Boller. :cool:
 

darkelf26

New Member
Joined
Oct 5, 2006
Messages
37
thanks guys, thecentre across coloumns has solved this issue and one I was having with the fact that you cant merge cells in a shared workbook.
 

Watch MrExcel Video

Forum statistics

Threads
1,114,093
Messages
5,545,917
Members
410,711
Latest member
Josh324
Top