首页 > 解决方案 > 使用多项选择创建多个命名范围

问题描述

我只是用这个代码得到一个名字范围,我的错是什么?

任何帮助,我的语言太糟糕了,对不起!

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

我有一个大数据,我想创建一次名称范围以使其简单..请帮助我..

我改变我的代码和它的工作就像我想要的..

for each rng in Application.Selection.Areas

'i run the code here


next rng

创建名称有限制吗?

我放的时候出错了

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

我的数据

我的姓名范围

标签: excelvba

解决方案


推荐阅读