简体   繁体   English

Excel VBA合并/组合具有相同名称的列

[英]Excel VBA merge/combine columns with same name

My project work has an issue similar to the one described below. 我的项目工作存在类似于下面描述的问题。

My dataset is like this 我的数据集是这样的 在此输入图像描述

What I want is like this (LAST COLUMN) 我想要的是这样的(最后一栏) 在此输入图像描述

What I have is many columns of same name like "Is_paid", "Job". 我所拥有的是许多同名的列,如“Is_paid”,“Job”。 What I want is to create a new column "Tot", Which combines all these "Is_Paid" and "Job" in a special manner like, 我想要的是创建一个新的列“Tot”,它以特殊的方式组合所有这些“Is_Paid”和“Job”,如,

  1. Combine all "Is_Paid" column into "Is_Paid_total" 将所有“Is_Paid”列合并为“Is_Paid_total”
  2. Combine all "Job" column into Job_total 将所有“作业”列合并到Job_total中

  3. And the code format is (Not correct) 代码格式是(不正确)

     Private Sub CommandButton1_Click() Dim MyWorksheetLastColumn As Byte MyWorksheetLastColumn = Worksheets(1).Cells(1, Columns.Count).End(xlToLeft).Column Worksheets(1).Cells(1, MyWorksheetLastColumn + 1).Value = "Tot_Employment" Dim rngTemp As Range Set rngTemp=Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious) With rngTemp For Each cel In Range(Cells(1, 1), rngTemp) '****************MERGING STEPS********************** If cel.column(Is_Paid_total)="NonPaid" then Tot=Is_Paid else Tot=Job_total 

End If next Cel End With End Sub 结束如果下一个Cel End With End Sub

Step 3 will be in a for loop 第3步将处于for循环中

I dont know how to merge/combine to get Is_Paid_Total and Job_Total. 我不知道如何合并/组合以获得Is_Paid_Total和Job_Total。 Also I know the if statement I have written is wrong. 我也知道我写的if语句是错误的。 Please help me to tackle this problem. 请帮我解决这个问题。

I imagine that someone can give you a better answer, however the best way I can think of to meet the criteria in your last comment is to set the data up as a table like so: 我想有人可以给你一个更好的答案,但是我能想到的最好的方法是满足你上一条评论的标准是将数据设置为如下所示的表格: 在此输入图像描述

The formula for the "Total" column is: “总计”列的公式为:

=IF(OR([@[is_paid]]="NonPaid",[@[is_paid2]]="NonPaid",[@[is_paid3]]="NonPaid"),"NonPaid",[@Job]&[@Job2]&[@Job3])

i have a different formula but the idea is almost the same: 我有一个不同的公式,但这个想法几乎是一样的:

your last column should have this formula 你的上一栏应该有这个公式

IF(ISERROR(MATCH("NonPaid",$A2:$G2,0)),OFFSET(INDIRECT(ADDRESS(ROW($D2),MATCH("Paid",$A2:$G2,0))),0,1),"NonPaid")

you need to adapt it to meet your columns, in my exampl my last column with data is G so that formula is on column H. 你需要调整它以满足你的列,在我的例子中,我的最后一列数据是G,所以公式在列H上。

i think you can also use it in vba using the range().formula = "=......" and then using the filldown for all your range 我认为你也可以使用范围()。formula =“= ......”在vba中使用它,然后使用所有范围的填充

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

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