简体   繁体   English

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

[英]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. 我在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

Error 1004 on this line: 在此行的错误1004:

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调用提供的参数有问题( WorkbooksWorksheets错误参数将引发错误9 /“下标超出范围”)。

.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. 在执行此指令之前,在您发布的代码中的任何地方都未声明或分配变量DS_StartRowDS_LastRow

Without Option Explicit and assuming they aren't global variables defined elsewhere, looks like it's safe to assume their value is 0 . 如果没有Option Explicit并且假设它们不是在其他地方定义的全局变量,则可以很安全地假定它们的值为0

.Range("A0:A0")

...is illegal, since worksheet row addresses are 1-based. ...是非法的,因为工作表行地址基于1。 Hence, error 1004 is thrown. 因此,引发错误1004。

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. 现在,更容易查看确切的指令失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击单元格将值从工作簿的一个工作表转移到另一个工作表 - Transfer values from one sheet of a workbook to another by clicking on the cell 从同一 Excel 工作簿中的另一个工作表中检索单元格值 - Retrieving cell values from another sheet in the same excel workbook 将单元格范围从一个工作簿工作表复制到另一个工作簿工作表 - Copying cell range from a workbook sheet to another workbook sheet 将表格从一个 Excel 工作簿导入另一个工作簿工作表 - Importing a table from one Excel workbook into another workbook sheet 将单元格值从一个工作簿复制到另一个工作簿 - Copying cell values from one Workbook into another 匹配并将单元格值从一张纸替换到另一张纸 - match and replace cell values from 1 sheet to another 如何将整个工作表从一个工作簿复制到另一个工作簿,并在顶角单元格中移动 - how to copy a full sheet from one workbook to another workbook with shift in the tope corner cell 根据条件将值从一张纸复制到另一张工作簿 - Copying values from one sheet based on condition to another workbook 从一个工作簿到另一个工作簿的VBA复制表(仅值和格式) - VBA copy sheet from one workbook to another (values and format only) 将工作表从excel工作簿复制到另一个工作簿 - Copy sheet from excel workbook to another workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM