编辑独立代码以接收参数

时间:2016-06-22 13:14:22

标签: excel vba

我有一个查询“)”的查询并将其向下移动,直到它不在任何数据旁边。

Sub SeekParen()

Dim c As Range, wheree As Range
Dim whatt As String
Dim TotalCycle As Long, CounterCycle As Long
whatt = ")"
Set c = Range("A1:A10")

Set wheree = c.Find(what:=whatt, after:=c(1)).Offset(0, 1)
    TotalCycle = Application.WorksheetFunction.CountIf(c, whatt)
For CounterCycle = 1 To TotalCycle
    If wheree.Value <> "" Then
        Range("A2").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Set wheree = c.Find(what:=whatt, after:=c(wheree.Row)).Offset(0, 1)
    Else
        Exit For
    End If
Next CounterCycle

End Sub

我还有一个主查询,它通过所有工作表运行每个查询。

Sub MasterTransformationMacro()

'Dim wb As Workbook
'Set wb = Workbooks("Book2.xlsm")
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    Call SeekParen(ws)
    Call SeekParen(ws)
    Call Move(ws)
    Call DoTrim(ws)
    Call CopyA(ws)
    Call ReplaceDelimit(ws)
    Call Split(ws)
    Call DeleteAllEmptyCRows(ws)
    Call DeleteC(ws)
    Call Formuoli(ws)
    Call InsertSystemPath(ws)
Next

End Sub

我无法弄清楚如何编辑SeekParen以适应MasterMacro。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果您将工作表引用传递给函数,就像这样

Call SeekParen(ws)

并修改SeekParen以接收它,就像这样

Sub SeekParen(ws as Worksheet)

那么您应该能够将传递的工作表引用附加到SeekParen正文中的每个相关行,就像这样

Dim c As Range, wheree As Range
Dim whatt As String
Dim TotalCycle As Long, CounterCycle As Long
whatt = ")"
Set c = ws.Range("A1:A10")

Set wheree = c.Find(what:=whatt, after:=c(1)).Offset(0, 1)
TotalCycle = Application.WorksheetFunction.CountIf(c, whatt)
For CounterCycle = 1 To TotalCycle
    If wheree.Value <> "" Then
        ws.Range("A2").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Set wheree = c.Find(what:=whatt, after:=c(wheree.Row)).Offset(0, 1)
    Else
        Exit For
    End If
Next CounterCycle