Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

excel - create multiple named ranges with multiple selection

i just get one names range with this code, what's my fault?

any help, my language is so bad, sorry!

Sub Create_Names()

Worksheets("DATA").Activate

Dim rng As Range
With ActiveSheet
    Set rng = Range("J2:J10, J47:S67")
End With
rng.Select
With Selection

'Set rng = Selection

Dim i As Integer
Dim n As Long
Dim new_range As Range

Dim col_num As Integer
Dim first_Row As Long
Dim last_row As Long

For i = 1 To rng.Columns.Count
    For n = rng.Rows.Count To 1 Step -1
       col_num = rng.Columns(i).Column
       first_Row = rng.Rows(1).Row
       last_row = rng.Rows(n).Row

       If Cells(last_row, col_num).Value <> "" Then
           Set new_range = Range(Cells(first_Row, col_num), Cells(last_row, col_num))
           new_range.CreateNames Top:=True
           Exit For
      End If
    Next n
Next i
End With
End Sub

i have a big data, and i want to create names range once to make it simple.. help me please..

i change my code and its work like i want..

for each rng in Application.Selection.Areas

'i run the code here


next rng

IS THERE LIMIT FOR CREATENAMES?

I GET ERROR WHEN I PUT

Set rng = Range("J2:J10, J47:S67,V47:BI77,BL1:BL21,CB35:CU64,CB120:FW170,CX20:MM35,CX51:EU61") 

My data

my name range

question from:https://stackoverflow.com/questions/66053391/create-multiple-named-ranges-with-multiple-selection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...