简体   繁体   中英

Importing Cell values to Sheet from another workbook

I keep getting and error message at Set MainRng = Workbooks(mainfile) It is an object not defined error, this works in my other spreadsheet, but not in the new one.

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

Error 1004 on this line:

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

Means something is wrong with the parameters given to the Range call (bad arguments for Workbooks or Worksheets would throw error 9 / "subscript out of range").

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

Variables DS_StartRow and DS_LastRow aren't declared or assigned anywhere in the code you posted before this instruction gets to run.

Without Option Explicit and assuming they aren't global variables defined elsewhere, looks like it's safe to assume their value is 0 .

.Range("A0:A0")

...is illegal, since worksheet row addresses are 1-based. Hence, error 1004 is thrown.

One way to narrow down on the problem, is to split such instructions doing too many things, into smaller statements that do one thing:

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)

Now it's much easier to see exactly which instruction is failing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM