The difference between worksheets and obj worksheet

I am trying to understand the difference between worksheetsand worksheetobj in Excel VBA. I understand from the MSDN link that worksheetis a child of worksheetsand sheets.

However, we reference each worksheet using worksheetsobj, not worksheetobj. eg.

worksheets("ExcelIsCool").range("a1").value -> CORRECT
worksheet ("ExcelIsCool").range("a1").value -> INCORRECT

My question is what is the difference between the two?

Used worksheetto declare a variable (the only place I've used so far). eg.

dim wks as worksheet 
+4
source share
3 answers

- Worksheet. " " " " - , - " ".

, , . ThisWorkbook.Worksheets( "MyWorksheetName" ), , .

:

, Worksheet, , , , , , , intellisense Worksheet. , (, , ), :

Public Function GetLastRow(ByRef wsTarget as Excel.Worksheet, ByVal column as Long) as Long
    GetLastRow = wsTarget.Cells(wsTarget.Rows.Count, column).End(xlUp).Row
End Function

, , , , ) ( ). , (GetLastRow) , (ActiveWorkbook ThisWorkbook ). , , . ( VBA):

last_row = GetLastRow(Sheet1, 1)

(. this).

Worksheet , . Worksheet intellisense, . , , .

+4

@Orphid, Worksheets collection Worksheet.

Worksheet .

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

, Worksheet collection Worksheets.

Worksheet collection.

Dim ws As Worksheet

For Each ws In Worksheets
    Debug.Print ws.Name
Next ws

, , Worksheet , Worksheet, .

+8

, "". , :

:

Worksheets("Sheet1").Range("A1").Value = "1"

"":

Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
wks.Range("A1").Value = "1"

, , - , "Sheet1". Excel wks , , wks , .

, wks, ( "Sheet1" ) - . , "" (, wks) "".

0
source

Source: https://habr.com/ru/post/1605692/


All Articles