简体   繁体   English

Excel-如果数据存在,将多列合并为一

[英]Excel - Combine Multiple Columns into One if Data Exists

I have multiple lists that are in separate columns in excel however some columns do not include data. 我在excel的单独列中有多个列表,但是某些列不包含数据。 What I need to do is combine these columns that do have data into one big column. 我需要做的是将确实有数据的这些列合并为一个大列。

Row 1 | A B
Row 2 | C D E
Row 3 | F 
Row 4 | G H I

Should combine into: 应合并为:

A
B
C
D
E
F
G
H 
I

With no blank rows in between. 中间没有空白行。 Would rather not use macros if possible. 如果可能,宁愿不使用宏。

Thanks! 谢谢!

If you don't want to use Macros, you will at least need to use filter to avoid the blank rows (zeros rows in my solution). 如果您不想使用宏,则至少需要使用过滤器来避免出现空白行(我的解决方案中的行为零)。

I have assumed your data lies in columns A, B and C 我假设您的数据位于A,B和C列中

In column D us the below formulae 在D列中,我们使用以下公式

=IF(MOD(ROW()+2,3)=0,INDEX(A:A,QUOTIENT(ROW()+2,3)),IF(MOD(ROW()+2,3)=1,INDEX(B:B,QUOTIENT(ROW()+2,3)),IF(MOD(ROW()+2,3)=2,INDEX(C:C,QUOTIENT(ROW()+2,3)),"")))

Drag the formula down as far as you can. 尽可能向下拖动公式。 the filter this column to remove zeros. 过滤此列以删除零。

屏幕截图

If you don't want to use VBA macros I think that the best way to get it is to copy column B, the column C, then column D, ... one under each other, like this: 如果您不想使用VBA宏,我认为获得它的最好方法是复制B列,C列,然后复制D列,...彼此下面,如下所示:

A
C
F
G
B
D

H

E

I

Then use the menu Data => Remove Duplicates to remove multiple spaces. 然后使用菜单数据=>删除重复项删除多个空格。

If you change idea about using macros just search "unpivot data Excel" on Google and you'll find a plenty of solutions about it. 如果您改变了使用宏的想法,只需在Google上搜索“ unpivot data Excel”,您就会找到很多解决方案。

I can think of a couple ways to do this. 我可以想到几种方法来做到这一点。 Use the formula below. 使用以下公式。

=INDEX(MyData,1+INT((ROW(A1)-1)/COLUMNS(MyData)),MOD(ROW(A1)-1+COLUMNS(MyData),COLUMNS(MyData))+1) = INDEX(MyData,1 + INT((ROW(A1)-1)/ COLUMNS(MyData)),MOD(ROW(A1)-1 + COLUMNS(MyData),COLUMNS(MyData))+ 1)

This assumes 'MyData' is a named range. 假设“ MyData”是一个命名范围。

As an alternative, you could use a simple VBA script to do the job. 或者,您可以使用简单的VBA脚本来完成此工作。

Sub ConvertRangeToColumn()
Dim Range1 As Range, Range2 As Range, Rng As Range
Dim rowIndex As Integer
xTitleId = "Title"
Set Range1 = Application.Selection
Set Range1 = Application.InputBox("Source Ranges:", xTitleId, Range1.Address, Type:=8)
Set Range2 = Application.InputBox("Convert to (single cell):", xTitleId, Type:=8)
rowIndex = 0
Application.ScreenUpdating = False
For Each Rng In Range1.Rows
    Rng.Copy
    Range2.Offset(rowIndex, 0).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    rowIndex = rowIndex + Rng.Columns.Count
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

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

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