【问题标题】:Excel VBA Class Property returning a public long variable, but not a public string variableExcel VBA 类属性返回公共长变量,但不是公共字符串变量
【发布时间】:2014-06-11 15:49:35
【问题描述】:

这是我第一次使用面向对象的方法:希望对我有所帮助。 (发现了这个,这似乎很有帮助,但我还是没看到:Excel Class properties returning empty strings

首先:我想要什么?用于导入表并具有对其中某些列进行排序和过滤(以及更多操作)的方法的类。列的位置(如果不是名称)可能会不时发生变化。

第二:这是我的课:(摘录) 显式选项

Private ColNumber_ As Long
Private ColTxt_ As String
Private blnVis As Boolean
Private blnSort As Boolean
Private sCrit As String

Private Sub Class_Initialize()
   blnVis = True
   blnSort = False
   ColTxt_ = ""
End Sub

Public Sub initCols(ColTxt_ As String)
   Dim rng As Range
   ActiveWorkbook.Worksheets("Glasliste").Range("Tabelle1[#Headers]").Select
   Set rng = Selection.Find(What:=ColTxt_, LookIn:=xlFormulas, _
     LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
     MatchCase:=False, SearchFormat:=False) 'After:=ActiveCell,
   MsgBox rng.Text ' This gives me the desired text, however I cannot use it outside of the class
   MsgBox rng.Column
   ColNumber_ = rng.Column
   ColTxt_ = rng.Text
End Sub

'Get the text of the header
Public Property Get ColTxt() As String 'Optional lngOrder As Long
   ColTxt = ColTxt_
End Property

'Get the position of the Column
Public Property Get ColNumber() As Long 'Optional lngOrder As Long
   ColNumber = ColNumber_
End Property

第三:这是我的代码(摘录):

Sub initSpalten()
   Dim Typ As clsSpalte: Set Typ = New clsSpalte: Typ.initCols ("Typ")
   MsgBox Typ.ColTxt ' Here I get an empty string
   Dim Aufbau As clsSpalte: Set Aufbau = New clsSpalte: Aufbau.initCols ("Aufbau")
   MsgBox Aufbau.ColNumber ' I here get the right number... but dont see any difference?
End Sub

第四:我的问题,或者我不明白的地方: 使用上面的 MsgBox,评论“这里我得到一个空字符串”我的代码稍后将不起作用。我确实需要这段文字。我无法直接分配相应的私有变量。但是,为什么它适用于长 "Aufbau.ColNumber" ? 当然这对你们中的一些人来说不是一个谜? (希望)......非常感谢任何帮助! 托马斯

【问题讨论】:

    标签: oop excel vba


    【解决方案1】:

    您的ColTxt_ 变量正在“重影”同名的类成员变量。尝试改变:

    '                   vvv
    Public Sub initCols(txt As String)
       Dim rng As Range
       ActiveWorkbook.Worksheets("Glasliste").Range("Tabelle1[#Headers]").Select
    '                                 vvv 
       Set rng = Selection.Find(What:=txt, LookIn:=xlFormulas, _
         LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
         MatchCase:=False, SearchFormat:=False) 'After:=ActiveCell,
       MsgBox rng.Text ' This gives me the desired text, however I cannot use it outside of the class
       MsgBox rng.Column
       ColNumber_ = rng.Column
       ColTxt_ = rng.Text
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 2015-10-27
      • 2019-06-17
      • 2014-03-15
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2012-10-10
      相关资源
      最近更新 更多