Sunday, April 2, 2017

WorkSheet

'Worksheet Object
'Represents a worksheet.
'Remarks
'
'
'The Worksheet object is a member of the Worksheets collection. The Worksheets collection contains all the Worksheet objects in a workbook.
'
'The Worksheet object is also a member of the Sheets collection. The Sheets collection contains all the sheets in the workbook (both chart sheets and worksheets).
'
'
'Example
'
'
'Use Worksheets(index), where index is the worksheet index number or name, to return a single Worksheet object. The following example hides worksheet one in the active workbook.
'
'Visual Basic for Applications
'Worksheets(1).Visible = False
'
'The worksheet index number denotes the position of the worksheet on the workbook’s tab bar. Worksheets(1) is the first (leftmost) worksheet in the workbook, and Worksheets(Worksheets.Count) is the last one. All worksheets are included in the index count, even if they’re hidden.
'
'The worksheet name is shown on the tab for the worksheet. Use the Name property to set or return the worksheet name. The following example protects the scenarios on Sheet1.
'

'Create a macro to find out if the workbook belongs to him.
Sub Wokbookname()

Name = Application.UserName
ans = MsgBox("Is your name: " & Name, vbYesNo)
If ans = vbYes Then MsgBox "Great"
If ans = vbNo Then MsgBox "Ohh I am Sorry"

End Sub

Sub testWrksheets()
   
    Worksheets("sheet14").Select
   
    Worksheets("sheet12").Select False                  'Selecting multiple sheet
   
    Worksheets(5).Select        'Indexing of worksheets
   
    Sheet16Code.Select      'anotherway to selectsheet(by 2nd name of the sheet)
   
    Worksheets.Add          'adding a worksheet
   
    Worksheets.Add Worksheets("Sheet14")        'adding a worksheet before sheet14
   
    Worksheets.Add , Worksheets("Sheet12")      'adding a worksheet after sheet12 or you can _
                                                    write before:=/after:=worksheets("Sheet14") for clear refernce
   
    Worksheets.Add Before:=Worksheets(1)        'adding sheet to the start
   
    Worksheets.Add after:=Worksheets(Sheets.Count)      'adding sheet to the end
   
    Worksheets.Add after:=Worksheets(Sheets.Count), Count:=3    'adding 3 new sheets to the end
   
    Sheets.Add Before:=Worksheets(1), Type:=XlSheetType.xlChart     'adding chart type worksheet
   
    Application.DisplayAlerts = False
   
    Sheets(Sheets.Count).Delete       'deleting last sheet of the workbook
   
    Application.DisplayAlerts = True    'please note sheets include charts and other type but worksheets only refer to spreadsheet
   
    Charts.Delete   'delete all object type in one go
   
    Worksheets("sheet1").Copy , Worksheets("sheet25")   'copying sheet to the specific location
   
    Worksheets("sheet1").Copy   'without option if copied it will be copied in new brand new workbook
   
    Worksheets("sheet1").Copy Workbooks("book1").Sheets(1)      'coping to a another workbook
   
    Sheet1.Move after:=Sheets(Sheets.Count)      'moving of sheet
   
    Worksheets("sheet28").Name = "somethingelse"       'Name change of sheet
   
    Worksheets("somethingelse").Visible = xlSheetHidden     'hiding a sheet
   
    Worksheets("somethingelse").Visible = xlSheetVisible    'Unhiding a sheet
   
    Worksheets("somethingelse").Visible = xlSheetVeryHidden 'user cant unhide the sheet without code

End Sub

No comments:

Post a Comment

*INTERVIEW QUESTIONS

* Ques 01. What is the difference between ByVal and ByRef and which is default ? Ans-  ByRef : If you pass an argument by reference when...