0

I have a lot of subroutines that are nearly identical in my macro, so I'm trying to condense it down to a generic sub that I can call multiple times. Here's what I have so far:

Sub Compare(wksheet As String, table As String, col1 As String, col2 As String, target As String)

Worksheets(wksheet).Activate
Range("table[[#Headers],[target]]").Select

On Error Resume Next
Dim tg_row As Integer

tg_row = 1

For Each tg_cl In Range("table[target]")
    If Range("table[col1]").Cells(tg_row, 1).Value = Range("table[col2]").Cells(tg_row, 1).Value Then
        tg_cl.Value = "Yes"
    Else
        tg_cl.Value = "No"
    End If
        tg_row = tg_row + 1
Next tg_cl

End Sub

This should be able to compare two columns and write "Yes" or "No" if they identical or not in a third column. The problem is when I try to run a sub that calls it, for example:

Sub Compare_Data()

Call Compare("Comparison Sheet", DataTbl, Data1Header, Data2Header, DataSameHeader)

End Sub

I get a "ByRef argument type mismatch" error. What am I doing wrong? Should I put quotes around the table and header names? How do I do that without breaking the original sub? Thank you in advance.

2
  • How are DataTbl, Data1Header, Data2Header, DataSameHeader Dim'ed in the calling routine?? Oct 14, 2014 at 19:31
  • They're not... How should I Dim these guys? They're names of tables and headers, not sure what type they should be... Oct 14, 2014 at 19:34

2 Answers 2

0

This might be helpful (Modify this according to requirement) :

Sub Compare(wksheet As WorkSheet, table As Object, col1 As Range, _
col2 As Range, target As Range)
'or you can use As Variant if you want to retain your piece of code
''''
''''
End Sub

and,

Sub Compare_Data()
Dim wks As WorkSheet
Dim table1 As Object
Dim Data1Header As Range
Dim Data2Header As Range
Dim DataSameHeader As Range

Set wks = Thisworkbook.Sheets("Comparison Sheet")
Set table1 = ActiveSheet.ListObjects(1)
Set Data1Header = Range(table1).Cells(0, 1)
Set Data2Header = Range(table1).Cells(0, 2)
'Set DataSameHeader = '''''''''

Call Compare(wks, table1, Data1Header, Data2Header, DataSameHeader)
''''
''''
''''
Set table1 = Nothing
Set Data1Header = Nothing
Set Data2Header = Nothing
End Sub
0

Ok, so I don't completely understand how I got this to work but it did, and I will share my results with you all. Here's what my code looks like now:

'This is the generalized subroutine that all comparison subroutines will reference when running
Sub Compare(wksheet As String, table As String, col1 As String, col2 As String, target As String)

'This activates the worksheet with the appropriate table and selects the top of the table
Worksheets(wksheet).Activate
Range(table & "[[#Headers]," & target & "]").Select

On Error Resume Next
'The valid range of an Integer variable is -2147483648 through +2147483647
Dim tg_row As Integer

tg_row = 1

'This will compare the two columns and will mark "Yes" or "No" if they match or not in the third column
For Each tg_cl In Range(table & target)
    If Range(table & col1).Cells(tg_row, 1).Value = Range(table & col2).Cells(tg_row, 1).Value Then
        tg_cl.Value = "Yes"
    Else
        tg_cl.Value = "No"
    End If
        tg_row = tg_row + 1
Next tg_cl

End Sub

So I reran this sub which is set up like this:

Sub Compare_Data()

Call Compare("Comparison Sheet", "DataTbl", "[Data1Name]", "[Data2Name]", "[DataSameName]")

End Sub

And it worked! Hope this is helpful and makes sense, because I'm still trying to figure out how it succeeded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.