简体   繁体   English

将具有命名范围的Excel函数转换为VBA时,类型不匹配错误

[英]Type mismatch error when converting Excel function with named ranges to VBA

I am trying to replicate a simple excel function into VBA but I get Type mismatch error. 我正在尝试将简单的excel函数复制到VBA中,但出现类型不匹配错误。 Basically, I have two named ranges rDate and rYear and the following Excel function that works perfectly giving the following output: 基本上,我有两个命名范围rDate和rYear,以下Excel函数可以完美地提供以下输出:

=IF(YEAR(rDate)=rYear,rYear,"") = IF(YEAR(RDATE)= rYear,rYear, “”)

https://www.screencast.com/t/rPpsVjudqrI https://www.screencast.com/t/rPpsVjudqrI

However, as I mentionned, I get a Type mismatch error when trying to convert it to VBA. 但是,正如我提到的,尝试将其转换为VBA时出现类型不匹配错误。 Here is my code: 这是我的代码:

Public ws As Worksheet
Sub Test()
Dim rDate, rYear, rResult, cell As Range

Set ws = Worksheets("Sheet1")
Set rDate = ws.Range("rDate")
Set rYear = ws.Range("rYear")
Set rResult = ws.Range("rResult")

For Each cell In rResult
    If Year(rDate) = rYear Then 'Problematic line
        cell = rYear
    Else
        cell = ""
    End If
Next
End Sub

Please let me know what I am doing wrong. 请让我知道我在做什么错。 Thank you 谢谢

vba reacts differently to ranges than the worksheet. vba对范围的反应不同于工作表。 In vba one needs to compare the value of one cell to one value not a multi-cell range to a multi-cell range. 在vba中,需要将一个单元格的值与一个值进行比较,而不是将多单元格范围与多单元格范围进行比较。

instead of trying to equate the whole range pick the one cell in each range based on where cell is located. 而不是试图使整个范围相等,而是根据单元格所在的位置选择每个范围中的一个单元格。

To get the correct reference on the vertical range we want to find the relative row: 为了获得垂直范围的正确参考,我们想找到相对行:

rDate.Cells(cell.Row - rResult.Row + 1, 1)

and the relative column: 和相对列:

rYear.Cells(1, cell.Column - rResult.Column + 1)

Then: 然后:

Dim rDate, rYear, rResult, cell As Range

only will declare cell as a range and the others as Variant . 仅将声明cell为范围,将其他声明为Variant

So use: 因此使用:

Dim rDate As Range, rYear As Range, rResult As Range, cell As Range

The worksheet function is making assumptions on row and column based on the relative location of the formula, that vba does not. 工作表函数基于公式的相对位置对行和列进行假设,而vba则不这样做。

Public ws As Worksheet
Sub Test()
Dim rDate As Range, rYear As Range, rResult As Range, cell As Range

Set ws = Worksheets("Sheet1")
Set rDate = ws.Range("rDate")
Set rYear = ws.Range("rYear")
Set rResult = ws.Range("rResult")

For Each cell In rResult
    If Year(rDate.Cells(cell.Row - rResult.Row + 1, 1)) = rYear.Cells(1, cell.Column - rResult.Column + 1) Then 'Problematic line
        cell = rYear.Cells(1, cell.Column - rResult.Column + 1)
    Else
        cell = ""
    End If
Next
End Sub

A quicker method would be to load the data into memory arrays and loop through those. 一种更快的方法是将数据加载到内存阵列中并循环遍历。

Public ws As Worksheet
Sub Test()
Dim rDate, rYear, rResult
Dim i As Long, j As Long

Set ws = Worksheets("Sheet1")
rDate = ws.Range("rDate").Value
rYear = ws.Range("rYear").Value
ReDim rResult(1 To UBound(rDate, 1), 1 To UBound(rYear, 2))

For i = LBound(rDate, 1) To UBound(rDate, 1)
    For j = LBound(rYear, 2) To UBound(rYear, 2)
        If rYear(1, j) = Year(rDate(i, 1)) Then
            rResult(i, j) = rYear(1, j)
            Exit For
        End If
    Next j
Next i
ws.Range("rResult").Value = rResult

End Sub

Every time one accesses the worksheet from vba it slows down the code. 每次有人从vba访问工作表都会减慢代码速度。 With this method we only access the worksheet 3 times, regardless of how large the ranges are. 使用这种方法,无论范围有多大,我们仅访问工作表3次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM