sheetname的不匹配错误

时间:2015-07-02 08:17:28

标签: excel vba excel-vba

我想知道为什么我会继续面对

不匹配错误
  

if(Sheets(newsheetname).Rows(l).Cells(1,2).Value = product)then

代码基本上是试图找出并匹配单元格并在不同的工作表中循环。

请参阅下面的代码:

    Dim range1, range2 As Range
    'define the variables
    Dim referencesheetcols As Integer
    Dim range1rows, range1cols, range2rows, range2cols, testrows, testcols, i, j, p As Long
    Dim bMatches, rowmatched   As Boolean
    Dim product As String

    product = "ProductA"
    newsheetcols = 2123
    referencesheetcols = 2123
    ' how many rows and columns should we compare?
    testrows = 1
    testcols = 7
    p = Sheets(referencesheetname).UsedRange.Rows.Count
    For l = 7 To newsheetcols
        'only test if correct product

        **If (Sheets(newsheetname).Rows(l).Cells(1, 2).Value = product) Then**
            rowmatched = False
            For k = 7 To referencesheetcols
                'bmatch = False
                Set range1 = Sheets(referencesheetname).Rows(k)
                Set range2 = Sheets(newsheetname).Rows(l)
                ' count the rows and columns in each of the ranges
                range1rows = range1.Rows.Count
                range1cols = range1.Columns.Count
                range2rows = range2.Rows.Count
                range2cols = range2.Columns.Count
                'are the ranges the same dimension?
                bMatches = (range1rows = range2rows And range1cols = range2cols)
                'if same dimensions loop through the cells
                If bMatches Then
                    For i = 1 To testrows
                        For j = 1 To testcols
                            If (range1.Cells(i, j).Value <> range2.Cells(i, j).Value) Then
                                 bMatches = False
                                 i = testrows
                                 j = testcols
                            End If
                        Next
                    Next
                End If
                If bMatches Then
                    rowmatched = True
                    k = referencesheetcols
                End If
                'Sheets(outputsheetname).Cells(1, 1).Value = rowmatched
                If (Not (rowmatched) And k = referencesheetcols) Then
                    range2.Copy
                    Sheets(outputsheetname).Range("a1:a6000").Cells(p, 1).Offset(2, 0).Select
                    ActiveSheet.Paste
                    p = p + 1
                End If
            Next
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

用SO指南完成问题,

If cstr(Sheets(newsheetname).Rows(l).Cells(1, 2).Value) = product Then
            rowmatched = False
            For k = 7 To referencesheetcols