TLDR:我需要更改我编写的宏,该宏根据用户输入选择了一系列数据。它最初使用整个范围(只是连续的列),但现在需要选择每隔一列。
我有一张表,我正在处理用户输入他们想要投资的年数。基本上,我已经填充了50年,我的宏隐藏了超过指定年数的列。然后使用剩余的可见范围,我有运行的指标(净现值,内部收益率)。这一切都运行良好,但我现在需要在初始数据旁边放置一个百分比列,这意味着度量公式不能按原样使用连续范围运行。他们需要每隔一列可见数据。这里 以下是我的代码:
Sub HideCol()
'Prep the sheet
Sheets("Modeling").Visible = True
Sheets("Modeling").Activate
Columns("C:AZ").Select
Selection.EntireColumn.Hidden = True
myNum = Worksheets("InitialData").Range("LeaseTerm").Value
Range("C1").Select
numrows = Selection.Rows.Count
numColumns = Selection.Columns.Count
Selection.Resize(numrows + 0, numColumns + myNum - 1).Select
Selection.EntireColumn.Hidden = False
'Input dynamic calcs
'This is my problem here. calcrange selects consecutive columns, and I need to do every other
Dim calcrange As Range
Set calcrange = Union(Range("InvestmentOutlay"), Range(Range("B57"), Range("B57").End(xlToRight)))
Range("IRR").Formula = "=IRR(" & calcrange.Address & ")"
Dim npvRange As Range
Set npvRange = Range(Range("C57"), Range("C57").End(xlToRight))
Range("NPV").Formula = "=NPV(EconGrowth," & npvRange.Address & ")"
Dim outF As Range
Dim inF As Range
Set outF = Range("InvestmentOutlay")
Set inF = Range(Range("C57"), Range("C57").End(xlToRight))
Range("PB").Formula = "=PaybackPeriod(" & outF.Address & ", " & inF.Address & ")"
End Sub
答案 0 :(得分:1)
而不是:
Set calcrange = Union(Range("InvestmentOutlay"), Range(Range("B57"), Range("B57").End(xlToRight)))
只需将每一列添加到联盟:
Dim c As Long
Set calcrange = Range("InvestmentOutlay")
For c = 2 To Range("B57").End(xlToRight).Column Step 2
Set calcrange = Union(calcrange, Cells(57, c))
Next