我有大约50张纸的数据,所有这些都是相同的结构。请在下面的示例中找到数据结构,在列中可能是数据,在下一列中也可以是“B”或“AB”。我想将这两列合并为一个,所以我的数据显示为236AB。我的代码应该适用于工作表中的所有列,因为在某些工作表中我有5列而在另一页中有25列。任何人都可以帮我这个吗?非常感谢!
答案 0 :(得分:0)
不是100%确定您的最终目标是什么,但是您不能在左侧添加新列并使其公式为CONCATENATE(A1:E1),并使其尽可能远离工作表它?
然后,如果您需要,之后您可以复制该列的粘贴值并删除其他值。
即使在excel中录制,也可以很快完成。 如果你遇到困难,你想放手一搏并回复吗?
答案 1 :(得分:0)
这是一个将2列合并在一起的函数:
Function mergeColumns(mergeColumn As Integer)
Dim i As Integer
'Adjust startvalue(1)
For i = 1 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row Step 1
'Combine mergeColumn and Column next to it
Cells(i, mergeColumn).Value = Cells(i, mergeColumn).Value & Cells(i, mergeColumn + 1).Value
'Clear the Content of the Cell next to Mergecolumn
Cells(i, mergeColumn + 1).Value = ""
Next i
End Function
假设您要合并A列和B列,则调用将为mergeColumns 1
现在找出一个例程来找到要合并的正确列。
答案 2 :(得分:0)
我已根据您的要求附加代码,它会自动搜索May和June关键字,并会单独为特定列执行连接。
Sub test()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim Ws As Worksheet
Dim monthss(12) As String
monthss(1) = "May"
monthss(2) = "June"
monthss(3) = "August"
For Each Ws In wb.Worksheets
For j = 1 To 3
With Ws.UsedRange
Set c = .Find(monthss(j), LookIn:=xlValues)
If Not c Is Nothing Then
firstrow = c.Row
firstcol = c.Column
End If
End With
Set c = Nothing
lastrow = Ws.Cells(Ws.Rows.Count, firstcol).End(xlUp).Row
' For May Sheet
If firstrow > 0 Then
For i = firstrow + 1 To lastrow
Ws.Cells(i, firstcol).Value = Ws.Cells(i, firstcol).Value & Ws.Cells(i, firstcol + 1).Value
Next
firstrow = 0
End If
' for June Sheet
Next j
Next Ws
End Sub