Excel宏的基本逻辑语句

IF语句

Sub 增减会计双下划线()
' 增减会计双下划线 宏
    If Selection.Font.Underline = xlUnderlineStyleDoubleAccounting Then
        Selection.Font.Underline = xlUnderlineStyleNone
    Else
        Selection.Font.Underline = xlUnderlineStyleDoubleAccounting
    End If
End Sub

For 循环和宏的相对引用、绝对引用

Sub 添加格式()
' 添加格式 宏
    Range("B1").Select
    
    ActiveCell.Range("A1:A6").Select
    
    For counter = 1 To 65
    
        Selection.Font.Bold = True
        ActiveCell.Offset(6, 0).Range("A1:A6").Select
        Selection.Font.Italic = True
        ActiveCell.Offset(6, 0).Range("A1:A6").Select
    
    Next counter
End Sub

Do While循环

Sub 添加格式改进版()
'
' 宏11 宏
'
    Range("B1").Select
    
    ActiveCell.Range("A1:A6").Select
    
    Do While ActiveCell <> ""
    
        Selection.Font.Bold = True
        ActiveCell.Offset(6, 0).Range("A1:A6").Select
        Selection.Font.Italic = True
        ActiveCell.Offset(6, 0).Range("A1:A6").Select
        
    Loop
End Sub

Do Until循环

Sub 添加格式改进版()
'
' 宏11 宏
'
    Range("B1").Select
    
    ActiveCell.Range("A1:A6").Select
'或者Do Until IsEmpty(ActiveCell)
    Do Until ActiveCell = ""
    
        Selection.Font.Bold = True
        ActiveCell.Offset(6, 0).Range("A1:A6").Select
        Selection.Font.Italic = True
        ActiveCell.Offset(6, 0).Range("A1:A6").Select
        
    Loop
End Sub

你可能感兴趣的:(Excel)