我已经创建了一个宏来从列表中创建工作表,这个工作正常,但我有一个问题,如果我在列表中只有一个项目我得到一个错误,这里是宏:
public class AbstractQueryCommand<T> implements IQueryCommand<T> {
abstract protected String getResult(ResultSet rs) throws SQLException;
...
}
return new AbstractQueryCommand<String>() {
@Override
protected String getResult(ResultSet rs) throws SQLException {
String result = "";
while (rs.next()) {
result = rs.getString("DESCRIPTION");
}
return result;
};
}
答案 0 :(得分:0)
您应该使用xlUp
而不是xlDown
,这样更安全!
您之前选择了整个列(从第14行开始,直到工作表结尾!)
这将顺利进行! ;)
Sub CreateSheetsFromAList()
Application.ScreenUpdating = False
Dim wsM As Worksheet, wsSR As Worksheet
Dim MyCell As Range, MyRange As Range, LastRow As Double
Set wsM = ThisWorkbook.Sheets("Master")
Set wsSR = ThisWorkbook.Sheets("Stock Removal")
wsM.Select
wsSR.Visible = True
Set MyRange = wsM.Range("A14")
LastRow = wsM.Range("A" & wsM.Rows.Count).End(xlUp).Row
If LastRow > 14 Then
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
wsSR.Copy after:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
Next MyCell
Else
wsSR.Copy after:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyRange.Value ' renames the new worksheet
End If
wsSR.Select
ActiveWindow.SelectedSheets.Visible = False
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
问题是如果只有Cell A14有数据,并且整个列A(在单元格A14下面)是空白的,那么MyRange.End(xlDown)
将导致&#34; A1048576&#34;。因此,您需要找到A列中的最后一行,然后检查它是否是14&gt;&gt;如果是,则MyRange
应包含1个单元格,以及该单元格A14。
请尝试使用以下代码替换Set MyRange
的方式:
With Sheets("Master")
If .Cells(.Rows.Count, "A").End(xlUp).Row = 14 Then ' if only cell A14 has data in entire Column A
Set MyRange = Sheets("Master").Range("A14")
Else
Set MyRange = Sheets("Master").Range("A14", Range("A14").End(xlDown))
End If
End With
答案 2 :(得分:0)
尝试更改:
Set MyRange = Sheets("Master").Range("A14")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
要:
With Sheets("Master")
Set MyRange = .Range(Range("A14"), .Range("A" & .Range("A" & .Rows.Count).End(xlUp).row))
End With