VBA:运行时错误' 1004'当我使用工作表和变量声明时

时间:2015-03-30 01:54:25

标签: vba excel-vba object variable-declaration excel

Public ws As Worksheet
Public Const Mpath As String = "H:\BankingGrp\MM Board rates\"

Dim USDON As Long, USDTN As Long, USDSN As Long, USD1W As Long, _
    USD2W As Long, USD3W As Long, USD1M As Long, USD2M As Long, _
    USD3M As Long, USD6M As Long, USD9M As Long, USD12M As Long


Sub Record()
Workbooks.Open Filename:=Mpath & Format(Date, "DD") & " " & _
               Format(Date, "MMM") & " " & Format(Date, "YYYY") & ".xls"
Set ws = ActiveWorkbook.Worksheets("BOARD RATE")

我应该使用范围声明并使用"设置USDON =范围(" B15")"代替?

USDON = ws.Range(“B15”).Value
USDTN = ws.Range(“D17”).Value
USDSN = ws.Range(“F19”).Value
USD1W = ws.Range(“D21”).Value
USD2W = ws.Range(“D23”).Value
USD3W = ws.Range(“D25”).Value

End Sub

1 个答案:

答案 0 :(得分:0)

您的双引号符号不正确。它们不是标准的双引号,而是有角度的引号。例如,USDON = ws.Range(“B15”).Value应该变为USDON = ws.Range("B15").Value(注意B15周围的引号符号稍有变化。)

相反,您的作业应该变为:

USDON = ws.Range("B15").Value
USDTN = ws.Range("D17").Value
USDSN = ws.Range("F19").Value
USD1W = ws.Range("D21").Value
USD2W = ws.Range("D23").Value
USD3W = ws.Range("D25").Value