【问题标题】:Finding and Deleting one line of Excel VBA code查找并删除一行 Excel VBA 代码
【发布时间】:2020-04-20 20:14:56
【问题描述】:

我有一个 Excel 电子表格,其中包含多个模块和多个子过程。一个模块有多个过程,另一个模块调用这些过程中的每一个。问题是 Call Subs 的列表会有所不同。我知道如何添加到下面的例程中,但是根据我在工作表的单元格中输入的内容,我无法弄清楚如何删除一行。该过程是在一个单元格中输入“Call TMS1707455”,然后运行一个宏,该宏将从下面的子单元中删除该单元格的值。

Sub TMS()

    Call TMS1707455
    Call TMS1367006
    Call TMS4268798
    Call TMS1366994
    Call TMS39522
    Call TMS39523
    Call TMS4482313
    Call TMS19395
    Call TMS39415
    Call TMS37118
End Sub

或者,在工作表列中输入此呼叫替补列表会更好或可能吗?然后,我可以根据需要修改列表,并拥有一个宏,该宏将根据 call subs 列调用所有过程。这样,我就不必有一个宏来添加行和一个宏来删除行。我只是找不到任何关于如何做这两种方式的信息。

【问题讨论】:

  • 所有这些子程序都相似吗?看到类似 sn-p 的东西有点代码味道。第二种情况可以使用Application.Run
  • 好像是XY问题。
  • 我不知道您所说的delete the value of that cell from the below sub 是什么意思,但是您可以保留一个子程序并选择使用IfCase 语句在其中运行任何程序。以If Range("A1").Value = TMS1707455 Then Call TMS1707455 为例。
  • 为了获得有关该问题的想法,我认为最好告诉我们您希望使用该范围的调用来实现什么。根据您完成某事的需要,我们也许可以提供一些想法...
  • @BigBen。子程序类似。下面是其中两个的示例。
    Sub TMS1707455() Cells.Replace What:= _ "1707455(HSPD-12 Sponsor Certification Training)", _ Replacement:="HSPD-12 Sponsor Certification Training" End Sub Sub TMS1367006() Cells.Replace What: = _ "1367006(VA 员工远程办公培训模块)", _ Replacement:="VA 员工远程办公培训模块" End Sub 我将它们拆分,以便在不再需要时更容易删除它们。这些潜艇在一个模块中,然后另一个模块具有所有调用潜艇。

标签: excel vba


【解决方案1】:

这样的事情会对你有很大帮助,我认为这个例子展示了许多编程原则,可以帮助你编写更高效/可管理/可编辑的代码。还要注意 cmets。

Option Explicit

Sub TMS()

    cleanUp "TMS1707455"
    cleanUp "TMS1367006"
    '... etc
    'add / delete comment as needed

End Sub

Sub cleanUp(which As String)

    Dim findIt As String, changeTo As String

    Select Case which

        Case Is = "TMS1707455"
            findIt = "1707455(HSPD-12 Sponsor Certification Training)": changeTo = "HSPD-12 Sponsor Certification Training"
        Case Is = "TMS1367006"
            findIt = "1367006(VA Telework Training Module For Employees)": changeTo = "VA Telework Training Module For Employees"
        'Case Is = ' ... next in line '
        'delete cases as needed, or comment them out

    End Select

    Dim ws As Worksheet
    Set ws = Worksheets("mySheet") 'always explicitly declare and work directly with objects

    ws.Cells.Replace What:=findIt, Replacement:=changeTo
    'i would also suggest only working with the cells with actual data you need,
        'like the columnset or row set, it will speed up the code, especially using Replace

End Sub

【讨论】:

    【解决方案2】:

    我知道我需要做什么。下面的代码根据单元格中的值删除了我的宏的一行。我在这里找到了答案:http://www.cpearson.com/excel/vbe.aspx 它可能不漂亮,但它有效。

        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim FindWhat As String
        Dim SL As Long ' start line
        Dim EL As Long ' end line
        Dim SC As Long ' start column
        Dim EC As Long ' end column
        Dim Found As Boolean
    
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("Module2")
        Set CodeMod = VBComp.CodeModule
    
        FindWhat = [C21]
    
        With CodeMod
            SL = 7
            EL = .CountOfLines
            SC = 1
            EC = 255
            Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                EndLine:=EL, EndColumn:=EC, _
                wholeword:=True, MatchCase:=False, patternsearch:=False)
            If Found = True Then
                .DeleteLines StartLine:=SL
            End If
        End With
    

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 2018-05-04
      • 1970-01-01
      相关资源
      最近更新 更多