删除和添加工作表会弄乱代码

时间:2019-05-03 14:37:51

标签: excel vba

我正在编写一个代码,以绘制不同工作表中的每一行(2-18)。我想每次按下按钮时都删除并添加新的工作表,以避免重复打印。该代码适用于注释掉的delete和add行,但是当我取消注释时,会收到运行时错误

For RowIndex = 2 To 18 'Repeats for each line
   sht = Cells(RowIndex, 2).Value 'Worksheet name is in column B
'   Worksheets(sht).Delete 'Deletes current worksheet for every tag
'   Worksheets.Add.Name = sht 'Creates blank new worksheet for every tag
'   Worksheets(sht).Move _
'   After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)   'Moves new sheet to end 
   CreateChart RowIndex, sht, DateColInd
Next RowIndex

'CODE GIVES ERROR AT SET XVALUES LINE

Sub CreateChart(Row, sht, DateColInd) 'DateColInd is the column index for today's date 
   Dim xValues As Range
   Dim yValues As Range
   Dim Target As Range
   Dim Limit As Range
   Dim cht As Chart
   Set xValues = Worksheets("Data Archive").Range(Cells(1, 3), Cells(1, 
   DateColInd)) 'Gets all data per given line

我希望它重置每行的所有工作表,每行都有一个图。注释掉线后,可以很好地创建图。但是当这些行未注释掉时,我会收到运行时错误,并且失败

0 个答案:

没有答案