Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在单元格A1中组合具有相似值的行_Excel_Vba - Fatal编程技术网

Excel 在单元格A1中组合具有相似值的行

Excel 在单元格A1中组合具有相似值的行,excel,vba,Excel,Vba,我在Excel中有两个文本列,大约有10万行。我需要合并B列中的文本,A列与之类似。因此: 为此: 如果这是一个一次性项目,我会将a列和B列复制到一个单独的表中,按a列排序 In column C (Row2) a formula "IIf(A2=A1;0;1)" In column D (Row2) a formula "IIf(C1=1;B2;B1 & " " & B2)" 然后填到最后一行。将整个内容(仅值)复制到另一个表中,然后再次排序(按C(向下)和a(向上)。这可

我在Excel中有两个文本列,大约有10万行。我需要合并B列中的文本,A列与之类似。因此:

为此:


如果这是一个一次性项目,我会将a列和B列复制到一个单独的表中,按a列排序

In column C (Row2) a formula "IIf(A2=A1;0;1)"
In column D (Row2) a formula "IIf(C1=1;B2;B1 & " " & B2)"

然后填到最后一行。将整个内容(仅值)复制到另一个表中,然后再次排序(按C(向下)和a(向上)。

这可能不是最有效的方法,但它是有效的

Sub CellStringCombine()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim intNumRange As Long
Dim strNewName As String
Dim x As Long
Dim y As Long
Dim intRowDiff As Long
Dim intRow As Long

intNumRange = WorksheetFunction.CountA(Range("A:A"))

x = 1

'start looping through rows
Do While Cells(x, "A") <> ""
'set the placeholder variable, offset to the next row
    y = x + 1
'if the current row is equal to the next one, find out how far it's equal
    Do While Cells(x, "A") = Cells(y, "A")
        y = y + 1
    Loop
    intRowDiff = y - x

'check to see if the next row isn't equal. go to next row if yes.

    If intRowDiff = 1 Then
        GoTo NextCell
    End If

'Loop through the range identified
    For intRow = x To x + intRowDiff - 1

'If it's the first round, only take the name
        If intRow = x Then
            strNewName = Cells(intRow, "B")
'If it's after the first round, have it equal itself and put a space
        ElseIf intRow > x Then
            strNewName = strNewName + " " + Cells(intRow, "B")
        End If
    Next intRow

'Delete the identified range except the first row
    Range("A" & x + 1, "B" & y - 1).EntireRow.Delete

'Overwrite the text in column B
    Cells(x, "B") = strNewName

NextCell:
x = x + 1

Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
Sub-CellStringCombine()
Application.ScreenUpdating=False
Application.DisplayAlerts=False
暗淡的内部区域与长的相同
作为字符串的Dim strNewName
暗x等长
长得一样暗
暗淡的内向如长
暗淡无光
intNumRange=WorksheetFunction.CountA(范围(“A:A”))
x=1
'开始在行中循环
Do While单元格(x,“A”)“”
'设置占位符变量,偏移到下一行
y=x+1
'如果当前行与下一行相等,请找出它相等的距离
Do While单元格(x,“A”)=单元格(y,“A”)
y=y+1
环
intRowDiff=y-x
'检查下一行是否不相等。如果不相等,请转到下一行。
如果intRowDiff=1,则
转到下一个
如果结束
'在确定的范围内循环
对于intRow=x到x+intRowDiff-1
如果是第一轮,只记名字
如果intRow=x,则
strNewName=单元格(简介,“B”)
如果它在第一轮之后,让它自己相等,然后放一个空格
ElseIf intRow>x然后
strNewName=strNewName+“”+单元格(简介,“B”)
如果结束
下一篇介绍
'删除除第一行以外的已标识范围
范围(“A”&x+1,“B”&y-1)。EntireRow.Delete
'覆盖B列中的文本
单元格(x,“B”)=strNewName
NextCell:
x=x+1
环
Application.ScreenUpdating=True
Application.DisplayAlerts=True
端接头

只是因为我想看看是否可以使用数组来完成

Sub JSA()
Dim i&, t&
Dim StrArr() As String
Dim ows As Worksheet
Dim tws As Worksheet

ReDim StrArr(0)

Set ows = ActiveWorkbook.Worksheets("Sheet2")
Set tws = ActiveWorkbook.Worksheets("Sheet3")

With ows
    For i = 1 To .Range("A" & .Rows.count).End(xlUp).Row
        If i = 1 Then
            StrArr(0) = .Cells(i, 1) & "|"
        ElseIf .Cells(i, 1) <> .Cells(i - 1, 1) Then
            ReDim Preserve StrArr(UBound(StrArr) + 1) As String
            StrArr(UBound(StrArr)) = .Cells(i, 1) & "|"
        End If
        StrArr(UBound(StrArr)) = StrArr(UBound(StrArr)) & .Cells(i, 2) & " "
    Next i
End With

For t = 1 To UBound(StrArr) + 1
    tws.Cells(t, 1) = Split(StrArr(t - 1), "|")(0)
    tws.Cells(t, 2) = Trim(Split(StrArr(t - 1), "|")(1))
Next t
End Sub
子JSA()
暗i&t&
Dim StrArr()作为字符串
将ows设置为工作表
将tws设置为工作表
雷迪姆斯特拉尔(0)
设置ows=ActiveWorkbook.Worksheets(“Sheet2”)
设置tws=ActiveWorkbook.Worksheets(“Sheet3”)
与ows
对于i=1到.Range(“A”&.Rows.count).End(xlUp).Row
如果i=1,那么
StrArr(0)=.单元(i,1)和“|”
ElseIf.Cells(i,1)。Cells(i-1,1)然后
ReDim将StrArr(UBound(StrArr)+1)保留为字符串
StrArr(UBound(StrArr))=.单元格(i,1)和“|”
如果结束
StrArr(UBound(StrArr))=StrArr(UBound(StrArr))&单元格(i,2)&“
接下来我
以
对于t=1至UBound(StrArr)+1
tws.Cells(t,1)=拆分(StrArr(t-1),“|”)0
tws.单元格(t,2)=修剪(分割(StrArr(t-1),“|”)单元格(1))
下一个t
端接头

请告诉我们您尝试了哪些代码。我尝试了一个搜索列然后更新的公式。问题是行太多,列a中的一些数据可能只有2行或27行。我不确定公式是否可行。如果您希望所有数据都进入列B,我很确定您需要VBA。如果您可以在B、C和D列(等)中选择不同的匹配项(“苹果”、“树”、“水”)然后可能有一个公式可以这样做。你也可以将它们放在C、D、E等中,然后连接到B中。到目前为止,你尝试过什么公式?如果你非常渴望公式解决方案,请参阅此处的讨论:简言之,没有简单的方法,但可以使用辅助列。你使用什么规则来省略B1 i中的
n您的
From
表从
表中的B1。如果您将所有单词组合在一起,那么
B1
不应该是
苹果树水