我有动态命名范围'HDaERReturns',从.Cells(108,2)
开始到范围内的最后一个Cell,我想在列中将其剪切,并使用它在第106行中保存的标题命名每一列。
Range的当前方式示例:
| Item | EURUSD | GBPUSD | USDCHF | XAUUSD |
|:----------:|:----------:|:----------:|:----------:|:----------:|
| Date | Volatility | Volatility | Volatility | Volatility |
| 06.03.2018 | | | | |
| 05.03.2018 | | | | |
| 04.03.2018 | | | | |
| 02.03.2018 | | | | |
| 01.03.2018 | | | | |
| 28.02.2018 | | | | |
| 27.02.2018 | | | | |
| 26.02.2018 | | | | |
| 25.02.2018 | | | | |
我需要在动态命名范围'HDaERReturns'中命名每一列,因为:
我需要循环动态命名范围'HDaERReturns'中每一列中的每个Cell,并在其中执行Normal Log返回计算,这是指另一个名为'HDaERClose'的动态命名范围,我必须切断在列和名称与标题。 这样我可以通过编写类似的东西来参考计算阶段:
For Each Column In Range("HDaERReturns")
' Set header as .Name
Next
For Each Cell In Columns
' Perform calculations
Next
得到这样的输出:
我还没有进入MLR阶段,所以我没有正确地纠正这个问题,但这是一个不同的话题。现在我需要创建列动态命名范围,并将其标题作为名称。
标题不断变化,它们对应的列中的值也是如此,因此它必须是动态方法。
到目前为止还没有真正得到一个有意义的代码,但我想知道是否有人可以给我一些关于如何解决这个问题的指示。
答案 0 :(得分:1)
这是为您指定的范围创建。
注意:
COUNTA
。否则,请将COUNTA
替换为COUNT
。版本1这会创建动态命名范围,但如果列中缺少数据,则可能会出现问题。
Option Explicit
Public Sub PerformLogOperation()
Dim wbTarget As Workbook
Dim wsTarget As Worksheet
Set wbTarget = ThisWorkbook
Set wsTarget = wbTarget.Worksheets("Sheet1")
Dim namedRange As Range
Dim namesArr()
Set namedRange = wsTarget.Range("HDaERReturns")
namesArr = GetNamedRangeNames(namedRange)
CreateNamedRanges namedRange, namesArr
''TODO Your log operation code goes here
End Sub
Public Sub CreateNamedRanges(ByVal namedRange As Range, ByVal namesArr As Variant, Optional wbTarget As Workbook)
Dim currentNamedRange As Long
Dim tempRange As Range
Dim tempRangeName As String
If wbTarget Is Nothing Then Set wbTarget = ThisWorkbook
Dim sheetName As String
sheetName = namedRange.Parent.Name
Dim startRow As Long
Dim endRow As Long
Dim columnForCount
startRow = namedRange.Row + 2
endRow = namedRange.Parent.Cells.Rows.Count
columnForCount = namedRange.Column
For currentNamedRange = LBound(namesArr, 2) To UBound(namesArr, 2)
With namedRange
Set tempRange = .Columns(currentNamedRange + 1).Offset(2, 0).Resize(.Rows.Count - 2, 1)
End With
tempRangeName = Trim$(namesArr(1, currentNamedRange))
wbTarget.Names.Add Name:=tempRangeName, _
RefersTo:="=OFFSET(" & sheetName & "!R" & startRow & "C" & columnForCount + currentNamedRange & _
",0,0,COUNTA(" & sheetName & "!R" & startRow & "C" & columnForCount & ":R" & endRow & "C" & _
columnForCount & "),1)"
Set tempRange = Nothing
tempRangeName = vbNullString
Next currentNamedRange
End Sub
Public Function GetNamedRangeNames(ByVal namedRange As Range) As Variant
Dim namesArr()
With namedRange.Rows(1)
namesArr = .Offset(, 1).Resize(1, .Columns.Count - 1).Value2
End With
GetNamedRangeNames = namesArr
End Function
第2版固定长度
Option Explicit
Public Sub CreateNamedRanges2(ByVal namedRange As Range, ByVal namesArr As Variant, Optional wbTarget As Workbook)
Dim currentNamedRange As Long
Dim tempRange As Range
Dim tempRangeName As String
If wbTarget Is Nothing Then Set wbTarget = ThisWorkbook
For currentNamedRange = LBound(namesArr, 2) To UBound(namesArr, 2)
With namedRange
Set tempRange = .Columns(currentNamedRange + 1).Offset(2, 0).Resize(.Rows.Count - 2, 1)
End With
tempRangeName = Trim$(namesArr(1, currentNamedRange))
wbTarget.Names.Add Name:=tempRangeName, RefersTo:=tempRange
Set tempRange = Nothing
tempRangeName = vbNullString
Next currentNamedRange
End Sub