返回值的VBA Sub

时间:2017-06-27 06:16:53

标签: excel excel-vba vba

如果这听起来像一个非常愚蠢的问题,我很抱歉,但我不熟悉使用Excel和VBA并且花了更多时间使用其他语言。我发现我正在编写更强大的代码,并且我一直在编写相同的代码块来反复做同样的事情。有没有办法我可以调用这段代码并获取我正在寻找的值并将其存储为我想要的变量,然后在不同的子中使用它。

希望这个例子能让它更加清晰。我想从A1开始搜索第1行中的所有值,直到有空值。我想在其中找到值为“Frequency”的单元格,然后返回列索引号。

   Sub findFrequency()
        'find "Frequency" colum and save the column index number
        Dim fFreq As String
        Dim FreqCol As Variant
        Range("A1").Select
        fFreq = "Frequency"
        Do Until IsEmpty(ActiveCell)
            If ActiveCell.Value = fFreq Then
                FreqCol = ActiveCell.Column
                Exit Do
            End If
        ActiveCell.Offset(0, 1).Select
        Loop
        End Sub

理想情况下,我可以写一个不同的并使用上面代码中的值

Sub Execute()
Call findFrequency
Cells(5, FreqCol).Select
With Selection.Interior     
     .Pattern = xlSolid
     .PatternColorIndex = xlAutomatic
     .ThemeColor = xlThemeColorAccent5
     .TintAndShade = 0.599993896298105
     .PatternTintAndShade = 0
End With
End Sub

我运行时遇到错误,因为FreqCol的值没有设置为运行Call findFrequency行的任何内容。有没有更好的方法来做到这一点,还是我应该采取不同的方式?

4 个答案:

答案 0 :(得分:1)

试试这个:

Function findFrequency()
    'find "Frequency" colum and save the column index number
    Dim fFreq As String
    Dim FreqCol As Variant
    Range("A1").Select
    fFreq = "Frequency"
    Do Until IsEmpty(ActiveCell)
        If ActiveCell.Value = fFreq Then
            findFrequency = ActiveCell.Column
            Exit Do
        End If
    ActiveCell.Offset(0, 1).Select
    Loop
End Function

Sub Execute()
Dim FreqCol As Long
FreqCol = findFrequency()
Cells(5, FreqCol).Select
With Selection.Interior     
     .Pattern = xlSolid
     .PatternColorIndex = xlAutomatic
     .ThemeColor = xlThemeColorAccent5
     .TintAndShade = 0.599993896298105
     .PatternTintAndShade = 0
End With
End Sub

答案 1 :(得分:1)

为什么循环或选择一个单元格来查找值?只需使用.Find

即可
Function findFrequency() As Long
    Dim ws As Worksheet
    Dim aCell As Range

    Set ws = ActiveSheet

    With ws
        Set aCell = .Columns(1).Find(What:="Frequency", LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        If Not aCell Is Nothing Then findFrequency = aCell.Column
    End With
End Function

如果找不到“频率”,会发生什么。你也需要迎合这一点。

Sub Execute()
    Dim FreqCol As Long

    FreqCol = findFrequency

    If FreqCol = 0 Then 
        MsgBox "Frequency not found"
        Exit Sub
    End If

    With Cells(5, FreqCol).Interior
         .Pattern = xlSolid
         .PatternColorIndex = xlAutomatic
         .ThemeColor = xlThemeColorAccent5
         .TintAndShade = 0.599993896298105
         .PatternTintAndShade = 0
    End With
End Sub

答案 2 :(得分:0)

代码就是这样。

Main Main sub是findFrequency,subprocedure是Sub Execute(rng As Range)

Sub findFrequency()
        'find "Frequency" colum and save the column index number
        Dim fFreq As String
        Dim FreqCol As Variant
        Range("A1").Select
        fFreq = "Frequency"
        Do Until IsEmpty(ActiveCell)
            If ActiveCell.Value = fFreq Then
                FreqCol = ActiveCell.Column
                Execute Cells(5, FreqCol)
                Exit Do
            End If
        ActiveCell.Offset(0, 1).Select
        Loop
End Sub
Sub Execute(rng As Range)

With rng.Interior
     .Pattern = xlSolid
     .PatternColorIndex = xlAutomatic
     .ThemeColor = xlThemeColorAccent5
     .TintAndShade = 0.599993896298105
     .PatternTintAndShade = 0
End With
End Sub

答案 3 :(得分:0)

让我告诉你,你不应该因为问一个关于 VBA 的基本问题而道歉:没有人值得花时间在 VBA 上,我很高兴你更熟悉其他编程语言。

我为所有不熟悉 VBA 的人写了我的答案。

VBA 默认通过引用传递参数,因此您可以这样编写代码:

Sub findFrequency(FreqCol as Long)
...
End SUb 

Sub Execute()
    Dim FreqCol As Long
    findFrequency FreqCol 
    ... 
End SUb