VBA动态范围插入当前月份的名称

时间:2015-02-17 01:35:51

标签: excel vba range

我是Excel的超级菜鸟,并且通过录音机完成了我的大部分宏观工作。但是现在我被卡住了。

我希望Z柱单元格中的动态范围$ Z $ 7到该Z列中最后一个数字条目的单元格(因为底部单元格是总数,并且与单元格具有不同的公式)以上我想要处于动态范围内。)

这是我从宏录制器录制的内容,现在我需要调整它。

ActiveWorkbook.Names.Add Name:="monthlyclaim", RefersToR1C1:= _
"='February 2015'!R7C26:R567C26"
ActiveWorkbook.Names.Add Name:="yearlyclaim", RefersToR1C1:= _
"='February 2015'!R7C29:R567C29"

有什么建议吗?它还说2015年2月的代码,但实际上更好的是将它用于活动工作表..将按月运行此宏。我是否可以在ActiveSheet中替换2015年2月'在代码中?到目前为止,它对我不起作用。

如果您需要更多信息,请与我们联系。

3 个答案:

答案 0 :(得分:0)

这应该有效:

ActiveWorkbook.Names.Add Name:="monthlyclaim", RefersToR1C1:= "='February 2015'!R7C26:R" & sheets("February 2015").cells(rows.Count,26).end(xlup).row - 1 & "C26"
ActiveWorkbook.Names.Add Name:="yearlyclaim", RefersToR1C1:= "='February 2015'!R7C29:R" & sheets("February 2015").cells(rows.Count,29).end(xlup).row - 1 & "C29"

答案 1 :(得分:0)

嗨,您可以使用" offset"结合counta给出动态行数:

RefersToR1C1:= "=Offset('February 2015!R7C26,0,0,counta('February 2015!R7C26:R10000C26) - 1,1)"

至于每张纸的名称......我不能说我真的使用"工作表"范围名称,他们似乎让我头疼...但尝试这样的事情:即,而不是从工作簿对象添加名称,从工作表添加:

for each wsSheet in thisworkbook.sheets
    wsSheet.names.add Name:="monthlyclaim", RefersToR1C1:= "=Offset('" & wssheet.name & "'!R7C26,0,0,counta('" & wssheet.name & "'!R7C26:R10000C26) - 1,1)"
next

冲洗并重复其他名称...所以有点概念上不同但效果相同:那些表格上的任何公式都引用了" monthlyclaim"将首先匹配工作表范围的名称。

答案 2 :(得分:0)

酷的工作真棒!

现在困难的部分是我的代码

Sub newSheet()     '     ' NewSheet宏     '     '键盘快捷键:Ctrl + e     '

'current counts

ActiveWorkbook.Names.Add Name:="hydrocount", RefersTo:="='" & ActiveSheet.Name & "'!$AE$7:$AE$" & ActiveSheet.Range("AC" & Rows.Count).End(xlUp).Row - 1
ActiveCell.FormulaR1C1 = "=IF(RC[-23]=""Isaac"",1,0)"
Range("AE7").Select
Selection.AutoFill Destination:=Range("hydrocount"), Type:=xlFillDefault

Dim wks As Worksheet


If MsgBox("Is month complete?", vbYesNo, "Confirm Action") = vbYes Then
'When user selects Yes
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy

Set wks = Sheets.Add(after:=ActiveSheet)

Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False


Month_year_picker.Show
'wks.Name = InputBox("What is the new month? Must be entered in the format of 'Month Year' - e.g. April 2015", "Sheet Name")

wks.Name = wks_Name


ActiveSheet.Paste

Else
'When user selects No
Exit Sub
End If

'
' RecalculateAnyNewMonth Macro
'
'claims stuff beginning

ActiveWorkbook.Names.Add Name:="monthlyclaim", RefersTo:="='" & ActiveSheet.Name & "'!$Z$7:$Z$" &         ActiveSheet.Range("Z" & Rows.Count).End(xlUp).Row - 1

ActiveWorkbook.Names.Add Name:="yearlyclaim", RefersTo:="='" & ActiveSheet.Name & "'!$AC$7:$AC$" & ActiveSheet.Range("AC" & Rows.Count).End(xlUp).Row - 1

'claims stuff finish

'countsnew

ActiveWorkbook.Names.Add Name:="hydrocountnew", RefersTo:="='" & ActiveSheet.Name & "'!$AE$7:$AE$" & ActiveSheet.Range("AC" & Rows.Count).End(xlUp).Row - 1


'The strPrevMonth string works based on the name of the sheet, if the sheet name is "January 2015",
'the string will be "December 2014".
'If the sheet name is "May 2015", the string will be "April 2015" and so on.
strPrevMonth = Format(DateAdd("M", -1, ActiveSheet.Name), "MMMM YYYY")

strMyFormula = "=IF(RC[-18]=""Isaac"",R6C8,0)+IF(RC[-12]=""BF"",R6C14,0)+IF(RC[-11]=""Client"",R6C15,0)+(IF(RC[-8]=""S"",1,0)*(IF(RC[-9]=""A"",R2C17,0)+IF(RC[-9]=""C"",R3C17,0)+IF(RC[-9]=""CS"",R4C17,0)))+IF(RC[-5]=""Y"",R6C21,0)+IF(RC[-4]=""Y"",R6C22,0)+((R6C24*RC[-2])*IF(RC[-2]="""",0,1))-'" & strPrevMonth & "'!RC[3]"

strMyFormula2 = "=RC[-3]+'" & strPrevMonth & "'!RC"


Range("Z7").FormulaR1C1 = strMyFormula
Range("Z7").AutoFill Destination:=Range("monthlyclaim")


Range("AC7").FormulaR1C1 = strMyFormula2
Range("AC7").AutoFill Destination:=Range("yearlyclaim")

'counts

ActiveWorkbook.Names.Add Name:="hydrocountnew", RefersTo:="='" & ActiveSheet.Name & "'!$AE$7:$AE$" & ActiveSheet.Range("AC" & Rows.Count).End(xlUp).Row - 1
ActiveCell.FormulaR1C1 = "=IF(RC[-23]=""Isaac"",1,0)"
Range("AE7").Select
Selection.AutoFill Destination:=Range("hydrocountnew"), Type:=xlFillDefault

Range("G573").Select
ActiveCell.FormulaR1C1 = "=SUM(hydrocountnew)-SUM(hydrocount)"
Range("G574").Select


End Sub

不幸的是,他们目前正在进行总计Hydrocountnew - 总和Hydrocount在当时给出了正确的值,但是一旦宏再次运行(对于新月),它将重置所有工作表,更新到最新的工作表。

有关如何解决此问题的任何想法?我希望它能够即时运行计算,但是随后为该槽计算的任何内容都存储在相应的工作表中,并且当宏再次运行时不会被覆盖。