[英]Finding and Deleting one line of Excel VBA code
I have an Excel spreadsheet that has several modules with multiple sub procedures.我有一个 Excel 电子表格,它有几个模块和多个子过程。 One module has the multiple procedures and another module Calls each of those procedures.一个模块有多个过程,另一个模块调用这些过程中的每一个。 The problem is that the list of Call Subs will vary.问题是 Call Subs 的列表会有所不同。 I know how to add to the below routine, but I can't figure out how to delete a line, based off of what I enter in a cell in the worksheet.我知道如何添加到下面的例程中,但是根据我在工作表的单元格中输入的内容,我无法弄清楚如何删除一行。 The process would be to enter "Call TMS1707455" in a cell, then run a macro that would delete the value of that cell from the below sub.该过程是在一个单元格中输入“调用 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
Or, would it be better or possible to enter this list of Call Subs in a worksheet column.或者,在工作表列中输入此呼叫潜艇列表会更好还是可能。 I could then modify the list as needed and have a macro that would call all of the procedures based on the column of call subs.然后,我可以根据需要修改列表,并拥有一个宏,该宏将根据 call subs 列调用所有过程。 This way, I wouldn't have to have a macro to add lines and a macro to delete lines.这样,我就不必有一个宏来添加行和一个宏来删除行。 I just can't find any info on how to do either ways.我只是找不到有关如何做任何一种方式的任何信息。
Something like this will help you a lot and I think the example demonstrates many programming principles that will help you write more efficient/manageable/editable code moving forward.这样的事情会对你有很大帮助,我认为这个例子展示了许多编程原则,可以帮助你编写更高效/可管理/可编辑的代码。 Note the comments as well.还要注意评论。
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
I figured out what I needed to do.我想出了我需要做什么。 The below code deletes one line of my macro, depending on the value in a cell.下面的代码根据单元格中的值删除了我的宏的一行。 I found the answer here: http://www.cpearson.com/excel/vbe.aspx It may not be pretty, but it works.我在这里找到了答案: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.