基于单元格创建新工作表但忽略空单元格

时间:2015-09-29 20:42:41

标签: excel vba excel-vba worksheet

我想在我的工作簿中创建一个新的工作表;根据"摘要"的C列(从C4开始)中的信息命名。工作表。到目前为止,我有以下VBA,但当它到达一个空白单元格时,它会停止。我需要它来忽略空白单元格并继续。有什么帮助吗?

Sub CreateSheetsFromAListTEST()

Dim MyCell As Range, MyRange As Range

Set MyRange = Sheets("Summary").Range("C4")
Set MyRange = Range(MyRange, MyRange.End(xlDown))

For Each MyCell In MyRange
Sheets.Add after:=Sheets(Sheets.Count) 'creates a new workbook
Sheets(Sheets.Count).Name = MyCell.Value 'renames the new workbook
Next MyCell

End Sub

1 个答案:

答案 0 :(得分:0)

替换

Set MyRange = Sheets("Summary").Range("C4")
Set MyRange = Range(MyRange, MyRange.End(xlDown))

For Each MyCell In MyRange
    Sheets.Add after:=Sheets(Sheets.Count) 'creates a new workbook
    Sheets(Sheets.Count).Name = MyCell.Value 'renames the new workbook
Next MyCell

通过

set MyRange=range(sheets("Summary").[c4],sheets("Summary").cells(rows.count,"C").end(xlup))

For Each MyCell In MyRange
    if len(mycell.text)>0 then 
        Sheets.Add after:=Sheets(Sheets.Count) 'creates a new workbook
        Sheets(Sheets.Count).Name = MyCell.Value 'renames the new workbook
    end if
Next MyCell