将活动单元地址分配给范围变量

时间:2018-03-31 09:57:43

标签: excel vba excel-vba

我在这一行得到Error 1004 "Application-defined or Object-defined error"

 `ws2.range(dstRef).offset(srn,0).value= srn+1`

为什么?

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

3 个答案:

答案 0 :(得分:1)

你要么用

ws2.Range(dstRef.Address).Offset(srn, 0).Value = srn + 1

ws2.Range("C19").Offset(srn, 0).Value = srn + 1

顺便说一句,你最好明确声明你所有的变量,或者从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

答案 1 :(得分:1)

我相信你正在寻找range.address property,虽然我不清楚为什么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?

您需要在昏暗的行中声明所有的vartype。

dim a, b, c, d as string

以上只将d dms作为字符串;其他一切都是变种。

答案 2 :(得分:0)

当您尝试将字符串(指定要使用的单元格)设置为范围而不是字符串时,会发生错误。您可以将dstRef声明为字符串,并在该行上使用它,例如

minSdkVersion