我想不出什么比正好包含一个 子单个模块,其中单元值对被输入就像正常的变量赋值简单:
'===============================================================================
' Module : NewbieProof
' Version : 1.0
' Part : 1 of 3
' References : N/A
' Online : https://stackoverflow.com/a/46068523/1961728
'===============================================================================
Sub SuperNewieProofData()
' Fill this list with your desired values in the format:
' Cell = Value (Remove the existing lines)
C5 = "Hello"
D6 = "World"
E2 = 23.45
End Sub
启用通过VBA IDE对象本身,这个成功使用的子需要一点魔力。认为自我修改的代码。在这种情况下,只有的代码读取来自NewbieProof模块的子部分,提取单元值对。
这片神奇的被封装在辅助功能TheNewbieDict()
返回完全填充的词典:
'===============================================================================
' Module : <in any standard module>
' Version : 1.0
' Part : 2 of 3
' References : Microsoft Visual Basic For Applications Extensibility 5.3
' Online : https://stackoverflow.com/a/46068523/1961728
'===============================================================================
Private Const l_Error As String = "Error"
Function TheNewbieDict() As Object
Const l_NewbieProof As String = "NewbieProof"
Dim e_Proc As VBIDE.vbext_ProcKind: e_Proc = VBIDE.vbext_ProcKind.vbext_pk_Proc
Dim vbprojThis As VBIDE.VBProject
Dim codeNewbieProof As VBIDE.CodeModule
Dim strProcName As String
Dim lngLineNumber As Long
Dim strCurrentLine As String
Dim strNewbieCell As String
Dim strNewbieValue As String
' Add reference to "Microsoft Visual Basic For Applications Extensibility 5.3"
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromGuid GUID:="{0002E157-0000-0000-C000-000000000046}", Major:=5, Minor:=3
On Error GoTo 0
Set TheNewbieDict = CreateObject("Scripting.Dictionary")
Set vbprojThis = ActiveWorkbook.VBProject
On Error Resume Next: Set codeNewbieProof = vbprojThis.VBComponents(l_NewbieProof).CodeModule: On Error GoTo 0
If codeNewbieProof Is Nothing Then
TheNewbieDict.Add l_Error, 1&
Exit Function
End If
With codeNewbieProof
If .CountOfLines = .CountOfDeclarationLines Then
TheNewbieDict.Add l_Error, 2&
Exit Function
End If
strProcName = .ProcOfLine(.CountOfDeclarationLines + 1, e_Proc)
lngLineNumber = .ProcBodyLine(strProcName, e_Proc)
Do Until lngLineNumber >= .CountOfLines: Do
lngLineNumber = lngLineNumber + 1
strCurrentLine = .Lines(lngLineNumber, 1)
' Skip comment and empty lines
If Left$(Trim(strCurrentLine), 1) & "'" Like "'*" Then Exit Do
' Skip non-assignment lines ("Function …" and "End Function" lines)
If Not strCurrentLine Like "*=*" Then Exit Do
' Extract the Cell-Value pair from the line
strNewbieCell = Trim(Replace(Left$(strCurrentLine, InStr(strCurrentLine, "=") - 1), """", ""))
strNewbieValue = Trim(Replace(Mid$(strCurrentLine, InStr(strCurrentLine, "=") + 1), """", ""))
If Not TheNewbieDict.Exists(strNewbieCell) Then
TheNewbieDict.Add strNewbieCell, strNewbieValue
End If
Loop While 0: Loop
If TheNewbieDict.Count = 0 Then
TheNewbieDict.Add l_Error, 3&
Exit Function
End If
End With
End Function
这是你会怎么称呼它:
'===============================================================================
' Module : <in any standard module>
' Version : 1.0
' Part : 3 of 3
' References : N/A
' Online : https://stackoverflow.com/a/
'===============================================================================
Sub NOT_NewbieProofSub()
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Set dict = TheNewbieDict()
If dict.Exists(l_Error) Then
' Error creating dictionary - Some newbie deleted/renamed/cleared
' or otherwise messed with the NewbieProof code module.
MsgBox _
"Oops! Not so newbie-proof!" & vbCrLf & vbCrLf _
& "Looks like some Newbie " _
& Choose(dict("Error"), "renamed or delete", "deleted the sub in", "deleted the data from") _
& " the NewbieProof code module." & vbCrLf & vbCrLf _
& "Please contact your local Code Guru." _
, vbCritical
Exit Sub
End If
'…
End Sub
如果你想把所有的东西放在一个模块中,使用相同的技术,你可以在下面放置该模块的最顶部,并将它自动加载到电子表格:
' Fill this list with your desired values in the format:
' "'Cell = Value" (Remove the existing lines)
'C5 = "Hello"
'D6 = "World"
'E2 = 23.45
交易破坏者:
特点:
配置:
的NewbieProof
模块名称是多变的,但必须与当地l_NewbieProof
不断进行配对;
该SuperNewieProofData
子名是多变的没有任何影响;
NewbieProof
模块头是完全可移除的;
如果需要,编程添加Microsoft Visual Basic For Applications Extensibility 5.3
引用,因为所有VBIDE对象访问都是早期绑定的。这可以根据您的要求进行更改。
注意:如果您想了解我的变量命名约定,它是基于RVBA。
您是否希望用户在单元格C5中键入内容并在D5中显示单词hello?只是想知道如果简单是你的目标,那么它可以以不同的方式完成吗?你当然可以将字典项目放在另一张纸上,然后直接从那里加载下一个循环,但我猜测这可能不会让阅读非程序员更容易。 – perfo
另外你知道你可以添加东西像这样的字典词典(“F2”)=“打嗝”这可能会更容易阅读? – perfo
我忘了添加行如何从工作表加载列表dict.add键:=(范围(“A1”)),项目:=范围(“B1”),你不需要下一个循环.. – perfo