Library (Object)

  1. Work book
    • To activated active or current Workbook.
      ActiveWorkbook.Activate
    • To activated Workbook that contain this code.
      ThisWorkbook.Activate
    • To open existing Workbook.
      Dim myBook As Workbook
      Set myBook = Workbooks.Open("C:\MyBook.xlsx")
    • To add new Workbook.
      Dim myNewBook As Workbook
      Set myNewBook = Workbooks.Add(1)
    • To save Workbook.
      Direct Save:
      ActiveWorkbook.Save
      Save As:
      ActiveWorkbook.SaveAs Filename:="C:\My Document\File Name.xlsx ", _
      FileFormat:=51, CreateBackup:=False
    • To delete Workbook.
      Kill "C:\Users\UserName\Documents\MyBook.xlsx"
        Note: Workbook must be close
    • To close Workbook.
      Close with out save:
      ActiveWorkbook.Close False
      Close and save:
      ActiveWorkbook.Close True
  2. Work sheet
    • To select active Sheet.
      ActiveSheet.Select
    • To select Sheet number.
      Sheets(1).Select
        Note: Change the number in bracket or variable as integer to loop
    • To select Sheet name.
      Sheets("Sheet Name").Select
    • To get Sheet index or number.
      Sheets("Sheet Name").Index
    • To add Sheet.
      Add before specific Sheet:
      Sheets.Add After:=Sheets(1)
        Note: Change the number in bracket

      Add after specific Sheet:
      Sheets.Add Before:=Sheets(1)
        Note: Change the number in bracket

      Add before active Sheet and rename:
      Worksheets.Add().Name = "Summary(Before Active)"
      Add after last Sheet and rename:
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Summary(End)"
      Add multiple (ex: 4) after last Sheet:
      Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=4
    • To copy Sheet.
      Copy before specific Sheet:
      Sheets("Sheet Name").Copy Before:=Sheets(1)
        Note: Change the number in bracket

      Copy after specific Sheet:
      Sheets("Sheet Name").Copy After:=Sheets(1)
        Note: Change the number in bracket

      Copy to last Sheet:
      Sheets("Sheet Name").Copy After:=Sheets(Sheets.Count)
    • To delete Sheets in Workbook.
      Application.DisplayAlerts = False
      ActiveSheet.Delete
      'Or Sheets(1).Delete Or Sheets("Sheet Name").Delete
      Application.DisplayAlerts = True
        Note: a) Set DisplayAlerts off to skip prompt message "Microsoft Excel will
                      permanently delete this sheet. Do you wish to continue?
                  b) Must left at least one Sheet after delete.
    • To count number of Sheets in Workbook.
      Sheets.Count
  3. Range
    • Select range.
      To select range between 2 cells:
      Range("A1:B2").Select
        Or
      Range(Cells(1, 1), Cells(2, 2)).Select
      To select range between 2 rows:
      Range(Rows(1), Rows(6)).Select
      To select range between 2 columns:
      Range("A:B").Select
        Or
      Range(Columns(1), Columns(6)).Select
    • To set used range.
      Active sheet:
      Dim myRange As Range
      Set myRange = ActiveSheet.UsedRange
      By Sheet number:
      Dim myRange As Range
      Set myRange = Sheets(1).UsedRange
      By Sheet name:
      Dim myRange As Range
      Set myRange = Sheets("Sheet Name").UsedRange
    • Go to specific range.
      Application.Goto ActiveSheet.Range("C5"), True
        Or
      Application.Goto Sheets(1).Range("C5"), True
        Or
      Application.Goto Sheets("Sheet Name").Range("C5"), True
        Note: Change C5 to any cell reference
    • Work with Selection
      Dim Cell as Range
      For Each Cell In Selection
        'Your Code Here..
      Next
      To get Row Index
      Cell.Row
      To get Column Index
      Cell.Column
      To get Total Rows in selection
      Selection.Rows.Count
      To get Total Columns in selection
      Selection.Columns.Count
  4. Rows
    • Select row.
      Individual row:
      Rows(2).Select
        Note: Change the number in bracket
      Multiple rows:
      Rows("1:2").Select
        Note: Change the number in bracket
    • Get last row for used range.
      Last row single column:
      Range("A1048576").End(xlUp).Row
      Total rows of used range:
      ActiveSheet.UsedRange.Rows.Count
      Last row of used range:
      ActiveCell.SpecialCells(xlCellTypeLastCell).Row
        Or
      Cells.SpecialCells(xlCellTypeLastCell).Row
        Or
      ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
        Or
      Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  5. Columns
    • Select column.
      Individual Column:
      Columns(1).Select
        Note: Change the number in bracket
      Multiple Columns:
      Columns("A:B").Select
        Note: Change the letter in bracket
    • Get last column for used range.
      Last column single row:
      Range("IV1").End(xlToLeft).Column
      Total columns of used range:
      ActiveSheet.UsedRange.Columns.Count
      Last columns of used range:
      ActiveCell.SpecialCells(xlCellTypeLastCell).Column
        Or
      Cells.SpecialCells(xlCellTypeLastCell).Column
        Or
      ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
        Or
      Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
  6. Cells
    • Select cell.
      Column use letter,Row use number:
      Range("A1").Select
        Note: Change the letter and number in bracket or use integer variable
      Both Column and Row use number:
      Cells(1, 1).Select
        Note: Change the number in bracket or use integer variable
      Whole Cell in Sheet:
      Cells.Select
        Note: To select whole cells in sheet.
  7. Auto Shapes
    • Count AutoShapes:
      ActiveSheet.Shapes.Count
        Note: Make sure your have at least one AutoShapes
    • Get AutoShapes name and text inside:
      Dim ObjShp As Object
      For Each ObjShp In ActiveSheet.Shapes
          Debug.Print ObjShp.Name
          Debug.Print ObjShp.TextFrame.Characters.Text
      Next
  8. Error Handler
    • Reset error:
      On Error GoTo 0
    • Skip Error:
      On Error GoTo SkipLine1

No comments:

Post a Comment