excel - Get name of the file from which the button was pressed on the toolbar
问题描述
I have a macro in the personal.xlsb file to format current sheet. That macro is used in 2 ways
- by pressing a button on the toolbar from any open file
- by being called from another sub/procedure
I'm trying to write the macro, without using SELECT, ACTIVATE, PASTE, etc. (so, by declaring all workbooks and sheets and using WITH...). To do that, I have to know the name of the file that called the macro and it has to work for both being called from the button and from a procedure.
The code below is not fully functional because it's not switching between the two workbooks. I didn't finish coding because I realized that I don't know how to get the file and sheet name.
Option Explicit
Public Sub FormatTheBasics()
Dim CurLastColumn As Long, CurLastRow As Long
Dim CurRowNum As Long, LastRow As Long, FirstRowOfSection As Long, LastRowOfSection As Long
Dim CurCell As Variant, CurRange As Range
Dim wbkM As Workbook, wbkC As Workbook
Dim wksReplaceWords As Worksheet, wksFilesToExportEMail As Worksheet, wksCopyFrom As Worksheet, wksCopyTo As Worksheet
Dim rngCopyFrom As Range, rngCopyTo As Range
Dim x As Long
Dim CurColumnLetter As String, CurColumnName As String, ReplaceFrom As String, ReplaceTo As String
Application.EnableCancelKey = xlDisabled
Set wbkM = Workbooks("Personal.xlsb")
Set wksReplaceWords = wbkM.Sheets("ReplaceWords")
Cells.Select
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("A1:" & MyColumnLetter(xlLastCol) & "1").Select
Selection.Font.Bold = True
With Selection.Interior
.PatternColorIndex = 2
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Selection.AutoFilter
Rows("2:2").Select
ActiveWindow.FreezePanes = True
For x = 1 To xlLastCol
CurColumnLetter = MyColumnLetter(x)
CurColumnName = StrConv(Range(CurColumnLetter & "1").Value, vbLowerCase)
Range(CurColumnLetter & "1").Value = StrConv(CurColumnName, vbProperCase)
CurRowNum = 2
With wksReplaceWords
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set CurRange = .Range("C" & CurRowNum & ":C" & LastRow)
For Each CurCell In CurRange
If CurCell <> "" Then
ReplaceFrom = .Range("B" & CurRowNum).Value
ReplaceTo = CurCell
If InStr(1, CurColumnName, ReplaceFrom, vbTextCompare) > 0 Then
Range(CurColumnLetter & "1").Replace what:=ReplaceFrom, replacement:=ReplaceTo, MatchCase:=True
End If
End If
Next CurCell
End With
Next x
Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:" & MyColumnLetter(xlLastCol)).Sort key1:=Range("A2"), order1:=xlAscending, Header:=xlYes
Range("A2").Select
End Sub
解决方案
Within a VBA function, there is no means to find out where and why execution was started. To put it in programmer-speak, there is no way of introspecting call stack and execution context.
So how to solve this? Your best bet is to use ActiveSheet
or ActiveWorkbook
once only, assuming that the target workbooks' window was in front when the function was triggered. Store it into a variable and from then on use the Variable:
Public Sub DoStuff():
Dim ws as Worksheet
Set ws = ActiveSheet
ws.Range("A1").value = "Hello World"
' ...
End Sub
This way, if you find flaws in your sheet-detection logic, you just need to improve the code right at the top.
推荐阅读
- css - 文本元素不适合 div 父级 [Oxygen Builder / Wordpress]
- python - 在 conda 中复制 colab env
- c++ - 如何使用自己的 operator= 函数将“X”类中的 operator= 调用到“Y”类中?
- python - python-docx包无法读取图像文件
- css - 在此上下文中,XHTML 元素不允许作为 XHTML 元素主体的子元素
- vue.js -
Vue路由器@click事件 - java - 如何在 Java 中实现可靠的单实例应用程序?
- python - 方法与属性
- r - 如何从R中for循环的迭代结果中获取向量?
- django - 条纹支付 - 为什么当我打电话 if payment_form.is_valid(): 总是无效?