简体   繁体   English

excel vba-将工作表名称插入一系列单元格

[英]excel vba - Inserting sheetname into a range of cells

trying to fill a range of blank cells with the name of the sheet that they are in. not sure why Range("ActiveSheet.Name") does not work? 试图用它们所在的工作表的名称填充一定范围的空白单元格。不确定为什么Range(“ ActiveSheet.Name”)不起作用? any help appreciated! 任何帮助表示赞赏!

Dim rng As Range
Set rng = Range("B1:B100")
For Each cell In rng
    If cell.Value <> "" Then
        cell.Offset(0, -1).Value = Range("ActiveSheet.Name")
    End If
Next

Try like this: 尝试这样:

If cell.value <> "" Then
    cell.Offset(0, -1) = ActiveSheet.name
End If

If you decide to get away from the ActiveSheet, this way is a bit fancier: 如果您决定不使用ActiveSheet,则这种方法可能会更有趣:

   cell.Offset(0, -1) = rng.Parent.name

or 要么

   cell.Offset(0, -1) = cell.Parent.name

您不需要使用Range...。只需使用以下命令:

cell.Offset(0, -1).Value = ActiveSheet.Name

尝试工作Sheets(i).Name ,其中i等于工作表的编号,而不是Range("ActiveSheet.Name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM