从另一个工作簿将单元格值导入到工作表

时间:2019-01-17 15:20:35

标签: excel vba

我在Set MainRng = Workbooks(mainfile)上不断收到错误消息 这是一个未定义的对象错误,此错误在我的其他电子表格中有效,但不适用于新的电子表格。

Sub LoadData(mainfile As String, srcfile As String)
    Dim DS As Worksheet
    Dim Cell As Range
    Dim CurrentRow As Integer, ValPos As Integer
    Dim AsFoundLoc As String, AsLeftLoc As String, ValTextLoc As String
    Dim SheetName As String, ValDataText As String, FileValidation As String
    Dim ImportData As Variant, Multiplier As Variant, AutomationType As String
    Dim AsLeftData As Variant
    Dim VerifySheetName As String

    Workbooks(srcfile).Activate
    AutomationType = Workbooks(mainfile).Worksheets("Import Map").Range("B5").Value
    SheetName = Workbooks(mainfile).Worksheets("Import Map").Range("B7").Value
    ValDataText = Workbooks(mainfile).Worksheets("Import Map").Range("A10").Value
    ValTextLoc = Workbooks(mainfile).Worksheets("Import Map").Range("B10").Value

    'Set ValPos to 0
    ValPos = 0
    AsLeftData = vbNo

    'Set the Verify Sheet Name
    VerifySheetName = SheetName


    'Change Verify Sheet Name for SureCal
    'If SureCal Ask if this is As Left Data
    If AutomationType = "SureCal" Then
        VerifySheetName = "Cover Sheet"
        AsLeftData = MsgBox("NOTE: For SureCal the you will need to Import Data for both" & Chr(13) & "the As Found and As Left Data Seperately if required" _
            & Chr(13) & Chr(13) & "Are you Importing the Left Data now?", vbYesNo)
    End If


    'Check to see if a validation text is used
    If ValDataText <> "" And ValTextLoc <> "" Then
        FileValidation = Workbooks(srcfile).Worksheets(VerifySheetName).Range(ValTextLoc).Value

        ValPos = InStr(1, FileValidation, ValDataText, vbTextCompare)
    Else
        ValPos = 1
    End If


    'Proceed if File Text Validated
    If ValPos <> 0 Then

        Application.StatusBar = "Importing Data..."

        Set MainRng = Workbooks(mainfile).Worksheets("Import Map").Range("A" & DS_StartRow & ":A" & DS_LastRow)
        Workbooks(mainfile).Activate


        For Each Cell In MainRng

            CurrentRow = Cell.Row
            SheetName = Workbooks(mainfile).Worksheets("Import Map").Range("B7").Value
            AsFoundLoc = Workbooks(mainfile).Worksheets("Import Map").Range("C" & CurrentRow).Value
            AsLeftLoc = Workbooks(mainfile).Worksheets("Import Map").Range("D" & CurrentRow).Value
            Multiplier = Workbooks(mainfile).Worksheets("Import Map").Range("E" & CurrentRow).Value
            ImportData = ""

            'Now add the AsFound data
            If AsFoundLoc <> "" Then

                ImportData = Workbooks(srcfile).Worksheets(SheetName).Range(AsFoundLoc).Value

                'Call the Correct Automation Type to Format Data input
                If AutomationType = "SureCal" Then ImportData = SureCalData(ImportData)
                If AutomationType = "NI" Then ImportData = NIData(ImportData)


                'First line of code moves data to datasheet, 2nd line of code adds it to the Repeatability column
                If Not IsEmpty(ImportData) Then
                    If IsNumeric(ImportData) Or LCase(ImportData) = "pass" Or LCase(ImportData) = "fail" Then
                        If IsNumeric(ImportData) Then
                            ImportData = ImportData * Multiplier
                        End If

                        If AsLeftData = vbNo Then
                            Workbooks(mainfile).Worksheets("Datasheet").Range("I" & CurrentRow).Value = ImportData
                            Workbooks(mainfile).Worksheets("Import Map").Range("F" & CurrentRow).Value = ImportData
                        Else
                            Workbooks(mainfile).Worksheets("Datasheet").Range("J" & CurrentRow).Value = ImportData
                        End If

                    End If
                End If
            End If

            'Now add the AsLeft data
            'Note: As Left is skipped for SureCal Imports
            If AutomationType <> "SureCal" Then

                If AsLeftLoc <> "" Then

                    ImportData = ""
                    ImportData = Workbooks(srcfile).Worksheets(SheetName).Range(AsLeftLoc).Value

                    'Call the Correct Automation Type to Format Data input - Note: SureCal Does not get Called
                    'If AutomationType = "SureCal" Then ImportData = SureCalData(ImportData)
                    If AutomationType = "NI" Then ImportData = NIData(ImportData)


                    If Not IsEmpty(ImportData) Then

                        If IsNumeric(ImportData) Or LCase(ImportData) = "pass" Or LCase(ImportData) = "fail" Then
                            If IsNumeric(ImportData) Then
                                ImportData = ImportData * Multiplier
                            End If

                            Workbooks(mainfile).Worksheets("Datasheet").Range("J" & CurrentRow).Value = ImportData
                        End If
                    End If
                End If
            End If

        Next Cell
        'Determine Starting of Data in each the main and the source
        'Workbooks(srcfile).Activate
        'Workbooks(mainfile).Activate

    Else
        MsgBox "Validation Text ( " & ValDataText & " ) Was not Found in the " & VerifySheetName _
        & " at Cell " & ValTextLoc & Chr(13) & Chr(13) & "No Data was Imported"


    End If
End Sub

1 个答案:

答案 0 :(得分:0)

此行的错误1004:

Set MainRng = Workbooks(mainfile).Worksheets("Import Map").Range("A" & DS_StartRow & ":A" & DS_LastRow)

表示给Range调用提供的参数有问题(WorkbooksWorksheets的错误参数将引发错误9 /“下标超出范围”。)

.Range("A" & DS_StartRow & ":A" & DS_LastRow)

变量DS_StartRowDS_LastRow在执行此指令之前未在您发布的代码中的任何地方声明或分配。

如果没有Option Explicit并假设它们不是在其他地方定义的全局变量,则可以安全地假设其值为0

.Range("A0:A0")

...是非法的,因为工作表行地址是从1开始的。因此,将引发错误1004。

缩小问题范围的一种方法是,将执行过多任务的此类指令拆分为可以执行 一件事的较小语句:

Dim wb As Workbook
Set wb = Workbooks(mainfile)

Dim ws As Worksheet
Set ws = wb.Worksheets("Import Map")

Dim map As Range
Set map = ws.Range("A" & DS_StartRow & ":A" & DS_LastRow)

现在,更容易准确地知道哪个指令失败了。