Home  >  Article  >  Software Tutorial  >  Excel VBA code: Merge child rows to parent rows, the result is parent row 1 and its children

Excel VBA code: Merge child rows to parent rows, the result is parent row 1 and its children

WBOY
WBOYforward
2024-01-20 08:15:05997browse

一段excel vba代码用于将子表行合并到父表结果:父行1若干

A piece of excel vba code is used to merge the child table rows into the parent table. The result is: several parent rows 1

Assuming your first line is the title line start with the second line

Sub merge()

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

BIAORADDRESS = 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(BIAORADDRESS).Offset(0, ii)

Next

Set A = BiaoErID.FindNext(A)

Loop While Not A Is Nothing And A.Address biaoertopaddress

End If

Next

End Sub

How to write a VBA code to count the number of worksheets in a workbook

Sub worksheet number statistics ()

If Sheets(1).Name "Statistics on the number of worksheets" Then

yn = MsgBox ("[Worksheet Quantity Statistics] worksheet does not exist, create [Worksheet Quantity Statistics]?", vbYesNo, "Create [Worksheet Quantity Statistics] worksheet")

If yn = vbYes Then

Sheets.Add(Sheets(1)).Name = "Statistics on the number of worksheets"

End

End If

End If

Sheets ("Statistics on the number of worksheets").Select

Range("A1").Select

Cells(1, 1).Value = "The number of worksheets is"

Cells(1, 2).Value = Sheets.Count

End Sub

A worksheet named Worksheet Statistics will be created, and the number of worksheets will be displayed in the first row

Please experts write comments for the following excel VBA code. Please write the comments as detailed as possible. Thank you

Public m& 'Define public variable m

Sub lqxs()

Dim Arr, i&, Brr(1 To 27, 1 To 23), rq, nl 'Define the above variables, Brr is a two-dimensional array with 27 rows and 23 columns

Sheet1.Activate 'Make sheet1 the currently active sheet

[c5:z32].ClearContents 'Clear the data in cells C5 to Z32

Myr = Sheet2.[a65536].End(xlUp).row 'Assign the number of rows in the area with data in column A of Sheet2 to the Myr variable

Arr = Sheet2.Range("a7:t" & Myr) 'Name the range from column A7 to column T and row Myr as Arr

For i = 1 To UBound(Arr) 'Loop

rq = DateSerial(Left(Arr(i, 7), 4), Mid(Arr(i, 7), 5, 2), Right(Arr(i, 7), 2)) 'Change G in Sheet2 The data in the column changes to date format, such as: 2013/12/12

nl = DateDiff("yyyy", rq, Now) 'Determine whether the year of the data date in column G in Sheet2 is the same as this year. If it is the same, 0 will be returned. If it is not the same, 1 will be returned.

Call jd(Arr(i, 13)) 'Call jd() function

Brr(1, nl 1) = Brr(1, nl 1) 1 'Add one to the value of array Brr(1, nl 1)

Brr(m, nl 1) = Brr(m, nl 1) 1 'Add one to the value of array Brr(1, nl 1)

Next

[d5].Resize(27, 23) = Brr 'Start by assigning the value of the array to cell D5, and go to the 22nd column on the right of the 26th row below

[c5].Formula = "=sum(rc[1]:rc[23])" 'Add formula to cell C5, C5=D5 E5 F5  AB5

[c5].AutoFill [c5].Resize(27, 1) 'Format filling Fill the format of C5 into cell C31

[d6].Formula = "=sum(r[1]c:r[4]c)" 'Add formula to cell D6, D6=D7 D8 D9 D10

[d6].AutoFill [d6].Resize(1, 23) 'Format filling, fill the format of D6 into cell AB31

End Sub

The above is the detailed content of Excel VBA code: Merge child rows to parent rows, the result is parent row 1 and its children. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete