0

我是 VBA 新手,我一直在尝试将一个文件中的一些数据粘贴到我的活动文件中。不幸的是,我收到了错误 13 - 类型不匹配。我尝试更改每个变量定义,甚至将它们声明为Variant,但没有任何帮助。代码中最相关的部分如下,星号之间有错误。

dim i, j, k, CompShtStartNum, CompShtQty as integer
dim OldFile as variant
dim WCompWS, WCOl, NumEntryCol, ShtName as string
dim InputsSht as worksheet
dim NumEntryColRange, OldEntryCount as range


'Paste data from Entry Label columns into comparison sheets
    'Paste in the data from the old file
        For i = CompShtStartNum To CompShtStartNum + CompShtQty - 1
            ShtName = ThisWorkbook.Sheets(i).Name
            Set OldSht = OldFile.Sheets(ShtName)
            Set OldEntryCount = Range(OldSht.Cells(2, 1), OldSht.Cells(Rows.Count, 1).End(xlDown))
            For j = 1 To CompShtStartNum - i + 1
                For k = 1 To InputsSht.Range(WCol & 12 + j - 1).Value
                     If OldFile.Sheets(i).Cells(1, k).Value = Sheets(i).Cells(1, k).Value Then
                        ***Sheets(i).Cells(2, k).Resize(OldEntryCount.Rows.Count, 1).Value = Application.Transpose(OldEntryCount.Value)***
                    End If
                Next k
            Next j
       Next i

对于上下文,这是完整的代码:

Set OldFile = Application.Workbooks("Old Input File.xlsx")
    Let WCompWS = "E"
    Let WCol = "F"
    Let CompShtStartNum = 2
    Set InputsSht = ThisWorkbook.Sheets("Inputs")
    Let CompShtQty = InputsSht.Range(WCompWS & 12, InputsSht.Range(WCompWS & 12).End(xlDown)).Count

'Loop thru each sheet and have the user determine the last column of labels.  Paste result on Inputs sheet.
    For i = CompShtStartNum To CompShtStartNum + CompShtQty - 1
        ShtName = ThisWorkbook.Sheets(i).Name
        Sheets(ShtName).Activate
        NumEntryCol = Application.InputBox("How many columns (from the left-hand side) contain entry labels?" & vbNewLine & "(Examples of entry labels: Library #, Entry #, etc.)" & vbNewLine & vbNewLine & "Please type your answer numerically.", ShtName)
        InputsSht.Range(WCol & 12 + i - CompShtStartNum).Value = NumEntryCol
    Next i
    Set NumEntryColRange = InputsSht.Range(WCol & 12, InputsSht.Range(WCol & 12).End(xlDown))
    InputsSht.Activate

'Paste data from Entry Label columns into comparison sheets
    'Paste in the data from the old file
        For i = CompShtStartNum To CompShtStartNum + CompShtQty - 1
            ShtName = ThisWorkbook.Sheets(i).Name
            Set OldSht = OldFile.Sheets(ShtName)
            Set OldEntryCount = Range(OldSht.Cells(2, 1), OldSht.Cells(Rows.Count, 1).End(xlDown))
            For j = 1 To CompShtStartNum - i + 1
                For k = 1 To InputsSht.Range(WCol & 12 + j - 1).Value
                     If OldFile.Sheets(i).Cells(1, k).Value = Sheets(i).Cells(1, k).Value Then
                        ***Sheets(i).Cells(2, k).Resize(OldEntryCount.Rows.Count, 1).Value = Application.Transpose(OldEntryCount.Value)***
                    End If
                Next k
            Next j
       Next i

任何帮助或建议将不胜感激!!

4

1 回答 1

1

但是,结果只是 A2 中的值被粘贴到活动文件表上的 A2:A7 中。如何将 A2:A7 中的每个值粘贴到活动工作表上各自的单元格中?

尝试这个

Sheets(i).Cells(2, k).Resize(OldEntryCount.Rows.Count, 1).Value = _
OldEntryCount.Value

这是一个简短的演示。假设我们的工作表看起来像这样

在此处输入图像描述

现在假设我们想要A1:A5inB1:B5的值Sheet1

试试这个

Sub Sample()
    Dim OldEntryCount As Range

    With ThisWorkbook.Sheets("Sheet1")
        Set OldEntryCount = .Range("A1:A5")

        .Range("B1").Resize(OldEntryCount.Rows.Count, 1).Value = _
        OldEntryCount.Value
    End With
End Sub

你会得到结果

在此处输入图像描述

于 2013-10-09T19:12:51.960 回答