通过ThisWorkbook.Name可以获取当前Excel文件的名称,但不包括文件扩展名(如.xlsx)。 获取完整文件路径(可选): 如果需要获取文件的完整路径,包括文件名和扩展名,可以使用ThisWorkbook.FullName。 以下是具体的代码示例: vba Sub GetCurrentWorkbookName() Dim workbookName As String Dim fullPath As String ' 获取当前...
1、定义名称 点击菜单的【公式】—【定义名称】,打开【新建名称】对话框,在【名称】处输入一个定义名称,本例输入:sheetname,方便后面在工作表中引用;在【引用位置】处输入:=GET.WORKBOOK(1),然后点击【确定】。说明:GET.WORKBOOK是宏表函数,不能直接在单元格中使用,只能定义名称后,使用名称得到结果。=...
SubGetSheetName() DimPathAsString DimFileAsString DimWBAsWorkbook DimshtAsWorksheet Dimarr()AsString Dimnarr()AsString Application.ScreenUpdating =False Path = ThisWorkbook.Path &"/" File = Dir(Path &"*.xlsx") i = 0 DoWhileFile <>"" ...
We want to return the name of the current worksheet, “Qtr 1 2022”, in a cell in the workbook using a formula that combines the TEXTAFTER and CELL functions. We use the following steps: Select any cell in the active worksheet; in this case, we select cell A1 and enter the below fo...
IntegerDimsProcNameAsStringDimpkAsvbext_ProcKind' Open Excel, and open the workbook.SetobjXLApp =NewExcel.ApplicationSetobjXLWorkbooks = objXLApp.WorkbooksSetobjXLABC = objXLWorkbooks.Open("C:\ABC.XLS")' Empty the list box.List1.Clear' Get the project details in the...
1、定义一个WorkBook对象 Dim wb As Workbook 这里的wb,就是一个WorkBook对象,wb只是一个代号,用什么其他字符串都可以,只要符合VBA的命名规范。但我们建议还是要定义一个有一定意义的对象、变量名称,并且尽量保持一惯性。这样做的好处是显而易见的,一是加快你写代码的速度,二是复制代码的时候,改动的地方会...
MsgBox "欢迎打开:" & Chr(10) & ThisWorkbook.Name End Sub 四、总结 对象的属性、方法、事件,是非常重要的,建议想学习VBA的朋友多多了解并运用。 另外,为了更好地理解WorkBook对象的有关内容,我写了一段大杂烩代码,基本包括了上述内容,由于篇幅的关系,我放到第二条文章。
In Excel, there is no direct function to get the sheet name of the active sheet. Now the solution to this problem is to create a formula using multiple functions or to use a custom function created using the VBA. In this tutorial, you will learn both methods with examples. ...
Set OpenWorkbook = wb End Function 5.3 操作Excel工作表(Worksheet) 5.3.1 移动工作表 移动工作表是指将工作表移到工作簿中的其他位置。 在VBA中,可以使用WorkSheet.Move方法来移动工作表。 语法:表达式.Move(Before, After) 其中,在Move方法中,主要包含...
使用VBA可以方便的获得sheet名,但事实上使用GET.WORKBOOK(1)宏函数也同样可以方便的获得sheet名。 不过GET.WORKBOOK(1)与普通的EXCEL函数不一样,无法直接使用,需要先在名称管理器里面自定义函数来引用。 定义名称“SN”引用GET.WORKBOOK(1) 这样,就能在EXCEL中通过“SN”来调用GET.WORKBOOK(1)函数了。如下所示 实...