2017-06-27 100 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