#在工作簿之间复制粘贴时表单中的N / A.

时间:2018-03-22 14:24:18

标签: vba excel-vba excel

我面临的问题是,在将所有其他值从一个工作簿粘贴到另一个工作簿之后,工作簿中的某个工作表会显示正好6行中的#N / A值。我只是不明白为什么这些N / A出现时它们出现的单元格不是复制粘贴的目标。

问题示例:

enter image description here

这是源表。我将此数据传输到单独工作簿中的另一个工作表。我得到的结果是:

enter image description here

这是一个示例代码:

If (mybook.Name Like "*X1*TOTAL*" Or mybook.Name Like "*X2*Total*") Then
        Var2.Sheets("Sheet2").Range("A2:B4").Value = mybook.Sheets("Sheet1").Range("A2:B4").Value
End if

完整的子例程:

If (mybook.Name Like "*66*TOTAL*" Or mybook.Name Like "*66*Total*") Then
        Var2.Sheets("66X").Range("H6:AC43").Value = mybook.Sheets("66X").Range("E9:Z46").Value
        Var2.Sheets("66X").Range("H44:AC77").Value = mybook.Sheets("66X").Range("E48:Z81").Value
        Var2.Sheets("66X").Range("H78:AC133").Value = mybook.Sheets("66X").Range("E83:Z118").Value
        Var2.Sheets("66X").Range("H114:AC119").Value = mybook.Sheets("66X").Range("E120:Z125").Value
        Var2.Sheets("66X").Range("H120:AC127").Value = mybook.Sheets("66X").Range("E127:Z134").Value

这只是较大原始代码的摘录。为了参考和找到解决方案的目的,我在这里提供了代码之前的代码:

Sub MergeX()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook
Dim rnum As Long, CalcMode As Long
Dim vTopType As Variant
Dim iTopInset As Integer
Dim iTopDepth As Integer



' Change this to the path\folder location of your files.
    MyPath = ThisWorkbook.Worksheets("Sheet1").Range("D3")
' Add a slash at the end of the path if needed.
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If

    ' If there are no Excel files in the folder, exit.
    FilesInPath = Dir(MyPath & "*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If
     'Get destination file name
    Dim Srepfile As String
    MsgBox ("Select AGU Template to populate")
    Srepfile = Application.GetOpenFilename
    Set Var2 = Application.Workbooks.Open((Srepfile), UpdateLinks:=0)

    ' Fill the myFiles array with the list of Excel files
    ' in the search folder.
    FNum = 0
    Do While FilesInPath <> ""
        FNum = FNum + 1
        ReDim Preserve MyFiles(1 To FNum)
        MyFiles(FNum) = FilesInPath
        FilesInPath = Dir()
    Loop

    ' Set various application properties.
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
     ' Loop through all files in the myFiles array.
    If FNum > 0 Then
        For FNum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Workbooks.Open(MyPath & MyFiles(FNum), UpdateLinks:=0)

谢谢!

0 个答案:

没有答案