VBA_常用VBA代码
'批量替换字符 Sub Test() Dim i As Integer For i = 2 To 89 If Cells(i, 3).Value = "已激活" Then Cells(i, 3).Value = "Active" End If Next End Sub
Sub Test() Dim rowsNum, i, j, equalRowsNum As Integer '声明变量 rowsNum = ActiveSheet.UsedRange.Rows.Count '获得行数 For i = 3 To rowsNum '遍历全部行数 If Cells(i, 1).Value = Cells(i + 1, 1).Value Then j = j + 1 Else For equalRowsNum = 1 To j '给指定列追加数值。把相同行的同列追加到第一相同行 Cells(i - j, 6).Value = CStr(Cells(i - j, 6).Value) + Chr(10) + CStr(Cells(i - j + equalRowsNum, 6).Value) Next j = 0 End If Next Range("A3:G20").RemoveDuplicates 1 '用自带函数清空重复行 For i = 3 To rowsNum '遍历全部行数,删除空行。 '(这段代码须多执行几次,才能把空行删干净) If Cells(i, 1) = "" Then Cells(i, 1).EntireRow.Delete End If Next '另外,还可以用排序的方式“清除”空行: 'Sheet1.UsedRange.Sort [A3], xlAscending, Header:=xlYes '注:[A3]代表作为排序依据的列 'Debug.Print rowsNum '测试 End Sub