filmov
tv
VBA Active Workbook, This Workbook, Name, Path and Renaming a Workbook (Code Included)

Показать описание
**Grab the Free VBA Quick Reference Guide
Grasping how the Activeworkbook and Thisworkbook work will help you gain confidence as you learn VBA. The active workbook is the workbook on top of all the other windows. In other words, the last Excel window or the current Excel window open is the active workbook. This workbook, on the other hand, is the workbook that is currently running the code. The Activeworkbook may change, and Thisworkbook will always be the Object running the code.
This video uses the Thisworkbook and Activeworkbook to display the "Name" of the workbook. We also pull the file folder or path for both this workbook and the active workbook. The last thing we do use FullName to get the full file path and name of the file.
In VBA, you cannot rename workbooks like you can with worksheets because it is the file's actual file path. So to rename a file, you need to use SaveAs to "rename" the workbook. In our example, we use the current path and use "&" to concatenate some text for the SaveAs method.
‘===================
‘======CODE========
‘===================
Sub WorkbookName()
Range("h10") = ThisWorkbook.Name
Range("h11") = ActiveWorkbook.Name
Range("h13") = ThisWorkbook.Path
Range("h14") = ActiveWorkbook.Path
Range("h16") = ThisWorkbook.FullName
Range("h17") = ActiveWorkbook.FullName
ThisWorkbook.SaveAs ThisWorkbook.Path & "New WB"
End Sub
Grasping how the Activeworkbook and Thisworkbook work will help you gain confidence as you learn VBA. The active workbook is the workbook on top of all the other windows. In other words, the last Excel window or the current Excel window open is the active workbook. This workbook, on the other hand, is the workbook that is currently running the code. The Activeworkbook may change, and Thisworkbook will always be the Object running the code.
This video uses the Thisworkbook and Activeworkbook to display the "Name" of the workbook. We also pull the file folder or path for both this workbook and the active workbook. The last thing we do use FullName to get the full file path and name of the file.
In VBA, you cannot rename workbooks like you can with worksheets because it is the file's actual file path. So to rename a file, you need to use SaveAs to "rename" the workbook. In our example, we use the current path and use "&" to concatenate some text for the SaveAs method.
‘===================
‘======CODE========
‘===================
Sub WorkbookName()
Range("h10") = ThisWorkbook.Name
Range("h11") = ActiveWorkbook.Name
Range("h13") = ThisWorkbook.Path
Range("h14") = ActiveWorkbook.Path
Range("h16") = ThisWorkbook.FullName
Range("h17") = ActiveWorkbook.FullName
ThisWorkbook.SaveAs ThisWorkbook.Path & "New WB"
End Sub
Комментарии