作业帮 > 综合 > 作业

求助一段VBA代码(合并和相加,取奇偶数)

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/08 17:06:59
求助一段VBA代码(合并和相加,取奇偶数)
要求:1、把F2和F3合并,(F1为标题),结果放在F2.如1和2,合并为12,同时删除F3内容.接着把F4和F5合并(同上)直至F列为空.
2、把I2和I3相加,(I1为标题),相加结果个位为奇数的,把奇字放在I2,(相加结果个位为偶数的,把偶字放在I2)同时删除I3内容,接着把I4和I5相加(同上)直至I列为空.
Sub iTest()
Dim r&, rs&, rng As Range, arr
With ActiveSheet

rs = .Cells(65536, "F").End(xlUp).Row
If rs Mod 2 = 0 Then rs = rs + 1
With .Cells(2, "F").Resize(rs - 2 + 1)
arr = .Cells
.ClearContents
For r = LBound(arr) To UBound(arr) - 1 Step 2
arr(r, 1) = arr(r, 1) & arr(r + 1, 1)
arr(r + 1, 1) = ""
Next
.Cells = arr
End With

rs = .Cells(65536, "I").End(xlUp).Row
If rs Mod 2 = 0 Then rs = rs + 1
With .Cells(2, "I").Resize(rs - 2 + 1)
arr = .Cells
.ClearContents
For r = LBound(arr) To UBound(arr) - 1 Step 2
If (arr(r, 1) + arr(r + 1, 1)) Mod 2 = 0 Then
arr(r, 1) = "偶"
Else
arr(r, 1) = "奇"
End If
arr(r + 1, 1) = ""
Next
.Cells = arr
End With
End With
MsgBox "ok!"
End Su