|
老师,上一课作业我不会做,听完课后才做。现交如下
Sub 第一题_按钮2_单击()
Dim rg1, rg2 As Range
Dim firstRow, endRow, k As Integer
Set rg1 = Range("a1")
Do
k = k + 1
Set rg1 = Range("a:a").Find("A", after:=rg1, lookat:=xlWhole)
firstRow = rg1.Row
Set rg2 = Range("a:a").Find("A", after:=rg1, lookat:=xlWhole)
If rg2.Row < firstRow Then
endRow = Range("a65536").End(xlUp).Row
Else
endRow = rg2.Row - 1
End If
Range("a" & firstRow & ":a" & endRow).Copy Cells(1, 2 + k)
Loop Until rg2.Row < firstRow
End Sub
Sub 按钮2_单击()
Dim x, firstRow As Integer
If IsNull(Range("a1:a15").MergeCells) = True Then
Range("a1:a15").UnMerge
Else
For x = 2 To Range("a65536").End(xlUp).Row
firstRow = x
Do
x = x + 1
Loop Until Cells(x, 1) <> Cells(x + 1, 1)
Range(Cells(firstRow, 1), Cells(x, 1)).Merge
Application.DisplayAlerts = False
Next x
End If
End Sub
第二题还有个疑问,如果合并之后,只剩下左上角数据了,中间会空一些,怎么办呢?
我加了一个判断,如下
Sub 按钮2_单击()
Dim x, firstRow As Integer
If IsNull(Range("a1:a15").MergeCells) = True Then
Range("a1:a15").UnMerge
For x = 2 To 15
If Cells(x, 1) = "" Then Cells(x, 1) = Cells(x - 1, 1)
Next x
Else
For x = 2 To Range("a65536").End(xlUp).Row
firstRow = x
Do
x = x + 1
Loop Until Cells(x, 1) <> Cells(x + 1, 1)
Range(Cells(firstRow, 1), Cells(x, 1)).Merge
Application.DisplayAlerts = False
Next x
End If
End Sub
vba第12课作业.rar
(9.51 KB, 下载次数: 3)
|
|