This code is to get the data from inv sheet column N to sheet rep column B Run-Time Error "1004" Method 'Range of object_'worksheet' failed

huangapple go评论72阅读模式
英文:

This code is to get the data from inv sheet column N to sheet rep column B Run-Time Error "1004" Method 'Range of object_'worksheet' failed

问题

这段代码用于从“inv”工作表的N列获取数据,然后将其写入到“rep”工作表的B列。

fw2 = Me.ListBox1.ListCount - 1

Inv.Range("B9:N500").ClearContents
Inv.Range("B8:N" & 8 + fw2).Cells.Value = ListBox1.List

With Rep
   .Range(.Cells(lastrow, "J"), .Cells(lastrow + fw2 - 1, "S")) = Inv.Range("B9:K" & 8 + fw2).Value 'all products

      '这里是我想要修改的部分
       运行时错误 "1004"
        对象worksheet的方法Range失败

    .Range(.Cells(lastrow, "B")) = Inv.Range("N9:N" & 8 + fw2).Value
End With
英文:

This code is to get the data from inv sheet column N to sheet rep column B

fw2 = Me.ListBox1.ListCount - 1

Inv.Range("B9:N500").ClearContents
Inv.Range("B8:N" & 8 + fw2).Cells.Value = ListBox1.List

with Rep
   .Range(.Cells(lastrow, "J"), .Cells(lastrow + fw2 - 1, "S")) = Inv.Range("B9:K" & 8 + 
     fw2).Value 'all products


      'Here is the one I want to modify 
       Run-Time Error "1004"
        Method 'Range of object_'worksheet' failed

    .Range(.Cells(lastrow, "B")) = Inv.Range("N9:N" & 8 + fw2).Value  
End With

答案1

得分: 0

这个错误是在您的表达式中,左边是一个单元格,但右边的范围包含多个单元格时生成的。
将以下行替换为:

.Range(.Cells(lastrow, "B")) = Inv.Range("N9:N" & 8 + fw2).Value

改为:

.Range(.Cells(lastrow, "B"), .Cells(lastrow + fw2, "B")) = Inv.Range("N9:N" & 8 + fw2).Value
英文:

This error is generated when in your expression the left side is one cell, but the right side's range consists of more than one cell.
Replace the line

    .Range(.Cells(lastrow, "B")) = Inv.Range("N9:N" & 8 + fw2).Value  

to

    .Range(.Cells(lastrow, "B") , .Cells(lastrow + fw2, "B")) = Inv.Range("N9:N" & 8 + fw2).Value

huangapple
  • 本文由 发表于 2023年6月6日 02:10:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定