[英]assign active cell address to range variable
I get Error 1004 "Application-defined or Object-defined error"
on this line: 我在此行收到Error 1004 "Application-defined or Object-defined error"
:
`ws2.range(dstRef).offset(srn,0).value= srn+1`
Why is that? 这是为什么?
Dim ws1, ws2 As Worksheet
Set ws1 = Worksheets("Tabelle1")
Set ws2 = Worksheets("Packinglist_Annexure-1")
Dim srcRef, dstRef, tempAdr As Range
Set dstRef = Range("C19")
Dim k, srn As Integer
k = reqRow
srn = 0
For k = reqRow To row1
ws2.Activate
ws2.Range(dstRef.Address).Offset(srn, 0).Value = srn + 1
ws1.Activate
ws1.Range(reqAddr.Address).Offset(0, srn).Copy Destination:=ws2.Range(dstRef.Address).Offset(1, srn)
srn = srn + 1
Next k
you either use 你要么用
ws2.Range(dstRef.Address).Offset(srn, 0).Value = srn + 1
or 要么
ws2.Range("C19").Offset(srn, 0).Value = srn + 1
BTW you'd better explicily declare all your variables or they will be implicitly assume as of Variant
type: 顺便说一句,您最好明确声明所有变量,否则它们将被隐式假定为Variant
类型:
Dim ws1 , ws2 As Worksheet ' w1 is of Variant type and w2 is of Worksheet type
Dim ws1 As Worksheet, ws2 As Worksheet ' both w1 and w2 are of Worksheet type
I believe you are looking for the range.address property although I'm unclear on why Range("C19") has no parent worksheet reference. 我不确定您在寻找range.address属性,尽管我不清楚为什么Range(“ C19”)没有父级工作表引用。
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Tabelle1")
Set ws2 = Worksheets("Packinglist_Annexure-1")
Dim srcRef As Range, dstRef As Range, tempAdr As Range
Set dstRef = ws1.Range("C19")
Dim srn As Integer
srn = 0
ws2.Activate
ws2.Range(dstRef.ADDRESS).Offset(srn, 0).Value = srn + 1
Is the . 是个 。 in .Range necessary when defined by .Cells? 在.Cells定义时的必要范围内?
You need to declare all of the vartypes in a dim line. 您需要在暗行中声明所有vartype。
dim a, b, c, d as string
The above only dimms d as a string; 上面只把d作为字符串调暗; everything else is a variant. 其他一切都是变种。
The error is occurring as you are trying to set the string (that specifies the cells to be used) as a range rather than a string. 当您尝试将字符串(指定要使用的单元格)设置为范围而不是字符串时,发生了错误。 You can declare dstRef as a string and use this on that line eg 您可以将dstRef声明为字符串,并在该行上使用它,例如
Dim srcRef, tempAdr As Range
Dim dstRef As String
Dim srn As Integer
dstRef = "C19"
srn = 0
ws2.Activate
ws2.Range(dstRef).Offset(srn, 0).Value = srn + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.