2017-06-27 75 views
1

如果这听起来像一个非常愚蠢的问题,但我对使用Excel和VBA并花费了更多时间处理其他语言,我很抱歉。我发现我编写的代码更加健壮,我一直在编写相同的代码块来一遍又一遍地做同样的事情。有没有一种方法可以调用这段代码,并获取我正在查找的值并将其存储为我想要的变量,然后将其用于不同的子类中。VBA Sub返回一个值

希望这个例子能让它更加清晰。我想从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的值不从运行设置为任何调用findFrequency线。有没有更好的方法来做到这一点,或者我应该有不同的结构?

+0

'Sub'不能返回一个值(但它可以将值分配给一个全局变量)。你在找什么是'Function'。 –

回答

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

为什么循环或选择一个细胞找到了价值?只需使用.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 
+1

2小时@Siddharth击败我。我正在考虑将'Find'答案作为模板,以便我可以复制/粘贴并在不到一秒内回答。 :) –

+0

那么我已经准备好了[这里](http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/):D –

+0

是的,有看看你的网站。我保留自己的意思,但我还没有任何新的东西可以加入到混音中。 –

0

代码会是这样的。

主要主子是findFrequency,子过程子执行(RNG由于范围)

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