合并多个单元格内容的函数(合并excel多个单元格数据)
导语:实例14-合并多个单元格数据,实例15-按文本查找指定列插入空行
实例14-合并多个单元格数据
Private Sub CommandButton合并数据_Click()
&34;操作界面&34;C&34;&34;C&34;请输入合并符号&39;合并区域不能为空
Dim mergerange As String
If .Cells(7, &34;).Value <> &34; Then
mergerange = .Cells(7, &34;).Value
Else
MsgBox &34;
Exit Sub
End If
&34;B&34;&39;合并数据
Dim itemcell
Dim mergeresult As String
For Each itemcell In ThisWorkbook.Worksheets(&34;).Range(mergerange)
If itemcell.Value <> &34; Then
If mergeresult <> &34; Then
mergeresult = mergeresult & mergetext & itemcell.Value
Else
mergeresult = itemcell.Value
End If
End If
Next
.Cells(10, &34;).Value = mergeresult
End With
End Sub
实例15-按文本查找指定列插入空行
Private Sub CommandButton插入_Click()
&34;操作界面&34;C&34;&34;C&34;&34;C&34;&34;C&34;&34;C&34;&34;D&34;&34;参数不能为空&39;定义变量
Dim wbname As String
Dim shname As String
wbname = Trim(.Cells(2, &34;).Value)
shname = Trim(.Cells(6, &34;).Value)
Dim matchcolumn As Long
Dim startnum As Long
Dim stopnum As Long
matchcolumn = Trim(.Cells(10, &34;).Value)
startnum = Trim(.Cells(14, &34;).Value)
stopnum = Trim(.Cells(14, &34;).Value)
Dim matchtext As String
matchtext = Trim(.Cells(18, &34;).Value)
End With
&39;循环判断(反向)
Dim i
For i = stopnum To startnum Step -1
If .Cells(i, matchcolumn) <> &34; And .Cells(i, matchcolumn) = matchtext Then
.Rows(i).Insert
End If
Next i
End With
Workbooks(wbname).Save
MsgBox &34;
Workbooks(wbname).Activate
ActiveWindow.WindowState = xlMaximized
Workbooks(wbname).Worksheets(shname).Activate
Workbooks(wbname).Worksheets(shname).Cells(1, 1).Select
Exit Sub
处理出错:
MsgBox Err.Description
End Sub
本文内容由小彤整理编辑!