使用VBA时,IF语句中的对象必需错误

时间:2013-08-13 20:03:02

标签: excel-vba if-statement vba excel

我是VBA的新手,我需要创建一个宏来检查数据库中的每一列与模板表。我已经让代码工作了,但是当我移动文件以显示我想要的文本时,我在If语句中不断收到错误“Object Required”。任何帮助将不胜感激。

Sub checkValues()
'Initalizes integers that will be used
Dim rwIndex As Long             '"Item Attributes" row index
Dim colIndex As Long            '"Item Attributes" column index
Dim rowEnd As Long              'Last row in "Item Attributes"
Dim colEnd As Long              'Last column in "Item Attributes"
Dim tempIndex As Integer        'Index used to move down 'Lookup Code' column in the template table
Dim resRow As Long              'Current row in "Report" to paste
Dim resCol As Long              'Current column in "Report" to paste

'Initializes the worksheets that will be usedd
Dim shnam1 As Worksheet
Dim shnam2 As Worksheet
Dim shnam3 As Worksheet

'Sets the worksheets for the workbook
Set shnam1 = Sheets("Item Attributes")
Set shnam2 = Sheets("Table")
Set shnam3 = Sheets("Report")

'Gets bounds for "Item Attributes" table
rowEnd = shnam1.Cells(Application.Rows.Count, 1).End(xlUp).Row
colEnd = shnam1.Cells(1, Application.Columns.Count).End(xlToLeft).Column

Call clearReport(shnam3)       'Clear results before every use

'Let the user know that the report is being made
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Application.DisplayAlerts = True

'Report Heading
shnam3.Cells(1, 1).Value = "Oracle Part Number"
shnam3.Cells(1, 2).Value = "Description"
shnam3.Cells(1, 3).Value = "Attribute Name"
shnam3.Cells(1, 4).Value = "Attribute Value"
shnam3.Cells(1, 5).Value = "Correct Value"

resRow = 2                  'Set row for Results

'From 2nd row to last row
For rwIndex = 2 To rowEnd

    tempIndex = 3       'Template table index
    resCol = 1          'Set column for results

    'From 3rd column to last column
    For colIndex = 3 To colEnd

        'Compare selection in data to template table
        If (shnam1.Cells(rwIndex, colIndex).Value) <> (shnam2.Cells(tempIndex, 1).Value) Then

            'Copy oracle part number and description
            shnam1.Range(shnam1.Cells(rwIndex, 1), shnam1.Cells(rwIndex, 2)).Copy shnam3.Cells(resRow, resCol)

            'Copy attribute name
            shnam2.Cells(tempIndex, 2).Copy shnam3.Cells(resRow, resCol + 2)

            'Copy correct attribute value
            shnam2.Cells(tempIndex, 1).Copy shnam3.Cells(resRow, resCol + 3)

            'Copy incorrect attribute value
            shnam1.Cells(rwIndex, colIndex).Copy shnam3.Cells(resRow, resCol + 4)

            resRow = resRow + 1                 'Move down a row in the "Report" table

        End If

        tempIndex = tempIndex + 1           'Increment through template table

    Next colIndex

Next rwIndex

'Turn on screen updating
Application.ScreenUpdating = True

'Format "Report" table
Call formatReport(shnam3)

'Turn off status bar and display that the report has finished
Application.StatusBar = False
Msgbox "The report has been created."

End Sub

'Clear the "Report" table
Sub clearReport(shnam As Worksheet)
    shnam.Select
    Cells.Select
    Selection.ClearContents
End Sub

'Adjust all text to be orientated on the left in "Report" table
Sub formatReport(shnam As Worksheet)
    shnam.Select
    Cells.Select
    Selection.NumberFormat = "@"
End Sub

1 个答案:

答案 0 :(得分:0)

上面写了一个简单的错误,正确和有效的代码。