2014-07-17 90 views
0

我的東西嘗試嘗試:Excel VBA中使用函數和數組

沒有與人的名單,我想做些什麼,是讀取數組單元值(這部分作品),比對工作表中的每個單元格執行檢查,並且如果給定的單元格與數組中的字符串相同,則執行一些操作。

但不幸的是,我得到了「類型不匹配」的錯誤。

詩篇。我知道這沒有多大意義,我可以在服務器功能裏面找到一些東西,但是相信我,我有我的理由。 :-)

編輯:固定的幾件事情,現在它看起來像這樣(現在我得到的對象不支持方法的該屬性)

如果有幫助,你也可以嘗試一下。你只需要添加一個名爲「服務器」的單元,然後在它下面寫一些隨機的單詞。現在它應該在的msgbox 「OK」 x次,其中x是你寫的行數寫在細胞下,命名爲 「服務器」

'server name 
Function server(ByVal issrvname As String) 
Dim j As Integer 
Dim c As Range 
Dim x As Integer, y As Integer 

For Each c In Sheets("Topology").UsedRange.Cells 

Dim srvname() As String 
j = 0 
    If c.Cells.Value = "Servers" Then 
    y = c.Column: x = c.Row + 1 
     Do Until IsEmpty(Cells(x, y)) 
     ReDim Preserve srvname(0 To j) As String 
     srvname(j) = Cells(x, y).Value 
     x = x + 1 
     j = j + 1 
     Loop 
    End If 
Next c 

For Each c In Sheets("Topology").UsedRange.Cells 
    If IsInArray(c.Cell.Value, srvname) Then 
     issrvname = True 
    Else 
     issrvname = False 
    End If 
Next c 

End Function 
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean 
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) 
End Function 
Sub test() 


Dim c As Range 

For Each c In Sheets("Topology").UsedRange.Cells 

    If server(c) = True Then 
    MsgBox "ok" 
    End If 

Next c 

End Sub 
+0

請突出顯示錯誤出現的行 – hnk

+0

它現在顯示出來,當我運行子測試()時出現msgbox說:運行時錯誤13:類型不匹配 – Divin3

+0

「Do」後,添加「調試。打印x,y'。在'For Each cell'後面添加'Debug.Print cell.address'。這將縮小它的範圍。然後,您可以在問題區域和單週期(F8)「停止」。請參閱http://www.cpearson.com/excel/DebuggingVBA.aspx – dcromley

回答

2

我認爲你可以將您的功能:

首先,你需要包括你的陣列生成塊到主子。
包括它在函數,因爲它需要產生在服務器功能

EDIT1的每個呼叫陣列服務器減慢執行代碼:這是試圖現在測試。我已經重新編寫了你的​​功能並改進了你的子功能。

Sub test() 
    Dim j As Integer 
    Dim c As Range, c1 As Range 
    Dim x As Integer, y As Integer 
    Dim i As Long '~~> added it just to check how many is shown in MsgBox 

    For Each c In Sheets("Topology").UsedRange.Cells 
     '~~> generate array if "Servers" is encountered 
     If c.Value = "Servers" Then 
      Dim srvname() As String 
      j = 0 
      y = c.Column: x = c.Row + 1 
      With Sheets("Topology").UsedRange 
       Do Until IsEmpty(.Cells(x, y)) 
        ReDim Preserve srvname(j) 
        srvname(j) = .Cells(x, y).Value 
        x = x + 1 
        j = j + 1 
       Loop 
      End With 
      '~~> use the generated Array of values here 
      i = 1 
      For Each c1 In Sheets("Topology").UsedRange.Cells 
       If IsInArray(c1.Value, srvname) Then 
        MsgBox "ok" & i 
        i = i + 1 
       End If 
      Next c1 
     End If 
    Next c 
End Sub 

這裏的新功能:(實際上,你不需要它,你可以直接在主分撥打匹配功能)

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean 
    IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0)) 
End Function 

也許你這樣做只是供測試用?我只是認爲用於生成陣列的工作表必須與要比較服務器名稱的工作表不同。

+0

這個屬性,你是一個天才,還有一些其他的問題與腳本。我盡力讓它工作儘快 – Divin3

+0

_可能你這樣做只是爲了測試?我只是認爲用於生成陣列的工作表必須與要比較服務器名稱的工作表不同。您是對的,我也想在其他工作表上使用它。但如果這部分工作,其餘部分將很容易。 :-) – Divin3

+1

@ Divin3你已經在一個* Function *中使用它,所以可以在一個* Sub *中完成。假設你有一個* Sub *名爲** Test2 **,你也可以通過它ByRef。你可以像下面這樣創建Sub:Sub Test2(arr As Variant),然後在你的main子文件中對該子文件進行調用。 – L42

2

我認爲這可能是你定義C作爲在測試的範圍內,但調用服務器c當服務器期待一個布爾一個。

+1

@ Divin3我想以上是由RowanC給出的答案。將'Function server(ByVal issrvname As Boolean)'改爲'Function server(issrvname)'就可以了。但是,當然,您可以使用'ByVal/ByRef'以及類型。像'Function server(ByVal issrvname As String)'。 – L42

+0

如果您可以爲您確定的問題/問題提供解決方案,它將幫助OP(並使他/她高興)。 :) – L42

+0

這有幫助,但現在我得到的對象doesen't不支持方法 – Divin3