English 中文(简体)
试图作为功能产出回归范围,就会有不匹配的类型?
原标题:Trying to return a range as function output, get type mismatch?
  • 时间:2012-01-13 19:04:18
  •  标签:
  • excel
  • vba

以下职能恢复“类型不匹配”。 我不理解,因为我重视使用“Set”指示来恢复我由此而产生的范围。

我辞退了这一职务,我有适当的回程,因此问题在其他地方。 Hmm.

Function getVals(column As String) As Range
    Dim col As Variant
    col = Application.Match(column, ThisWorkbook.ActiveSheet.Range("1:1"), 0)
    Dim rng As Range
    Set rng = ThisWorkbook.ActiveSheet.Cells(1, col)
    Set rng = rng.Offset(1, 0)
    Set rng = Range(rng, rng.End(xlDown))
    Set getVals = rng
End Function

事先感谢任何帮助:

最新资料:我正在研究如何把我的成果作为一阵列。 我尝试将这种功能合并起来,将“变数”/“变数”()”类型退回,并因此而推升。

最佳回答

为了把结果作为一系列的价值观来回,简单地将返回类型改为Variant,并将之退回rng。 数值。 以下代码对我有效,只要通过<代码>column,则载于。 The Workbook.ActiveSheet.Range ("1:1”).

Function getVals(column As String) As Variant
    Dim col As Variant
    col = Application.Match(column, ThisWorkbook.ActiveSheet.Range("1:1"), 0)
    Dim rng As Range
    Set rng = ThisWorkbook.ActiveSheet.Cells(1, col)
    Set rng = rng.Offset(1, 0)
    Set rng = Range(rng, rng.End(xlDown))
    getVals = rng.Value
End Function

Sub TestingGetVals()
   Dim v As Variant, i As Integer
   v = getVals("a")    returns a 2-D array
   For i = 1 To UBound(v)
      Debug.Print v(i, 1)
   Next i
End Sub
问题回答

你正在发现这一错误,因为Match无法找到你想要的东西,因此,你正在评估“无”:

考虑本守则

Option Explicit

Sub Sample()
     Dim Ret As Range

     If Not getVals("Value To Match") Is Nothing Then
        Set Ret = getVals("Value To Match")
        MsgBox Ret.Address
     Else
        MsgBox "Value To Match - Not Found"
     End If
End Sub

Function getVals(column As String) As Range
    Dim col As Variant
    Dim rng As Range

    On Error GoTo Whoa

    col = Application.Match(column, ThisWorkbook.ActiveSheet.Range("1:1"), 0)

    Set rng = ThisWorkbook.ActiveSheet.Cells(1, col)

    Set rng = rng.Offset(1, 0)
    Set rng = Range(rng, rng.End(xlDown))
    Set getVals = rng

    Exit Function
Whoa:
    Set getVals = Nothing
End Function

首先,我不理解你正在做什么。 您有参数<代码>column,但你正在第1行内寻找一个含有这一价值的电池。 例如,如果一栏=23,而P1包含23,则Cambridge应退回16。

Your routine fails because if the Match fails, col is set to Error 2042. You should test col before using it as a number.

在我的测试中,我按随机顺序排列第1至第1行。 我的马舍克公司之所以失败,是因为P1号载有23号,但可变的一栏载有“23”。 当我把一栏重新分类为长时,马氏公司就进行了工作。

我对Siddharth使用表示不满意。 关于Error。 我不想使用<条码>关于Error,因为我预计会出现错误。 我将测试col在“马舍”后成为数字。

其他人在书面上更快,当时我是: 还有一种可能性是,尚未提及。

由于你在行乞时有错误,问题可能是你使用<代码>。 活性Sheet。 如果错误的工作单是有效的,那么“匹配”就会造成其他答复所述错误。

如果你明确,错误是否消失?

col = Application.Match(column, ThisWorkbook.Sheet(1).Range("1:1"), 0)




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签