본문 바로가기

WeekdayLife

[엑셀VBA] Sheet와 WorkSheet type의 다른점

변수를 선언하다보면, type 중 Sheet와 WorkSheet가 뭐가 다른지 싶다. 살짝 구글링을 해본 결과를 기술해 두자면,

Sheet는 WorkSheet를 포함한다.

Sheet > WorkSheet

 

Sheet를 구성하는 타입은 WorkSheet 외에도 더 있다. 단, 현재는 다 없어진(?)거나 다름없고, ChartSheet 아니면 WorkSheet만 남았다고 생각해도 무방하다 한다.

WorkSheet : the sheet with the gridlines and cells
Chart : the sheet which contains a single chart
DialogSheet : deprecated
Macro sheets : deprecated
International Macro sheet : deprecated

 

아래 article을 참고했다.

 

Difference between Sheets and Worksheets in VBA - Excel Off The Grid

I use the terms Sheet and Worksheet interchangeably when talking about Excel, I think most users do.  Google also appears to think they are the same thing; If

exceloffthegrid.com

 

 

그래서 나는 그냥 아무거나 쓴다.

아래 코드는 sheet를 검사하여, listobject의 수를 반환한다.

 

Sub listobjectcount()

Dim obj As Object
Dim sht As Worksheet
   
    For Each obj In ActiveWorkbook.Sheets
       
        Debug.Print "[WorkSheet name] " & obj.Name
        Debug.Print "[Sheet type] " & TypeName(obj)
       
        Set sht = Worksheets(obj.Name)
       
        With sht            
           
            If .ListObjects.Count > 0 Then
                For Each lic In .ListObjects
                    Debug.Print lic.Name
                Next lic
            Else: Debug.Print "There are no ListObjects"
           
            End If
   
        End With
       
    Next obj
   
End Sub

'<<Result>>
'[Sheet type] Worksheet
'[WorkSheet name] Sheet1
'Table1
'Table13
'Table14
'[Sheet type] Worksheet
'[WorkSheet name] Sheet2
'There are no ListObjects

 

몰라서 찾아봤는데, 모르고 있는게 많다는 사실만 확인하게 됐다.