1

我正在尝试使用 Excel 自动执行以下过程,但我遇到了一些困难,因为显然我需要在 OFFSET 函数中设置一个变量:

Sheets("XXX").Visible = True
Sheets("XXX").Select
ActiveWorkbook.Names.Add Name:="XXX_aaa", RefersToR1C1:= _
    "=OFFSET('XXX'!R2C1,0,1,COUNTA('XXX'!C1),21)"
Sheets("XXX").Visible = False

Sheets("YYY").Visible = True
Sheets("YYY").Select
ActiveWorkbook.Names.Add Name:="YYY_bbb", RefersToR1C1:= _
    "=OFFSET('YYY'!R2C1,0,1,COUNTA('YYY'!C1),21)"
Sheets("YYY").Visible = False

Sheets("ZZZ").Visible = True
Sheets("ZZZ").Select
ActiveWorkbook.Names.Add Name:="ZZZ_ccc", RefersToR1C1:= _
    "=OFFSET('ZZZ'!R2C1,0,1,COUNTA('ZZZ'!C1),21)"
Sheets("ZZZ").Visible = False`

是否有一个简单的宏功能可以用来自动执行此任务(必须重复 30 次!)

谢谢 !

4

1 回答 1

1

在您正在使用的工作簿的副本上尝试此操作:

Sub Sample()
Dim intCurrentSheet As Integer
Dim lngLastRow As Long

For intCurrentSheet = 2 To 31

    lngLastRow = Sheets(intCurrentSheet).Range("U1048576").End(xlUp).Row

    Sheets(intCurrentSheet).Range("A2:U" & lngLastRow).Name = Sheets(intCurrentSheet).Name & _
                                                           "_" & Chr(intCurrentSheet + 63) & _
                                                           Chr(intCurrentSheet + 63) & _
                                                           Chr(intCurrentSheet + 63)
    Sheets(intCurrentSheet).Visible = False

Next intCurrentSheet

End Sub
于 2013-06-11T16:20:02.527 回答