Excel VBA中的范围运行时错误

时间:2015-11-02 19:37:41

标签: excel vba excel-vba

我相信我已经知道了其他一切,但是设置newSheetName = sht.Range(“A1:A”)这一行并没有提取任何信息。我得到运行时错误'1004':对象'_Worksheet'的方法'范围'失败。我错过了什么?

我想要实现的是这个宏查看工作表“ARK_E_TEXAS”中的范围是A1到C23(“ARK_E_TEXAS_LIST”)。如果A1:A有数据,它将创建一个新工作表并将该工作表命名为带有单元名称的新工作表。我正在使用Lastrow线来知道要下降多少行以及if函数以跳过空白。

Sub Create_ARK_E_TEXAS()
    Dim sht As Worksheet
    Dim newSheetName As Range
    Dim dataRange As Range
    Dim Lastrow As Long

    Set sht = ThisWorkbook.Sheets("ARK_E_TEXAS")
    Set newSheetName = sht.Range("A1:A")
    Lastrow = sht.Range("ARK_E_TEXAS_LIST").Rows.Count
    Set dataRange = sht.Range("A1:C" & Lastrow)

For Each newSheetName In dataRange
    If newSheetName.Value <> "" Then
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = newSheetName.Value ' renames the new worksheet
    End If
Next newSheetName
End Sub

1 个答案:

答案 0 :(得分:-1)

好的,我在@AlexWeber的帮助下得到了答案。

Sub Create_ARK_E_TEXAS()


Dim sht As Worksheet
Dim newSheetName As Range
Dim dataRange As Range
Dim Lastrow As Long

Set sht = ThisWorkbook.Sheets("ARK_E_TEXAS")
Lastrow = sht.Range("ARK_E_TEXAS_LIST").Rows.Count
Set newSheetName = sht.Range("A1:A" & Lastrow)
Set dataRange = sht.Range("A1:A" & Lastrow)

For Each newSheetName In dataRange
If newSheetName.Value <> "" Then
    Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
    Sheets(Sheets.Count).Name = newSheetName.Value ' renames the new worksheet
End If
Next newSheetName
End Sub