用VBA在同一行不同列粘贴复制的数据

用VBA在同一行不同列粘贴复制的数据,vba,excel,Vba,Excel,通过我先前提出的问题,新的话题诞生了。 现在,我有几个复制的单元格,我想将它们粘贴到初始文件中(例如宏所在的工作簿) 我尝试的是保留块操作符“FOR”的同一行,将光标向后移动几列,并将选择粘贴到那里。但是出现了一个错误。当我使用statit方式“Range(“C10”)”时,但每次程序都必须有不同的单元格粘贴到那里。那么,我该如何应对呢 提前谢谢你 Option Explicit Sub FileFinder() ' Excel variables: Dim wbResults, oWB As

通过我先前提出的问题,新的话题诞生了。 现在,我有几个复制的单元格,我想将它们粘贴到初始文件中(例如宏所在的工作簿)

我尝试的是保留块操作符“FOR”的同一行,将光标向后移动几列,并将选择粘贴到那里。但是出现了一个错误。当我使用statit方式“Range(“C10”)”时,但每次程序都必须有不同的单元格粘贴到那里。那么,我该如何应对呢

提前谢谢你

Option Explicit

Sub FileFinder()
' Excel variables:
Dim wbResults, oWB As Workbook
Dim Sht As Worksheet
Dim RngS As Range
Dim sDir As String
Dim LastRow, i, Col As Long
'Optimizing CPU speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' set the worksheet object
Col = 25
Set Sht = Worksheets("Accounts source data")
With Sht
    ' find last row with data in Column "Y" (Col = 25)
    LastRow = .Cells(.Rows.Count, 25).End(xlUp).Row
    For i = 3 To LastRow
        If .Cells(i, Col) = "In Scope" Then
            ' Set the range directly, no need to use `Select` and `Selection`
            Set RngS = .Cells(i, Col).Offset(, -22)
            ' Search, in same directory where the file is located, the file with that account (file comes with account number as name)
            sDir = Dir$(ThisWorkbook.Path & "\" & RngS.Value & ".xlsx", vbNormal)
            Set oWB = Workbooks.Open(ThisWorkbook.Path & "\" & sDir)
            oWB.Worksheets("Report").Range("B27:B30").Copy

            'My error appears here: Run-time Error 424: Object required
            'If I replace "Cells(i, Col).Offset(, -11)" from below with "Range("C10")" the code works perfectly. But this is not the desired result
            wbResults.Worksheets("Accounts source data").Cells(i, Col).Offset(, -11).PasteSpecial Paste:=xlPasteAll, Transpose:=True
            oWB.Close SaveChanges:=False

            ' clear objects
            Set RngS = Nothing
            Set oWB = Nothing
        End If

    Next i
End With
'End optimizing CPU speed
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

只是一个快速修复。试着这样做:

此工作簿。工作表(“帐户源数据”)。单元格(i,Col)。偏移量(,-11)。粘贴特殊粘贴:=xlPasteAll,转置:=True

如果有效,请尝试将
wbResults
设置为
ThisWorkbook
,如注释中所述



您需要将
wbResults
设置为注释中建议的指定工作簿<代码>设置wbResults=ThisWorkbook是一种可行的方法。此外,在
VBA
中,只要Long
只将最后一个值设置为
Long
,其他两个值设置为
Variant
,这只是一个快速解决方案。试着这样做:

此工作簿。工作表(“帐户源数据”)。单元格(i,Col)。偏移量(,-11)。粘贴特殊粘贴:=xlPasteAll,转置:=True

如果有效,请尝试将
wbResults
设置为
ThisWorkbook
,如注释中所述



您需要将
wbResults
设置为注释中建议的指定工作簿<代码>设置wbResults=ThisWorkbook是一种可行的方法。此外,在
VBA
Dim a、b、c中,只要Long
只将最后一个值设置为
Long
,其他两个值设置为
Variant

您需要特别使用
Dim wbResults作为工作簿
,因为
Dim wbResults,oWB作为工作簿
意味着
wbResults
是一个
变量
。另外,您从不
设置wbResults=ThisWorkbook
。您需要特别使用
将wbResults作为工作簿进行调整
,因为
将wbResults作为工作簿进行调整
意味着
wbResults
是一个
变体
。此外,您从未
设置wbResults=ThisWorkbook