Excel 将sub转换为带参数的sub

Excel 将sub转换为带参数的sub,excel,vba,Excel,Vba,VBA新手,我写的一个sub有点问题 此子项从各个列中获取值,并将这些值放入字典,然后在另一个列中打印字典 Sub Unitario() Dim Dict As Object Dim bRiga As Long Dim aRiga As Long Dim cRiga As Long Dim dRiga As Long Dim I As Long Dim MyString As String Dim arr Set Dict =

VBA新手,我写的一个sub有点问题

此子项从各个列中获取值,并将这些值放入字典,然后在另一个列中打印字典

Sub Unitario()
    Dim Dict As Object
    Dim bRiga As Long
    Dim aRiga As Long
    Dim cRiga As Long
    Dim dRiga As Long
    Dim I As Long
    Dim MyString As String
    Dim arr


Set Dict = CreateObject("Scripting.Dictionary")
Dict.CompareMode = vbTextCompare 'compare without distinction between capitals
'while vbBinaryCompare distinguish between capitals


ThisWorkbook.Worksheets("Foglio2").Range("c1").EntireColumn.Clear


aRiga = Sheets("Lavoro").Cells(Rows.Count, "M").End(xlUp).Row
bRiga = Sheets("Lavoro").Cells(Rows.Count, "N").End(xlUp).Row
cRiga = Sheets("Lavoro").Cells(Rows.Count, "O").End(xlUp).Row
dRiga = Sheets("Lavoro").Cells(Rows.Count, "P").End(xlUp).Row

For I = 4 To aRiga
    MyString = Sheets("Lavoro").Cells(I, "M")
    'to change coloumn i need to change values up there
    If Not Dict.exists(MyString) Then
        Dict.Add MyString, MyString
    End If
Next I
'adds coloumns value to dictionary

For I = 4 To bRiga
    MyString = Sheets("Lavoro").Cells(I, "N")
    'to change coloumn i need to change values up there
    If Not Dict.exists(MyString) Then
        Dict.Add MyString, MyString
    End If
Next I
    'adds coloumns value to dictionary

For I = 4 To cRiga
    MyString = Sheets("Lavoro").Cells(I, "O")
    'to change coloumn i need to change values up there
    If Not Dict.exists(MyString) Then
        Dict.Add MyString, MyString
    End If
Next I
'adds coloumns value to dictionary

    For I = 4 To dRiga
    MyString = Sheets("Lavoro").Cells(I, "P")
    'to change coloumn i need to change values up there
    If Not Dict.exists(MyString) Then
        Dict.Add MyString, MyString
    End If
Next I
'adds coloumns value to dictionary

arr = Dict.Items

Worksheets("Foglio2").Range("c1").Resize(Dict.Count, 1).Value = Application.Transpose(arr)
End Sub
很明显,此sub没有优化,因为我必须在任何时候手动更改sub中的值,以便将其用于其他范围

我要做的是制作一个sub,它可以通过按钮中的各种范围参数调用,而不必使用不同的范围编写100次相同的宏。 因此,我可以简单地编写如下内容,而不是手动修改代码:

    Private sub Commandbutton1_Click
    Unitario(OutputSheet,OutputCell,InputRange1,InputRange2,..., InputRangeN)
    End Sub
因此,我在excel上只有一个宏和具有不同参数的各种按钮

你能帮我吗?

它可以像下面这样:

Sub Unitario(strFirstCol作为字符串,strSecondCol作为字符串,strThirdCol作为字符串,strforthcol作为字符串)

然后你必须采用下面的部分

aRiga = Sheets("Lavoro").Cells(Rows.Count, strFirstCol).End(xlUp).Row
bRiga = Sheets("Lavoro").Cells(Rows.Count, strSecondCol).End(xlUp).Row
cRiga = Sheets("Lavoro").Cells(Rows.Count, strThirdCol).End(xlUp).Row
dRiga = Sheets("Lavoro").Cells(Rows.Count, strFourthCol).End(xlUp).Row
在每个“For循环”内:

然后打电话给潜艇

调用Unitario(“M”、“N”、“O”、“p”)
它可以如下所示:

Sub Unitario(strFirstCol作为字符串,strSecondCol作为字符串,strThirdCol作为字符串,strforthcol作为字符串)

然后你必须采用下面的部分

aRiga = Sheets("Lavoro").Cells(Rows.Count, strFirstCol).End(xlUp).Row
bRiga = Sheets("Lavoro").Cells(Rows.Count, strSecondCol).End(xlUp).Row
cRiga = Sheets("Lavoro").Cells(Rows.Count, strThirdCol).End(xlUp).Row
dRiga = Sheets("Lavoro").Cells(Rows.Count, strFourthCol).End(xlUp).Row
在每个“For循环”内:

然后打电话给潜艇


调用Unitario(“M”、“N”、“O”、“p”)

每当我必须向常用的子函数或函数添加参数时,我只需添加带有“optionaĺ”的参数

这样我就不必对潜艇的每一次呼叫都重新编码

范例

Public sub test (byval optional addr as string)

每当我必须向常用的子函数或函数添加参数时,我只需添加带有“optionaĺ”的参数

这样我就不必对潜艇的每一次呼叫都重新编码

范例

Public sub test (byval optional addr as string)
使用call关键字执行sub。使用call关键字执行sub。