【问题标题】:Excel vba set style in new sheet with a tableExcel vba 在带有表格的新工作表中设置样式
【发布时间】:2016-08-14 15:49:00
【问题描述】:

如何纠正此代码? 我会在新工作表中设置样式,但此代码给出 1004 错误

Sub test()
Sheets.Add.Name = "new"
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(3, 3)) = "test"
ActiveSheet.ListObjects.Add(xlSrcRange, Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(3, 3)), , xlYes, , "TableStyleMedium7").Name = "sk"
End Sub

【问题讨论】:

  • 你想做什么?
  • 在新工作表中设置样式但此代码给出 1004error
  • @sajad 你想用这条线做什么Sheets("new").DeleteSheets.Add.Name = "new"???
  • @ShaiRado 现已正确编辑
  • 它适用于我用 Excel 2016 编写的。

标签: excel stylesheet vba


【解决方案1】:

我可以模拟您的错误的唯一原因是您的工作簿中已经有一个名为“new”的工作表。如果是这种情况,请尝试以下代码:

Sub test()

Dim sht                     As Worksheet
Dim shtNewExists            As Boolean

shtNewExists = False

' loop through workbook sheet to search if "new" already exists
For Each sht In ThisWorkbook.Sheets
    If sht.Name = "new" Then
        shtNewExists = True
    End If
Next sht

' is sheet "new" doesn't exist then create one
If Not shtNewExists Then Sheets.Add.Name = "new"

Set sht = ThisWorkbook.Sheets("new")

sht.Range(sht.Cells(1, 1), sht.Cells(3, 3)) = "test"
sht.ListObjects.Add(xlSrcRange, sht.Range(sht.Cells(1, 1), sht.Cells(3, 3)), , xlYes, , "TableStyleMedium7").Name = "sk"

End Sub

【讨论】:

  • 感谢您的回答,但对于这一行错误: ActiveSheet.ListObjects.Add(xlSrcRange, Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(3, 3)), , xl是, , "TableStyleMedium7").Name = "sk"
  • 为您解答问题
猜你喜欢
  • 2016-05-09
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
相关资源
最近更新 更多