简体   繁体   中英

VBA merge and unmerge according to if statement

Example Image 在此处输入图片说明

I am having an issue with merging data in excel using VBA.

Basically, I would like every cell row to be the same size and when a cell matches with a specific name, I want certain cells (specific 2 sets of rows preceding the name) to merge and center while others just merge across but that way some cells in the column have one number while others have a percentage and fraction as seen in this picture example above.

I'm wondering if offset could be useful here as well?

Here is some pseudo ish code to give you an idea: basically, if a cell in the A range matches a color (or whatever I put), then we want to merge and center two rows of cells and the 3 columns. Else if the cell in the A range matches a name (or whatever I put), then leave the cells separate but merge across maybe.

sub example1()

if (cell in A range) = "red", "orange", "yellow", "green", "blue",
then offset ( , 1) and merge&center 
else if (cell in A range) = "sarah", "mark", "bob", "steve", "Brittany",
then offset (, 1) and leavealone or merge just across

end if

end sub

Like Scott Holtzman said more specific question and any example will be great to better understanding... However base on what we have:

I'm thinking I need to first merge and center every 2 cells, then need an if statement saying if it highlights a cell with " " whatever name that needs to be split, unmerge the cells, else leave them merged and cantered.

Doing something and the next undoing this in the same action is probably not good idea... Better is if you check conditions before and then you perform only one specific action.

I assume that you use some kind of loop ? So, do you can check your 'if/else' condition first and then perform merge or split ?

Really, example will be very helpful here.

I'm wondering if offset could be useful here as well?

I can't imagine for what? If I understand your question, you need just `if / else if / else' condition in your VBA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM