Tuesday, February 7, 2012

Excel VBA command (3)

Workbook

Command

Description

1. Workbooks.Open "Book1.xls"

Open "Book1.xls"

2. Workbooks.Open Sheets("Sheet1").Range("A1").Value

Open file by referring to file name in cell "A1" of "Sheet1"

3. Workbooks.Open Filename:= "C:\Book1.xls"

Open "C:\Book1.xls"

4. ThisWorkbook.Close

Close workbook which is coded

5. Workbooks("Book1.xls").Close

Close file "Book1.xls"

6. Workbooks(Range("A1").Value).Close

Close file by referring to file name in cell "A1"

7. ActiveWorkbook.Save

Save active workbook

8. Workbooks("Book1.xls").Save

Save "Book1.xls"

9. Workbooks(Range("A1").Value).Save

Save file by referring to file name in cell "A1"

10. ActiveWorkbook.SaveAs "C:\Book1.xls"

SaveAs active workbook to "C:\Book1.xls"

11. Workbooks("Book1.xls").SaveAs "C:\Book1.xls"

SaveAs "Book1.xls" to "C:\Book1.xls"

12. Workbooks(1).Close SaveChanges:=False

Close workbook index 1 without save

13. Workbooks(1).Close SaveChanges:=True

Close workbook index 1 with save

14. Kill "C:\Book1.xls"

Kill file "C:\Book1.xls"

15. Workbooks.Add

Add new workbook

16. ActiveWorkbook.Name

Return name of active workbook

17. Difference between ThisWorkBook and ActiveWorkBook

  • ThisWorkBook object refers to the workbook that the code is contained in
  • ActiveWorkBook object refers to the workbook that is currently on top (Active) in Excel

No comments:

Post a Comment