简体   繁体   English

Excel VBA 中的范围运行时错误

[英]Range runtime error in Excel VBA

I believe I have everything else figured out but the line Set newSheetName = sht.Range("A1:A") is not pulling any information.我相信我已经弄清楚了其他所有内容,但是 Set newSheetName = sht.Range("A1:A") 行没有提取任何信息。 I am getting the Run-time error '1004': Method 'Range' of object'_Worksheet' failed.我收到运行时错误“1004”:对象“_Worksheet”的方法“范围”失败。 What am I missing?我错过了什么?

What I am trying to achieve is for this macro to look at the range in sheet "ARK_E_TEXAS" which is A1 through C23("ARK_E_TEXAS_LIST").我想要实现的是让这个宏查看工作表“ARK_E_TEXAS”中的范围,即 A1 到 C23(“ARK_E_TEXAS_LIST”)。 If A1:A has data it will create a new sheet and name that new sheet with the cell name.如果 A1:A 有数据,它将创建一个新工作表并使用单元格名称命名该新工作表。 I am using the Lastrow line to know how many lines to go down and the if function to skip over the blanks.我正在使用 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

Ok I got my answer with the help of @AlexWeber.好的,我在@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

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

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