ActiveSheet.UsedRange.Columns.Count - 8 what does it mean?

后端 未结 5 2073
孤独总比滥情好
孤独总比滥情好 2020-12-28 09:14

what does ActiveSheet.UsedRange.Columns.Count - 8 mean in vba?

How does vba know the usedRange?

5条回答
  •  悲&欢浪女
    2020-12-28 09:58

    BernardSaucier has already given you an answer. My post is not an answer but an explanation as to why you shouldn't be using UsedRange.

    UsedRange is highly unreliable as shown HERE

    To find the last column which has data, use .Find and then subtract from it.

    With Sheets("Sheet1")
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lastCol = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByColumns, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Column
        Else
            lastCol = 1
        End If
    End With
    
    If lastCol > 8 Then
        'Debug.Print ActiveSheet.UsedRange.Columns.Count - 8
    
        'The above becomes
    
        Debug.Print lastCol - 8
    End If
    

提交回复
热议问题