[英]Get a worksheet name using Excel VBA
I would like to create an user-defined
function in Excel
that can return the current worksheet.我想在
Excel
中创建一个可以返回当前工作表的user-defined
函数。 I could use the我可以使用
sheetname = ActiveSheet.Name
But the problem with this is, it works and suddenly it starts to get different sheet name.但问题是,它起作用了,突然间它开始得到不同的工作表名称。 For example, instead of
SHEET I LOVE YOU
it returns SHEET I HATE YOU
.例如,它返回
SHEET I HATE YOU
而不是SHEET I LOVE YOU
SHEET I HATE YOU
。
Is there anyway to fix this - or it might possible because I think it can not be static but varies?有没有办法解决这个问题 - 或者有可能因为我认为它不能是静态的而是变化的?
Function MySheet()
' uncomment the below line to make it Volatile
'Application.Volatile
MySheet = Application.Caller.Worksheet.Name
End Function
This should be the function you are looking for这应该是您正在寻找的功能
这对我有用。
worksheetName = ActiveSheet.Name
Sub FnGetSheetsName()
Dim mainworkBook As Workbook
Set mainworkBook = ActiveWorkbook
For i = 1 To mainworkBook.Sheets.Count
'Either we can put all names in an array , here we are printing all the names in Sheet 2
mainworkBook.Sheets("Sheet2").Range("A" & i) = mainworkBook.Sheets(i).Name
Next i
End Sub
Extend Code for Show Selected Sheet(s) [ one or more sheets].扩展用于显示选定工作表的代码 [一张或多张纸]。
Sub Show_SelectSheet()
For Each xSheet In ThisWorkbook.Worksheets
For Each xSelectSheet In ActiveWindow.SelectedSheets
If xSheet.Name = xSelectSheet.Name Then
'=== Show Selected Sheet ===
GoTo xNext_SelectSheet
End If
Next xSelectSheet
xSheet.Visible = False
xNext_SelectSheet:
Next xSheet
MsgBox "Show Selected Sheet(s) Completed !!!"
end sub
You can use below code to get the Active Sheet name and change it to yours preferred name.您可以使用以下代码获取活动工作表名称并将其更改为您的首选名称。
Sub ChangeSheetName()
Dim shName As String
Dim currentName As String
currentName = ActiveSheet.Name
shName = InputBox("What name you want to give for your sheet")
ThisWorkbook.Sheets(currentName).Name = shName
End Sub
i need to change the sheet name by the name of the file was opened我需要通过打开文件的名称来更改工作表名称
Sub Get_Data_From_File5()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim currentName As String
currentName = ActiveSheet.Name
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.csv*),*csv*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("A1:g5000").Copy
ThisWorkbook.Worksheets(currentName).Range("Aw1:bc5000").PasteSpecial xlPasteValues
OpenBook.Close False
End If
Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.