从Excel传参数到宏
Parameter transmission from Excel into Macro
我正在 VBA (Excel 2016 32bit + Windows 10 Pro) 中制作一个宏,当 [=52 时触发=] 事件发生。
此宏必须重命名每个新 sheet.
的 Sheet.CodeName 属性
问题是当我 运行 我的宏处于中断模式时,一步一步,它完美地工作。
但是不能在VB编辑器之后重命名Sheet.CodeName属性(VBE ) 已经关了。只是 Sheet.CodeName 属性 是空的,所以宏不知道 sheet 必须重命名.如果我打开 VBE 并且什么都不做,然后再次关闭它,Macro 运行s 就正确了。
接下来,此宏可与 Workbook.Open 和 Workbook.SheetChange[ 一起正常工作=38=] 事件。只有Workbook.NewSheet事件出问题了
请给我一个建议,如果你知道,我如何改进从 Excel 到关闭后的宏的参数传输 VBE.
嗨!
谢谢你愿意帮助我!
代码非常简单。
目标是 - 在消息 window 中正确获取 sh_CodeName 属性 的值
当 VBE 关闭时。
当前代码:
Private Sub Workbook_NewSheet(ByVal sh As Object)
NewSheet sh
End Sub
Option Explicit
Sub NewSheet(ByVal sh As Object)
Dim sh_CodeName As String, sh_Name As String, i As Integer
sh_CodeName = sh.CodeName: sh_Name = sh.Name
MsgBox "Code Name - " & sh_CodeName & vbCrLf & "Name - " & sh_Name, vbOKOnly, "MESSAGE FROM WorkBook.NewSheet"
End Sub
似乎sh.codename
在添加新的sheet后不可用,只要没有访问VBA项目。也许其他人对此有更多了解。
此代码适用于我,但缺点是您需要 trust access to the VBA project object model。
Sub NewSheet(ByVal Sh As Object)
Dim sh_CodeName As String, sh_Name As String, i As Integer
sh_CodeName = ActiveWorkbook.VBProject.VBComponents(Sh.Name).Properties("_Codename")
sh_Name = Sh.Name
MsgBox "Code Name - " & sh_CodeName & vbCrLf & "Name - " & sh_Name, vbOKOnly, "MESSAGE FROM WorkBook.NewSheet"
End Sub
当 运行 关闭 VBE
时,即使这个简单的宏也不会给出代号
Sub TestAddIt()
Dim sh As Worksheet
Set sh = ActiveWorkbook.Worksheets.Add
MsgBox sh.Name & " - " & sh.CodeName
End Sub
PS:另一种解决方法是打开 VBE 并再次关闭它,但您仍然需要访问 VBA 项目
Sub TestAddIt()
Dim Sh As Worksheet
Set Sh = ActiveWorkbook.Worksheets.Add
With Application.VBE.MainWindow
.WindowState = 1
.Visible = True
.Visible = False
End With
MsgBox Sh.Name & " - " & Sh.CodeName
End Sub
更新:这对我也有用。
Sub TestAddIt()
Dim Sh As Worksheet
Set Sh = ActiveWorkbook.Worksheets.Add
' Recompile the project
Application.VBE.CommandBars.FindControl(ID:=578).Execute
MsgBox Sh.Name & " - " & Sh.CodeName
End Sub
你帮我做这个声明。
sh_CodeName = ActiveWorkbook.VBProject.VBComponents(Sh.Name).Properties("_Codename")
我稍微修改了一下(Sh.CodeName 而不是 Sh.Name)。并添加了延迟。
Sub Check_NewSheet(ByVal sh As Object)
Dim sh_CodeName As String, sh_Name As String, i As Integer
On Error Resume Next
sh_CodeName = ThisWorkbook.VBProject.VBComponents(sh.CodeName).Properties("_Codename")
If Err.number > 0 Then
i = 0
Do While sh_CodeName = ""
sh_CodeName = ThisWorkbook.VBProject.VBComponents(sh.CodeName).Properties("_Codename")
DoEvents
i = i + 1
Loop
End If
sh_Name = sh.Name
MsgBox "Code Name - " & sh_CodeName & vbCrLf & "Name - " & sh_Name & vbCrLf & _
"Attempts - " & i, vbOKOnly, "MESSAGE FROM WorkBook.NewSheet"
End Sub
现在效果很好。这是一个很好的临时解决方案。
但我仍然有兴趣找出 Excel 与宏通信中错误的解释。
所以,如果你找到一个,请与我分享。
我正在 VBA (Excel 2016 32bit + Windows 10 Pro) 中制作一个宏,当 [=52 时触发=] 事件发生。 此宏必须重命名每个新 sheet.
的 Sheet.CodeName 属性问题是当我 运行 我的宏处于中断模式时,一步一步,它完美地工作。 但是不能在VB编辑器之后重命名Sheet.CodeName属性(VBE ) 已经关了。只是 Sheet.CodeName 属性 是空的,所以宏不知道 sheet 必须重命名.如果我打开 VBE 并且什么都不做,然后再次关闭它,Macro 运行s 就正确了。 接下来,此宏可与 Workbook.Open 和 Workbook.SheetChange[ 一起正常工作=38=] 事件。只有Workbook.NewSheet事件出问题了
请给我一个建议,如果你知道,我如何改进从 Excel 到关闭后的宏的参数传输 VBE.
嗨!
谢谢你愿意帮助我! 代码非常简单。 目标是 - 在消息 window 中正确获取 sh_CodeName 属性 的值 当 VBE 关闭时。
当前代码:
Private Sub Workbook_NewSheet(ByVal sh As Object)
NewSheet sh
End Sub
Option Explicit
Sub NewSheet(ByVal sh As Object)
Dim sh_CodeName As String, sh_Name As String, i As Integer
sh_CodeName = sh.CodeName: sh_Name = sh.Name
MsgBox "Code Name - " & sh_CodeName & vbCrLf & "Name - " & sh_Name, vbOKOnly, "MESSAGE FROM WorkBook.NewSheet"
End Sub
似乎sh.codename
在添加新的sheet后不可用,只要没有访问VBA项目。也许其他人对此有更多了解。
此代码适用于我,但缺点是您需要 trust access to the VBA project object model。
Sub NewSheet(ByVal Sh As Object)
Dim sh_CodeName As String, sh_Name As String, i As Integer
sh_CodeName = ActiveWorkbook.VBProject.VBComponents(Sh.Name).Properties("_Codename")
sh_Name = Sh.Name
MsgBox "Code Name - " & sh_CodeName & vbCrLf & "Name - " & sh_Name, vbOKOnly, "MESSAGE FROM WorkBook.NewSheet"
End Sub
当 运行 关闭 VBE
时,即使这个简单的宏也不会给出代号Sub TestAddIt()
Dim sh As Worksheet
Set sh = ActiveWorkbook.Worksheets.Add
MsgBox sh.Name & " - " & sh.CodeName
End Sub
PS:另一种解决方法是打开 VBE 并再次关闭它,但您仍然需要访问 VBA 项目
Sub TestAddIt()
Dim Sh As Worksheet
Set Sh = ActiveWorkbook.Worksheets.Add
With Application.VBE.MainWindow
.WindowState = 1
.Visible = True
.Visible = False
End With
MsgBox Sh.Name & " - " & Sh.CodeName
End Sub
更新:这对我也有用。
Sub TestAddIt()
Dim Sh As Worksheet
Set Sh = ActiveWorkbook.Worksheets.Add
' Recompile the project
Application.VBE.CommandBars.FindControl(ID:=578).Execute
MsgBox Sh.Name & " - " & Sh.CodeName
End Sub
你帮我做这个声明。
sh_CodeName = ActiveWorkbook.VBProject.VBComponents(Sh.Name).Properties("_Codename")
我稍微修改了一下(Sh.CodeName 而不是 Sh.Name)。并添加了延迟。
Sub Check_NewSheet(ByVal sh As Object)
Dim sh_CodeName As String, sh_Name As String, i As Integer
On Error Resume Next
sh_CodeName = ThisWorkbook.VBProject.VBComponents(sh.CodeName).Properties("_Codename")
If Err.number > 0 Then
i = 0
Do While sh_CodeName = ""
sh_CodeName = ThisWorkbook.VBProject.VBComponents(sh.CodeName).Properties("_Codename")
DoEvents
i = i + 1
Loop
End If
sh_Name = sh.Name
MsgBox "Code Name - " & sh_CodeName & vbCrLf & "Name - " & sh_Name & vbCrLf & _
"Attempts - " & i, vbOKOnly, "MESSAGE FROM WorkBook.NewSheet"
End Sub
现在效果很好。这是一个很好的临时解决方案。 但我仍然有兴趣找出 Excel 与宏通信中错误的解释。 所以,如果你找到一个,请与我分享。