excel 2010的宏用于自动化名称管理器

时间:2013-03-06 08:26:02

标签: excel excel-vba vba

我的要求是在macro中创建一个excel 2010,它会使用Name Manager(功能区>公式>名称管理器)自动为等距单元格指定或创建名称。

For e.g. Cell A1 to be named as Name_1, Cell A11 to be named as Name_2, Cell A21 to be named as Name_3 and so on.

希望这是有道理的,否则请求澄清。谢谢!

1 个答案:

答案 0 :(得分:0)

彼得,你干练的指导帮助我取得了成功(也得到了Siddharth Rout的帮助!):)谢谢!请在下面找到我的代码 -

Private Sub CommandButton1_Click()

'Define the variables
Dim vRangeDefined, vRowCount, vRowIndex, vColIndex, vCounter, vCellValue As String, vNameValue As String

'Define the range where the values are entered
vRangeDefined = ActiveSheet.Range("A:B").Value
vRowCount = ActiveSheet.UsedRange.Rows.Count

For vCounter = 2 To vRowCount
    vCellValue = vRangeDefined(vCounter, 1)
    vNameValue = vRangeDefined(vCounter, 2)
    'Divide the Cell Value in two parts
    vRowIndex = Left(vCellValue, 1)
    vColIndex = Right(vCellValue, Len(vCellValue) - 1)
    'MsgBox vRowIndex & "-" & vColIndex

   'Assign the names to cells as per the range
    ActiveWorkbook.Names.Add _
        Name:=vNameValue, _
        RefersTo:="='Sheet1'!$" & vRowIndex & "$" & vColIndex

Next

MsgBox "Process complete..."

End Sub