Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 是否将range1中C列和F列中的值复制到range2中的P列和R列?_Excel_Vba - Fatal编程技术网

Excel 是否将range1中C列和F列中的值复制到range2中的P列和R列?

Excel 是否将range1中C列和F列中的值复制到range2中的P列和R列?,excel,vba,Excel,Vba,我对下面的粗体代码行有问题:我得到一个错误,它表示“应用程序定义的或对象定义的错误” 我把这段代码从我以前的代码改过来了 Option Explicit Sub BringDownSNs() Call OptimizeCode_Begin Dim cell1 As Range, rng1 As Range, cell2 As Range, rng2 As Range Dim array1, array2, counter1 As Long, counter2 As Long Dim Star

我对下面的粗体代码行有问题:我得到一个错误,它表示“应用程序定义的或对象定义的错误”

我把这段代码从我以前的代码改过来了

Option Explicit

Sub BringDownSNs()
Call OptimizeCode_Begin

Dim cell1 As Range, rng1 As Range, cell2 As Range, rng2 As Range
Dim array1, array2, counter1 As Long, counter2 As Long
Dim StartTime As Double
Dim MinutesElapsed As String

Set rng1 = Sheets("Sheet1").Range("B2:B" & Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "B").End(xlUp).row)
Set rng2 = Sheets("Sheet1").Range("M2:M" & Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "M").End(xlUp).row)

array1 = rng1.Offset(0, 4).Resize(, 5).Value 'creating an array by taking rng1 (column B) and offsetting it by 4 (to include up to column F), then resizing to include columns B through F (5 columns total)
array2 = rng2.Offset(0, 5).Resize(, 6).Value 'same as above but using rng2

For counter2 = 1 To UBound(array2) 'for each row in array2 - from row 1 in array 2 to upper bound of array 2 (row 3865)
    For counter1 = 1 To UBound(array1) 'for each row in array1 - from row 1 in array 1 to upper bound of array 1 (row 2390)

    'if array 2 -> sheet 1, current row (starting with row 1 of the array), column 1 of the array (Column M, NHA Part Number) equals array 1 -> sheet 1, current row, column 1 of the array (Column B, NHA Part Number) _
     AND if array 2 -> sheet 1, current row, column 5 of the array (Column Q , Part Number) equals array 1 -> sheet 1, current row, column 4 of the array (Column E, Part Number) THEN _
     copy data from current row in array 1 (columns C & F) into current row in array 2 (columns P & R)

        If array2(counter2, 1) = array1(counter1, 1) And array2(counter2, 5) = array1(counter1, 4) Then
            **Sheets("Sheet1").Range("C:C" & counter1 + 1 & "F:F" & counter1 + 1).Copy Destination:=Sheets("Sheet1").Range("P:P" & counter2 + 1 & "R:R" & counter2 + 1)**
        Exit For
        End If
    Next
Next

Call OptimizeCode_End
End Sub

我相信不是

Sheets("Sheet1").Range("C:C" & counter1 + 1 & "F:F" & counter1 + 1).Copy Destination:=Sheets("Sheet1").Range("P:P" & counter2 + 1 & "R:R" & counter2 + 1)
你真的在追求什么

Sheets("Sheet1").Cells(counter1 + 1, "C").Copy Destination:=Sheets("Sheet1").Cells(counter2 + 1, "P")
Sheets("Sheet1").Cells(counter1 + 1, "F").Copy Destination:=Sheets("Sheet1").Cells(counter2 + 1, "R")
或者,如果您只希望复制数据:

Sheets("Sheet1").Cells(counter2 + 1, "P").Value = Sheets("Sheet1").Cells(counter1 + 1, "C").Value
Sheets("Sheet1").Cells(counter2 + 1, "R").Value = Sheets("Sheet1").Cells(counter1 + 1, "F")

这是为了达到什么目的<代码>范围(“C:C”和“F:F”和计数器1+1&“F:F”和计数器1+1)您是否在两行之后
工作表(“Sheet1”)。单元格(计数器1+1,“C”)。复制目的地:=工作表(“Sheet1”)。单元格(计数器2+1,“P”)
工作表(“Sheet1”)。单元格(计数器1+1,“F”)。复制目的地:=工作表(“Sheet1”)。单元格(计数器2+1,“R”)
?(或者,如果您只希望复制数据,
Sheets(“Sheet1”)。单元格(counter2+1,“P”)。Value=Sheets(“Sheet1”)。单元格(counter1+1,“C”)。Value
Sheets(“Sheet1”)。单元格(counter2+1,“R”)。Value=Sheets(“Sheet1”)。单元格(counter1+1,“F”)
),您应该更新
数组中的注释1=rng1。偏移量(0,4)。调整大小(,5).Value“通过获取rng1(B列)并将其偏移4(最多包含F列),然后调整大小以包含B列到F列(总共5列)
-注释实际上应为
”,通过获取rng1(B列)并将其偏移4(参考F列)来创建数组,然后调整大小以包括F列到J列(总共5列)
@YowE3K Yea,所以我只想复制C的值并将其放入P,F的值并将其放入R列……然后移到下一行。看起来您的代码正是我想要的,但我一直收到一个“不匹配”错误是
If
语句行上的不匹配吗?你真的想比较R列和F列,V列和I列吗?