我正在尝试根据第三列中的值将多个列从一个workbook
复制到另一个range
。我尝试了多种格式,将对象声明为variant
或.value
,将所选范围设置为.select
或Sub Fetch()
Dim Group As String
Dim gPath As String
Dim BUname As String
Dim PMAname As String
Dim Backup As Workbook
Dim PMA As Workbook
Dim Fetch As Workbook, Home As Worksheet
Set Fetch = ThisWorkbook
Set Home = ThisWorkbook.Sheets("Home")
Group = Range("B2").Value
mola = Range("B1").Value
maybe = Format(mola, "mm")
real = Format(mola, "yy")
nope = Format(mola, "yyyy")
ShtNm = Format(mola, "mm.yy")
gPath = "U:\BILLREC\M & R EG Billing\Analysts\My Name\" & Group & "\M2M\Original Backup" & "\" & nope & "\"
BUname = gPath & maybe & "." & real & " " & "Original Backup" & ".xlsx"
PMAname = gPath & maybe & "." & real & " " & "PMA" & ".xlsx"
'Opens Backup File and determines current premium
Set Backup = Workbooks.Open(BUname)
Backup.Sheets(ShtNm).Range(Range("E2"), Range("E2").End(xlDown)).Copy
Home.Range("D2").PasteSpecial xlPasteValues
Backup.Sheets(ShtNm).Range(Range("N2"), Range("N2").End(xlDown)).Copy
Home.Range("E2").PasteSpecial xlPasteValues
If Application.CountIf(Backup.Sheets(ShtNm).Range(Range("BW2"), Range("BW2").End(xlDown)).Values, "<>0") < 0 Then 'Runtime 438 occurs here.
Backup.Sheets(ShtNm).Range(Range("BX2"), Range("BX2").End(xlDown)).Copy
Home.Range("F2").PasteSpecial xlPasteValues
Backup.Sheets(ShtNm).Range(Range("BY2"), Range("BY2").End(xlDown)).Copy
Home.Range("G2").PasteSpecial xlPasteValues
Else
Backup.Sheets(ShtNm).Range(Range("CA2"), Range("CA2").End(xlDown)).Copy
Home.Range("F2").PasteSpecial xlPasteValues
Backup.Sheets(ShtNm).Range(Range("CB2"), Range("CB2").End(xlDown)).Copy
Home.Range("G2").PasteSpecial xlPasteValues
End If
Set PMA = Workbooks.Open(PMAname)
End Sub
。总而言之,这导致了运行时错误438,424,91和13。
最近的迭代产生了“运行时错误438:对象不支持此属性或方法”。
Backup.Sheets(ShtNm).Range(Range("N2"), Range("N2").End(xlDown)).Copy
Home.Range("E2").PasteSpecial xlPasteValues
TotPrm = Backup.Sheets(ShtNm).Range(Range("BW2"), Range("BW2").End(xlDown)).Values 'Error Occurs here now.
If Application.CountIf(TotPrm, "<>0") < 0 Then
Backup.Sheets(ShtNm).Range(Range("BX2"), Range("BX2").End(xlDown)).Copy
Home.Range("F2").PasteSpecial xlPasteValues
Backup.Sheets(ShtNm).Range(Range("BY2"), Range("BY2").End(xlDown)).Copy
Home.Range("G2").PasteSpecial xlPasteValues
Else
我尝试的另一个变体是命名实际范围......再次,结果为438:
TotPrm
我尝试将Range
同时声明为Variant
和Dim TotPrm As Range
,并通过相同的438声明:
Dim TotPrm As Variant
和
.Values
在同一点发生同样的438。
将.Value
更改为Set TotPrm = Backup.Sheets(ShtNm).Range(Range("BW2"), Range("BW2").End(xlDown)).Value 'This line is the bane of my existence
会导致需要运行时424对象:
BW BX BY BZ CA CB
TotPrmA CurA RetroA TotPrmB CurB RetroB
$0.00 $0.00 $0.00 $42.55 $42.55 $0.00
$0.00 $0.00 $0.00 $39.72 $39.72 $0.00
$0.00 $0.00 $0.00 $39.72 $39.72 $0.00
$0.00 $0.00 $0.00 $41.14 $41.14 $0.00
$0.00 $0.00 $0.00 $41.14 $41.14 $0.00
$0.00 $0.00 $0.00 $41.14 $41.14 $0.00
$0.00 $0.00 $0.00 $82.28 $82.28 $0.00
$0.00 $0.00 $0.00 $39.72 $39.72 $0.00
我正在运行的数据集是:
Total Premiums
这是重复的Current Premiums
,Retroactive Adjustments
,If...Else
数组。正如您可以看到紧跟在我的麻烦恶魔之后的Total Premiums
函数,它在第一个TotPrm
列中查找非零值,并根据以下内容确定要复制的列。结果。如果Variant
声明为Range.Value
并定义为Runtime Error 13: Type Mismatch
,则会在同一行生成Dim TotPrm As Range
TotPrm = Backup.Sheets(ShtNm).Range(Range("BW2"), Range("BW2").End(xlDown)).Value
。
最后但并非最不重要的是,运行时91:
[XmlSerializerFormat]
我在我的智慧&#39;结束。任何帮助都非常赞赏!
答案 0 :(得分:0)
首先,您需要完全限定所有范围参考(请参阅下面的点),否则他们可能会引用不同的工作表,这将导致错误。例如
With Backup.Sheets(ShtNm)
.Range(.Range("N2"), .Range("N2").End(xlDown)).Copy
End With
其次,Values
属性是什么?只要您将TotPrm
声明为Variant,您的最终配方就会对我好。
答案 1 :(得分:0)
Dim TotPrm As Range
在分配时需要Range
个对象,并且由于它是对象,因此必须使用Set
关键字:
Set TotPrm = <expression that evaluates to an instance of a Range object>
因此,当您执行此操作时,您错过了Set
关键字作为初学者,同时Select
方法实际返回Boolean
(True / False)值,而不是范围本身。
TotPrm = Backup.Sheets(ShtNm).Range(Range("BW2"), Range("BW2").End(xlDown)).Select
同样,这会失败,因为您已将TotPrem
定义为对象(Range
)但您尝试为其分配其他一些数据类型 - {{1 }}不是range.Value
(即使它是,你需要使用Range
keyord,如上所述) - 但是一个字符串,数字/等。
Set
删除TotPrm = Backup.Sheets(ShtNm).Range(Range("BW2"), Range("BW2").End(xlDown)).Value`
(或Select
等)并使用.Value
:
Set