VBA - 函数中的工作表参数

时间:2017-03-31 16:27:43

标签: excel vba excel-vba worksheet

我有一个功能' mergeCategories'考虑参数(工作表,工作表,长)。我们的想法是读取单元格的值,并将其替换为基于映射表的其他值。

当我将它作为子函数运行并声明子函数内的值时,函数的内容很有效。但是当我从一个sub调用该函数时,我得到错误运行时错误424该行所需的对象: last_row_matching = ws_matching.Range(" A1")。End(xlDown).Row

显然,工作表ws_matching存在问题

这是功能:

    Function mergeCategories(ws_source As Worksheet, ws_macthing As Worksheet, last_row_used As Long) As Boolean
   'Variables
    'Result boolean
    Dim final_result As Boolean
    final_result = False

    'Source category name
    Dim src_cat_name As String

    'Destination category name
    Dim dest_cat_name As String

    'Index of last row in matching table
    Dim last_row_matching As Long
    last_row_matching = ws_matching.Range("A1").End(xlDown).Row
    MsgBox "Last row matching " & last_row_matching

    'Result of the matching (as range, .Value used to get name)
    Dim result_range As Range


    'Loop
    For i = 1 To last_row_used


        'get the source category name
        src_cat_name = ws_source.Range("A" & i).Value
        MsgBox "The category name pulled is " & src_cat_name

        'Find the mapping
        Set result_range = ws_matching.Range("A2:A" & last_row_matching).Find(src_cat_name)
        dest_cat_name = result_range.Offset(0, 1).Value

      MsgBox "The new category name is " & dest_cat_name
        ws_source.Range("A" & i).Value = dest_cat_name
        ws_source.Range("A" & i).Activate
        MsgBox "Check"
    Next i

    final_result = True
End Function

这是宏:

Sub test_mergeCategories()

    Dim ws_matching As Worksheet
    Set ws_matching = Sheets("Matching")
    Dim ws_source As Worksheet
    Set ws_source = Sheets("Temp_Import")
    Dim last_row_used As Long
    last_row_used = ws_source.Range("A1").End(xlDown).Row

   Call mergeCategories(ws_source, ws_matching, last_row_used)


End Sub

知道这是什么问题吗?

0 个答案:

没有答案