错误方法对象工作表范围失败

时间:2018-02-25 15:40:34

标签: excel-vba vba excel

我已经阅读了关于错误的每个问题,但还没有找到解决方案。我试图根据从表1到表2的连接标准复制一系列数据。代码似乎一直在工作,直到找到匹配并且是时候提取数据,我得到了错误。代码如下。从M到S的第一场比赛是在第五排。代码循环直到第五次迭代然后在THEN

之后的复制部分出现错误
Public Sub myWay()

Dim wsSource As Worksheet
Dim wsMain As Worksheet
Dim rngs As range
Dim rngm As range



Set wsSource = ThisWorkbook.Worksheets("Source")
Set wsMain = ThisWorkbook.Worksheets("Main")

Set rngs = wsSource.range("L2")
Set rngm = wsMain.range("L2")





    'Clear old data
    wsMain.range("D2:L1500").ClearContents
    wsSource.range("L2:L1500").ClearContents


    wsMain.range("L2:L" & range("A65000").End(xlUp).row).FormulaR1C1 = "=CONCATENATE(RC[-11],RC[-10],RC[-9])"
    wsSource.range("L2:L" & range("A65000").End(xlUp).row).FormulaR1C1 = "=CONCATENATE(RC[-11],RC[-10],RC[-9])"




        Do Until rngm.Value <> "" And rngm.Offset(0, 1).Value <> ""
            Do Until rngs.Value <> "" And rngs.Offset(0, 1).Value <> ""
                If (rngs.Value = rngm.Value) Then

                    wsMain.range("D" & rngm.row & "J" & rngm.row).Value = _
                    wsSource.range("D" & rngs.row & "J" & rngs.row).Value

                 End If
                 Set rngs = rngs.Offset(1, 0)

              Loop
              Set rngm = rngm.Offset(1, 0)

          Loop



End Sub

我尝试过使用工作表名称和其他一些调整,但我没有想法。任何帮助理解错误将不胜感激

1 个答案:

答案 0 :(得分:0)

只需在“J”之前添加冒号(:):

wsMain.range("D" & rngm.row & ":J" & rngm.row).Value = _
wsSource.range("D" & rngs.row & ":J" & rngs.row).Value