简体   繁体   English

VBA根据if语句合并和取消合并

[英]VBA merge and unmerge according to if statement

Example Image 范例图片 在此处输入图片说明

I am having an issue with merging data in excel using VBA. 我在使用VBA在excel中合并数据时遇到问题。

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. 这是一些伪代码,可以给您一个概念:基本上,如果A范围内的单元格与颜色(或我输入的任何颜色)匹配,则我们要合并两行单元格和3列并居中。 Else if the cell in the A range matches a name (or whatever I put), then leave the cells separate but merge across maybe. 否则,如果A范围中的单元格与名称(或我输入的名称)匹配,则将这些单元格分开,但可能会合并在一起。

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: 就像斯科特·霍尔茨曼(Scott Holtzman)所说的那样,更具体的问题和任何示例都将有助于更好地理解...但是,基于我们的知识:

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. 我在想我需要首先合并和居中每2个单元格,然后需要一个if语句,说它是否突出显示带有“”的单元格(无论需要拆分的名称),取消合并这些单元格,否则使它们合并并分散。

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 ? 因此,您是否可以先检查'if / else'条件,然后执行合并或拆分?

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. 如果我理解您的问题,则只需在VBA中提供“ if / else if / else”条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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