需要获取单元格值并自动将其声明为行范围

时间:2017-08-02 10:40:55

标签: excel-vba excel-formula vba excel

使用vba的名称管理员... 我需要逐一从列中获取单元格值,并且必须将该名称声明到该列旁边的该行范围

示例

D 栏中,我有名单

我必须得到D1值并将该值声明为行范围( E1:S1 )

下一步

必须D2 ---> E2:S2

2 个答案:

答案 0 :(得分:0)

这是前5行的方法:

For i = 1 To 5
    ThisWorkbook.Names.Add Name:=yourWorksheet.Cells(i, 4).Value, RefersTo:=yourWorksheet.Range(yourWorksheet.Cells(i, 5), yourWorksheet.Cells(i, 19))
Next

请记住,名称必须是唯一的

答案 1 :(得分:0)

尝试以下代码

Sub AddNamedRange()
    Dim cel As Range
    Dim ws As Worksheet
    Dim lastRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet4")  'change Sheet4 to your sheet name
    With ws
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row    'last row with data in Column D
        For Each cel In .Range("D1:D" & lastRow)     'loop through all cell in Column D
            ThisWorkbook.Names.Add cel, ws.Range(cel.Offset(, 1), cel.Offset(, 15)) 'adding named range
        Next
    End With
End Sub