2014-07-17 89 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