当我通过将内容存储在变量中为excel编写宏时,我试图使事情变得容易些,对吗?好吧,当我运行该子程序时,它并不高兴,这是我所拥有的:
Sub TransferPrices()
''*************************
''Variables
''*************************
Dim Mon As Worksheet
Dim Tue As Worksheet
Dim Wed As Worksheet
Dim Thur As Worksheet
Dim Fri As Worksheet
Dim Sat As Worksheet
Dim Prices As Range
Dim Discount As Range
Dim todayDate As Date
Dim sheetDate As Range
''Sheet Names
Set Mon = Sheets("Monday prices")
Set Tue = Sheets("Tuesday prices")
Set Wed = Sheets("Wednesday prices")
Set Thur = Sheets("Thursday price")
Set Fri = Sheets("Frday price")
Set Sat = Sheets("Saturday price")
''Prices
Set Prices = Range("E11:E487")
Set Discount = Range("G209:G356")
''Dates
todayDate = Format(Date, "mm/dd/yyyy")
Set sheetDate = Range("Sheet_Date")
''*************************
''Change dates on sheets
''*************************
Mon.sheetDate = todayDate
Tue.sheetDate = DateDiff(d, 1, todayDate)
Wed.sheetDate = DateDiff(d, 2, todayDate)
Thur.sheetDate = DateDiff(d, 3, todayDate)
Fri.sheetDate = DateDiff(d, 4, todayDate)
Sat.sheetDate = DateDiff(d, 5, todayDate)
''*************************
''Transfer prices from Saturday sheet to Monday sheet
''*************************
Sat.Prices.Copy Mon.Prices
Sat.Discount.Copy Mon.Discount
''*************************
''Clear other sheets
''*************************
结束子
当我在此行Mon.sheetDate = todayDate
上编译模块时,出现错误,编译错误:在sheetDate上找不到方法或数据成员。您可以使用Mon.SheetDate
代替Mon.Range()
吗?还是我错过了什么?
答案 0 :(得分:0)
可以多次定义一个命名范围“ Sheet_Date”:
工作表范围的名称可以与工作表范围的名称之一在同一工作表中。
您可以通过e来解决每个问题。 g。
Set sheetDate = ActiveWorkbook.Names("Sheet_Date").RefersToRange
Set sheetDate = Sheets("Monday prices").Range("Sheet_Date")
Set sheetDate = Sheets("Tuesday prices").Range("Sheet_Date")
如果您之前没有手动设置它们(请注意范围!),则可以添加如下名称:
ActiveWorkbook.Names.Add Name:="Sheet_Date", _
RefersTo:=ActiveSheet.Range("B4")
Sheets("Monday prices").Names.Add Name:="Sheet_Date", _
RefersTo:=Sheets("Monday prices").Range("B2")
Sheets("Tuesday prices").Names.Add Name:="Sheet_Date", _
RefersTo:=Sheets("Tuesday prices").Range("B2")
如果它们具有与本示例中相同的名称,则必须首先添加工作簿作用域的名称-否则它将覆盖同一工作表中的现有工作表作用域的名称。
答案 1 :(得分:0)
如果创建一个类模块,则可以大致完成您在问题中尝试的操作。这样做:
创建一个类模块,并将其命名为 DaySheet 。这是您在属性中设置的任意名称(按F4到达那里),但是它必须与代码中的声明匹配。
将此代码放在类模块中:
Dim m_Worksheet As Worksheet
Property Set DaySheet(DaySheet As Worksheet)
Set m_Worksheet = DaySheet
End Property
Property Get DaySheet() As Worksheet
Set DaySheet = m_Worksheet
End Property
Property Let SheetDate(SheetDate As Date)
m_Worksheet.Range("A1") = SheetDate
End Property
Property Get SheetDate() As Date
SheetDate = m_Worksheet.Range("A1").Value
End Property
Property Get Prices() As Range
Set Prices = m_Worksheet.Range("E11:E487")
End Property
Property Get Discount() As Range
Set Prices = m_Worksheet.Range("G209:G356")
End Property
然后您可以在常规模块中像这样使用它:
Sub Test()
Dim TodayDate As Date
Dim Mon As New DaySheet
Dim Tue As New DaySheet
Dim Wed As New DaySheet
Set Mon.DaySheet = Worksheets("Mon")
Set Tue.DaySheet = Worksheets("Tue")
Set Wed.DaySheet = Worksheets("Wed")
TodayDate = Format(Date, "mm/dd/yyyy")
Mon.SheetDate = TodayDate
Tue.SheetDate = TodayDate + 1
Wed.SheetDate = TodayDate + 2
Tue.Prices.Copy Mon.Prices
End Sub
答案 2 :(得分:0)
感谢大家的投入。我了解到,您无法执行Mon.VARIABLE,它必须是变量以外的东西。同样在我的原始代码中,我在DateDiff函数中使用DateAdd语法,因此我将其修复。
最初的想法是,这将比我想的要复杂,但事实并非如此。我可能已经在其中丢了一些循环以编写更少的代码,但我认为它并不需要全部。与其使类变得复杂,我只是使它变得愚蠢。
它可以按我想要的方式工作,所以这里是最后一个宏:
Sub TransferPrices()
''*************************
''Variables
''*************************
Dim Mon As Worksheet
Dim Tue As Worksheet
Dim Wed As Worksheet
Dim Thur As Worksheet
Dim Fri As Worksheet
Dim Sat As Worksheet
''Sheet Names
Set Mon = Sheets("Monday prices")
Set Tue = Sheets("Tuesday prices")
Set Wed = Sheets("Wednesday prices")
Set Thur = Sheets("Thursday price")
Set Fri = Sheets("Friday price")
Set Sat = Sheets("Saturday price")
''Dates
todayDate = Format(Date, "mm/dd/yyyy")
sheetDate = "Sheet_Date"
''*************************
''Change dates on sheets
''*************************
Mon.Range(sheetDate) = todayDate
Tue.Range(sheetDate) = DateAdd("d", 1, todayDate)
Wed.Range(sheetDate) = DateAdd("d", 2, todayDate)
Thur.Range(sheetDate) = DateAdd("d", 3, todayDate)
Fri.Range(sheetDate) = DateAdd("d", 4, todayDate)
Sat.Range(sheetDate) = DateAdd("d", 5, todayDate)
''*************************
''Transfer prices from Saturday sheet to Monday sheet
''*************************
Sat.Range("E11:E487").Copy Mon.Range("E11:E487")
Sat.Range("G209:G356").Copy Mon.Range("G209:G356")
''*************************
''Clear other sheets
''*************************
''Clear Prices
Tue.Range("E11:E487").ClearContents
Wed.Range("E11:E487").ClearContents
Thur.Range("E11:E487").ClearContents
Fri.Range("E11:E487").ClearContents
Sat.Range("E11:E487").ClearContents
''Clear Discounts
Tue.Range("G209:G356").ClearContents
Wed.Range("G209:G356").ClearContents
Thur.Range("G209:G356").ClearContents
Fri.Range("G209:G356").ClearContents
Sat.Range("G209:G356").ClearContents
结束子