其他工作簿中的复制/粘贴脚本根本出了问题

时间:2019-07-11 14:50:21

标签: excel vba

很抱歉,如果遇到这种情况,我已经花了最后8个小时将其砸在墙上。无论我尝试哪种变体,此代码都会不断抛出各种程度的错误。我能干,我已经举了很多例子,但都没有做。我开始认为代码在工作簿中的位置有问题。我把它放在Module1中,我通常会保留所有代码。

实际问题-从弹出提示中选择文件后,将值从一个工作簿表复制/粘贴到另一工作表。而已。该表的结构相同,但是需要在目标表上向上移动5行。

我尝试了很多不同类型的代码,当我尝试迭代不同的代码时,您会看到注释。它不会正确地引用打开的工作簿。不断抛出错误,例如我尝试访问一些隐藏的Darknet数据库而不是旁边的文件……

我尝试了很多不同的方法,但是它们最终都会出现不同的错误。上面的代码给我“运行时错误13。类型,不匹配。”在试图复制代码的循环中。

我认为这全都源于excel无法正确引用打开的文件。即使应该.......................

 Sub ImportEstimatorData()
    Dim xTargetWb As Workbook 'Consolidator
    Dim xSourceWb As Workbook 'Estimator
    Set xTargetWb = ActiveWorkbook

    Dim xTargetRng As Range 'Target row/column in new sheet, changes row starting
    Dim xSourceRng As Range 'Source data from Estimator, Never changes

    Dim xSourceSt As Worksheet
    Dim xTargetSt As Worksheet

    Sheets("CR Data").Activate

    Set xTargetSt = ThisWorkbook.Sheets("CR Data")

    Dim vFile As Variant
    'fileToOpen = Application _
    ' .GetOpenFilename("Text Files (*.txt), *.txt")
    'If fileToOpen <> False Then
    ' MsgBox "Open " & fileToOpen
    'End If

    'Dim vFile As Variant
    'vFile = Application.GetOpenFilename("Excel-files,*.xlsx", 1, "Select One File To Open", , False)
    'if the user didn't select a file, exit sub
    'If vFile = "" Then Exit Sub
    'Set targetworkbook
    'Set xSourceWb = Workbooks.Open(vFile)
        If Not Application.OperatingSystem Like "*Mac*" Then
            ' Is Windows.
        vFile = Application.GetOpenFilename("Excel-files,*.xlsm", 1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
            If vFile = "" Then
            Exit Sub
            End If
        Else
        Exit Sub
        End If
            ' Is a Mac and will test if running Excel 2011 or higher.
      ' If Val(Application.Version) > 14 Then
       '         Set xSourceWb = Select_File_Or_Files_Mac

        '        End If
          '      End If

        'Workbooks.Open (vFile)

        Set xSourceWb = Workbooks.Open(vFile)

        'Workbooks(xSourceWb).Open
        'Workbooks(xSourceWb).Activate

        'Set Sheets for both Source & Target Workbooks

        'Set xSourceSt = xSourceWb.Sheets("Output data GPE")

        'Set xTargetRng = xTargetSt.Range(Cells(4, 2), Cells(80, 16))


        Sheets("Output Sheet GPE").Activate 'Range(Cells(1, 1), Cells(2, 2)).Select
        Set xSourceWb = ActiveWorkbook

        'xSourceWb.Activate

        'Set xSourceRng = xSourceSt.Range(Cells(4, 2), Cells(80, 16))

        'xSourceRng.Copy xTargetRng

        'Workbooks(xSourceWb).Worksheets("Output Sheet GPE").Range(Cells(8, 2), Cells(84, 16)).Copy Workbooks(xTargetWb).Worksheets("CR Data").Range(Cells(4, 2), Cells(80, 16))

        For i = 8 To 84

        For j = 2 To 16

        Workbooks(xSourceWb).Worksheets("Output Sheet GPE").Cells(i, j) = Workbooks(xTargetWb).Worksheets("CR Data").Cells(i - 4, j)
   'Debug error here

        Next j

        Next i


        'Workbooks(xWb).Worksheets("CR Data").Range(Cells(4, 2), Cells(80, 16)).Copy Workbooks(xTargetWb).Worksheets("C").Range(Cells(4, 2), Cells(80, 16))

        'Workbooks(xTargetWb).Worksheets("CR Data").Range(Cells(4, 2), Cells(80, 16)).PasteSpecial Paste:=xlPasteValues

        'Workbooks(xTargetWb).Sheets("CR Data").Range(Cells(4, 2), Cells(80, 16)).Value = Workbooks(xSourceWb).Sheets("Output Sheet GPE").Range(Cells(4, 2), Cells(80, 16))

       'xSourceWb.Close
    'End If
'End With

End Sub'

1 个答案:

答案 0 :(得分:0)

谢谢BigBen!就是这样!我删除了“工作簿”封闭器,它起作用了:-)

在下面复制/粘贴回复以供将来阅读: xSourceWb和xTargetWb已经是Workbook对象。不要将它们包含在工作簿中。就是说,您不需要为此循环。 – BigBen 16小时前