VBA常用

获取有内容的单元格的总行数,内容相同的的打○ 不同的打×

Sub biaoji()

leng =ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row  'get use cells count

Debug.Print leng

For i = 2 To leng

  IfRange("C" & i).Value <> Range("d" & i)Then       ' <>

   

   Range("e" & i).Value = "亊"

   Range("b" & i).Interior.ColorIndex = 28     'add color

   Range("c" & i).Interior.ColorIndex = 28

   Range("d" & i).Interior.ColorIndex = 28

   Range("e" & i).Interior.ColorIndex = 28

  

   If Range("b" & i) = "" Then

   Range("a" & i).Interior.ColorIndex = 28

   End If

 Else

  Range("e" & i).Value = "仜"

  Range("a" & i).Interior.ColorIndex = 2

  Range("b" & i).Interior.ColorIndex = 2     'add color

   Range("c" & i).Interior.ColorIndex = 2

   Range("d" & i).Interior.ColorIndex = 2

   Range("e" & i).Interior.ColorIndex = 2

  EndIf

Next

End Sub


增加合并单元格功能

Sub biaoji()

Dim cnt As Integer

leng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row  'get use cells count

Debug.Print leng

For i = 2 To leng

  IfRange("C" & i).Value <> Range("d" & i)Then       ' <>

   

   Range("e" & i).Value = "×"

   Range("b" & i).Interior.ColorIndex = 28     'add color

   Range("c" & i).Interior.ColorIndex = 28

   Range("d" & i).Interior.ColorIndex = 28

   Range("e" & i).Interior.ColorIndex = 28

  

   If Range("b" & i).Value = "" Then

   Range("a" & i).Interior.ColorIndex = 28

   End If

 Else

  Range("e" & i).Value = "○"

  Range("a" & i).Interior.ColorIndex = 2

  Range("b" & i).Interior.ColorIndex = 2     'add color

   Range("c" & i).Interior.ColorIndex = 2

   Range("d" & i).Interior.ColorIndex = 2

   Range("e" & i).Interior.ColorIndex = 2

  EndIf

Next

For i = 1 To leng Step 1

If Range("a" & i).Value<> "" Then                   'merge条件

   j= i

       Do While 1

           If Range("a" & j + 1).Value = "" Then

                 cnt = cnt + 1

           Else

                 'cnt = 0

                 Exit Do

           End If

       j = j + 1         ‘count

       If j > leng Then

       j = j - 1

       Exit Do

       End If

       

       Loop

End If

   Range("A" & i & ": A" & j).Select

   Selection.merge          'merge function

    i= j '- 1

   'Debug.Print "i= " & i

   'Debug.Print "j= " & j

   

Next

End Sub



猜你喜欢

转载自blog.csdn.net/daihuibing389/article/details/80131002
vba
今日推荐