假设你的第一行是标题行从第二行开始
Sub 合并()
Endcol1 = Sheet1.[iv1].End(xlToLeft).Column
endrow1 = Sheet1.Range("B65536").End(xlUp).Row
endcol2 = Sheet2.[iv1].End(xlToLeft).Column
endrow2 = Sheet2.Range("B65536").End(xlUp).Row
Dim A As Range
Dim BiaoYiID As Range
Dim BiaoErID As Range
Dim MyRange1 As Range
Dim BiaoErH As Range
Dim leiji As Long
Sheet2.Activate
Set BiaoErID = Sheet2.Range(Cells(2, 2), Cells(endrow2, 2))
For i = 2 To endrow1
sxh = i + lieji
lieji1 = 0
biaoerneirong = Sheet1.Range("B" & sxh).Text
Set A = BiaoErID.Find(biaoerneirong, after:=BiaoErID.Cells(BiaoErID.Cells.Count), lookat:=xlWhole)
If Not A Is Nothing Then
biaoertopaddress = A.Address
Do
sxh1 = sxh + lieji1
BIAOERADDRESS = A.Address(RowAbsolute:=False, ColumnAbsolute:=False)
biaoyiaddress = Sheet1.Range("B" & sxh1).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Sheet1.Select
Sheet1.Range(biaoyiaddress).Offset(1).Activate
ActiveCell.EntireRow.Insert
lieji = lieji + 1
lieji1 = lieji1 + 1
For ii = 0 To endcol2
ActiveCell.Offset(0, ii) = Sheet2.Range(BIAOERADDRESS).Offset(0, ii)
Next
Set A = BiaoErID.FindNext(A)
Loop While Not A Is Nothing And A.Address biaoertopaddress
End If
Next
End Sub
Sub 工作表数量统计()
If Sheets(1).Name "工作表数量统计" Then
yn = MsgBox("【工作表数量统计】工作表不存在,建立【工作表数量统计】吗?", vbYesNo, "建立【工作表数量统计】工作表")
If yn = vbYes Then
Sheets.Add(Sheets(1)).Name = "工作表数量统计"
End
End If
End If
Sheets("工作表数量统计").Select
Range("A1").Select
Cells(1, 1).Value = "工作表数量为"
Cells(1, 2).Value = Sheets.Count
End Sub
会建立一个名为工作表统计的工作表,在第一行显示工作表数量
Public m& '定义公共变量m
Sub lqxs()
Dim Arr, i&, Brr(1 To 27, 1 To 23), rq, nl '定义上述几个变量,Brr为27行23列的二维数组
Sheet1.Activate '使sheet1为当前活动的sheet
[c5:z32].ClearContents '清除C5到Z32单元格内的数据
Myr = Sheet2.[a65536].End(xlUp).row '将Sheet2中A列有数据的区域的行数,赋值给Myr变量
Arr = Sheet2.Range("a7:t" & Myr) '将A7到T列Myr行的区域命名为Arr
For i = 1 To UBound(Arr) '循环
rq = DateSerial(Left(Arr(i, 7), 4), Mid(Arr(i, 7), 5, 2), Right(Arr(i, 7), 2)) '将Sheet2中G列中的数据变为日期格式,如:2013/12/12
nl = DateDiff("yyyy", rq, Now) '判断Sheet2中G列数据日期的年份是否和今年相同,相同传回0,不相同传回1
Call jd(Arr(i, 13)) '调用jd()函数
Brr(1, nl + 1) = Brr(1, nl + 1) + 1 '数组Brr(1, nl + 1) 的值加一
Brr(m, nl + 1) = Brr(m, nl + 1) + 1 '数组Brr(1, nl + 1) 的值加一
Next
[d5].Resize(27, 23) = Brr '将数组的值赋值给D5单元格开始,到下面26行右面22列
[c5].Formula = "=sum(rc[1]:rc[23])" '给C5单元格添加公式, C5=D5+E5+F5++AB5
[c5].AutoFill [c5].Resize(27, 1) '格式填充 将C5的格式填充到C31单元格
[d6].Formula = "=sum(r[1]c:r[4]c)" '给D6单元格添加公式,D6=D7+D8+D9+D10
[d6].AutoFill [d6].Resize(1, 23) '格式填充,将D6的格式填充到AB31单元格
End Sub
以上是Excel VBA代码:将子行合并到父行,结果为父行1及其子行的详细内容。更多信息请关注PHP中文网其他相关文章!