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

preview_player
Показать описание
**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
Рекомендации по теме
Комментарии
Автор

Thanks a lot. It's great than other to me.

asaduzzamandaria
Автор

Thanks for this video. I wonder whether can I get the name of the WB which I have saved from?

dimamedvedev
Автор

So can i make an if statement to this? For if the the file path <> to my path then ill put msg box then close the. App

mr.write
Автор

I don't code. A former employee created a macro that imports orders from Amazon. I have opened that macro in my own PC but when I want to import data myself it tells me something about a path.
I'm sorry for the vague wording and explanation to the issue. Anyway, how do I create the right path so I can continue using this system? Thank you

Oszysr
Автор

Any function to insert the ActiveWorkbook path on cell? I mean, any time you load the excel book an write automaticaly in the cel the new path activeworkbook.path

miicelltechnosv
Автор

Do you know a way to create an alias for the username section of the file path. Similar to the path you extracted, my organization shows the individual usernames near the beginning of the string. I created a macro that will copy and paste data from a template into a new workbook and save the new workbook as a defined cell reference. It works perfectly for me but my coworker cannot run it on her machine.

ericmcgough
Автор

Hi, . thanks for the video. Can you please help me to detect the error that it is triggered in the last line: ThisWorkbook.SaveAs Filename:=nombreFichero

Sub backAuto()

RunTimer = Now + TimeValue("01:00:00")

Application.OnTime RunTimer, "backAuto"


'nombreFichero = "c:\temp\Excel\" & Year(Date) & Month(Date) & Day(Date) & Hour(Time) & Minute(Time) & Second(Time) & ".xlsx"
nombreFichero = "f:\davidtemp\" & Year(Date) & Month(Date) & Day(Date) & Hour(Time) & Minute(Time) & Second(Time) & ".xlsx"
MsgBox nombreFichero
ThisWorkbook.SaveAs Filename:=nombreFichero

End Sub

davidsanchez-buun