假設你的第一行是標題行從第二行開始
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
biaoertopaddressEnd If
Next
End Sub
Sub 工作表數量統計()
If Sheets(1).Name
"工作表數量統計" Thenyn = 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中文網其他相關文章!