Excel VBA 试图创建一个新的工作表 [英] Excel VBA Trying to create a new worksheet

查看:59
本文介绍了Excel VBA 试图创建一个新的工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新工作表,然后我可以将数据逐个单元格添加到单元格中.我已经尝试过 Sheets.Add.Name 并且没有用.目前我收到运行时错误13"感谢任何帮助.

I want to create a new worksheet which I can then add data to cell by cell. I have tried the Sheets.Add.Name and it did not work. Currently I get a Run-time error "13" Appreciate any help.

ws.Name = "Sheet1" 在这种情况下.

ws.Name = "Sheet1" in this occasion.

Dim mySheet As Worksheet    
j = -1
i = 0
strCounter = 0
For Each ws In ThisWorkbook.Worksheets
    If (InStr(ws.Name, " Logix Friendly") = 0) Then
        ifChecker = True
    Else
        ifChecker = False
    End If
    If ifChecker Then

    Set mySheet = Workbooks.Add
        With mySheet
            .Nasme = ws.Name, " Logix Friendly"
        End With

推荐答案

Tim Williams 关于添加新工作表的观点绝对正确.

Tim Williams is absolutely correct about adding a new worksheet.

但是您的逻辑似乎有缺陷.您的代码仅在没有名为Logix Friendly"的工作表且工作簿中只有 1 个工作表时才有效.

However your logic appears to be flawed. Your code only works if there isn't a worksheet named "Logix Friendly" and there is only 1 sheet in the workbook.

你应该做什么:

For Each ws In ThisWorkbook.Worksheets
    bWS_Found = (InStr(1, ws.Name, "Logix Friendly", vbTextCompare) = 1)
    ' Exit checking if a worksheet already named "Logix Friendly"
    If bWS_Found Then Exit For
Next
' Add worksheet if there isn't a "Logix Friendly" worksheet
If Not bWS_Found Then
    With ThisWorkbook.Worksheets.Add
        .Name = "Logix Friendly"
    End With
End With

这篇关于Excel VBA 试图创建一个新的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆