2016-07-24 68 views
0

当我运行我的代码不止一次时,它将重复工作表中的结果。我需要删除以前的数据,并在每次运行时粘贴新数据。使我的宏不重复结果

Sub CreateMonthlySheets() 
    Dim lastRow, mMonth, tstDate1, tstDate2, shtName, nxtRow 

    On Error Resume Next 
    'Turn off ScreenUpdating 
    Application.ScreenUpdating = False 
    'Make a copy of the data sheet and sort by date 
    Sheets("Main Data Sheet").Copy After:=Sheets(1) 
    Sheets(2).Name = "SortTemp" 
    With Sheets("SortTemp") 
     lastRow = .Cells(Rows.Count, 1).End(xlUp).Row 
     Rows("2:" & lastRow).Sort Key1:=Range("C2"), Order1:=xlAscending 

     'Using SortTemp Sheet, create monthly sheets by 
     'testing Month and Year values in Column A 

     'Loop through dates 
     For Each mMonth In .Range("C2:C" & lastRow) 
      tstDate1 = Month(mMonth) & Year(mMonth) 
      tstDate2 = Month(mMonth.Offset(-1, 0)) & Year(mMonth.Offset(-1, 0)) 

      'If Month and Year are different than cell above, create new sheet 
      If tstDate1 <> tstDate2 Then 
       ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count) 


       'Name the sheet based on the Month and Year 
       ActiveSheet.Name = MonthName(Month(mMonth)) & " " & Year(mMonth) 
       'Copy Column Widths and Header Row 
       .Rows(1).Copy 
       ActiveSheet.Rows(1).PasteSpecial Paste:=8 'ColumnWidth 
       ActiveSheet.Rows(1).PasteSpecial  'Data and Formats 
      End If 
     Next 
     On Error GoTo 0 

     'Loop through dates, copying row to the correct sheet 
     For Each mMonth In .Range("C2:C" & lastRow) 
      'Create sheetname variable 
      shtName = MonthName(Month(mMonth)) & " " & Year(mMonth) 
      'Determine next empty row in sheet 
      nxtRow = Sheets(shtName).Cells(Rows.Count, 1).End(xlUp).Row + 1 
      'Copy Data 
      .Range(mMonth.Address).EntireRow.Copy Destination:=Sheets(shtName).Cells(nxtRow, 1) 
     Next 
    End With 
    'Delete SortTemp sheet 
    Application.DisplayAlerts = False 
    Sheets("SortTemp").Delete 
    Application.DisplayAlerts = True 
    'Turn on ScreenUpdating 
    Application.ScreenUpdating = True 
End Sub 
+0

请提供数据 – EBH

+0

的一例我有数据的行中的一个具有时间(主片1/1/2016,13/2/2016,17/6/2016)我的代码为每个月创建工作表。当我运行我的代码时,它会正常工作创建带月份名称的工作表,每个工作表包含本月的数据,但是当我再次运行它时,它将复制创建的工作表中的数据并创建另一个工作表范围。我想要的是(如果工作表存在首先删除表中存在的所有数据,然后将数据移动到工作表) – daniel

+0

你说_“行有日期”_但你的代码循环通过一列...我假设日期在列“主数据表”的“C”,并粘贴到“SortTemp” – user3598756

回答

0

我找到了解决办法>>感谢对于所有

Option Explicit 

Sub CreateMonthlySheets() 
    Dim mMonth As Range 
    Dim shtName As String 
    Dim monthSht As Worksheet 
    Dim newSheet As Boolean 


' 'Turn off ScreenUpdating 
    Application.ScreenUpdating = False 
    'Make a copy of the data sheet and sort by date 

    With GetSheet("SortTemp", True, newSheet) '<-- get your "temp" sheet: if not existent then create it 
     If Not newSheet Then .Cells.Clear '<--| if it existed then clear it 
     Sheets("Main Data Sheet").UsedRange.Copy Destination:=.Cells(1, 1) '<--| fill it with "Main Data Sheet" sheet 

     'Using SortTemp Sheet, create monthly sheets by 
     'testing Month and Year values in Column A 

     'Loop through dates 
     For Each mMonth In .Range("C2:C" & .Cells(.Rows.Count, 1).End(xlUp).Row) 
      shtName = MonthName(Month(mMonth)) & Year(mMonth) '<--| build "month" sheet name 
      Set monthSht = GetSheet(shtName, False, newSheet) 'Set "month" sheet: if not existent then create it 
      monthSht.UsedRange.Offset(1).Clear 

     Next 


     For Each mMonth In .Range("C2:C" & .Cells(.Rows.Count, 1).End(xlUp).Row) 
      shtName = MonthName(Month(mMonth)) & Year(mMonth) '<--| build "month" sheet name 
      Set monthSht = GetSheet(shtName, False, newSheet) 'Set "month" sheet: if not existent then create it 
      ' monthSht.UsedRange.Offset(1).Clear 
      ' If newSheet Then '<--| if it didn't exist... 
       '...Copy Column Widths and Header Row 
       .Rows(1).Copy 
       monthSht.Rows(1).PasteSpecial Paste:=8 'ColumnWidth 
       monthSht.Rows(1).PasteSpecial   'Data and Formats 
     ' Else 'otherwise... 
       'monthSht.UsedRange.Offset(1).Clear '<--| ...clear it from row 2 downwards (assuming row 1 has at least one value...) 
     ' End If 
      'Copy Data 
      mMonth.EntireRow.Copy Destination:=monthSht.Cells(monthSht.Rows.Count, 1).End(xlUp).Offset(1) 
     Next 

    End With 
    'Delete SortTemp sheet 
    Application.DisplayAlerts = False 
    Sheets("SortTemp").Delete 
    Application.DisplayAlerts = True 
    'Turn on ScreenUpdating 
    Application.ScreenUpdating = True 
End Sub 


Function GetSheet(shtName As String, Optional okClear As Boolean = False, Optional newSheet As Boolean = False) As Worksheet 
    On Error Resume Next 
    Set GetSheet = Worksheets(shtName) 
    On Error GoTo 0 
    If GetSheet Is Nothing Then 
     newSheet = True 
     Set GetSheet = ActiveWorkbook.Sheets.Add(After:=Worksheets(Worksheets.Count)) 
     GetSheet.Name = shtName 
    Else 
     If okClear Then GetSheet.Cells.Clear 
     newSheet = False 
    End If 
End Function 
+0

你拿我的答案,张贴它,并标记为您的解决方案?请让事情恢复公平!谢谢 – user3598756

+0

很高兴能够为尝试和帮助你的人提供适当的反馈意见 – user3598756

0

试试这个

Option Explicit 

Sub CreateMonthlySheets() 
    Dim mMonth As Range 
    Dim shtName As String 
    Dim monthSht As Worksheet 
    Dim newSheet As Boolean 

' 'Turn off ScreenUpdating 
    Application.ScreenUpdating = False 
    'Make a copy of the data sheet and sort by date 
    With GetSheet("SortTemp", True, newSheet) '<-- get your "temp" sheet: if not existent then create it 
     If Not newSheet Then .Cells.Clear '<--| if it existed then clear it 
     Sheets("Main Data Sheet").UsedRange.Copy Destination:=.Cells(1, 1) '<--| fill it with "Main Data Sheet" sheet 

     'Using SortTemp Sheet, create monthly sheets by 
     'testing Month and Year values in Column A 

     'Loop through dates 
     For Each mMonth In .Range("C2:C" & .Cells(.Rows.Count, 1).End(xlUp).row) 
      shtName = MonthName(Month(mMonth)) & " " & Year(mMonth) '<--| build "month" sheet name 
      Set monthSht = GetSheet(shtName, False, newSheet) 'Set "month" sheet: if not existent then create it 
      If newSheet Then '<--| if it didn't exist... 
       '...Copy Column Widths and Header Row 
       .Rows(1).Copy 
       monthSht.Rows(1).PasteSpecial Paste:=8 'ColumnWidth 
       monthSht.Rows(1).PasteSpecial   'Data and Formats 
      Else 'otherwise... 
       monthSht.UsedRange.Offset(1).Clear '<--| ...clear it from row 2 downwards (assuming row 1 has at least one value...) 
      End If 
      'Copy Data 
      mMonth.EntireRow.Copy Destination:=monthSht.Cells(monthSht.Rows.Count, 1).End(xlUp).Offset(1) 
     Next 

    End With 
    'Delete SortTemp sheet 
    Application.DisplayAlerts = False 
    Sheets("SortTemp").Delete 
    Application.DisplayAlerts = True 
    'Turn on ScreenUpdating 
    Application.ScreenUpdating = True 
End Sub 

'Sub main() 
' Dim sh As Worksheet 
' Dim existent As Boolean 
' 
' Set sh = GetSheet("data1", False, existent) 
' 
'End Sub 

Function GetSheet(shtName As String, Optional okClear As Boolean = False, Optional newSheet As Boolean = False) As Worksheet 
    On Error Resume Next 
    Set GetSheet = Worksheets(shtName) 
    On Error GoTo 0 
    If GetSheet Is Nothing Then 
     newSheet = True 
     Set GetSheet = ActiveWorkbook.Sheets.Add(After:=Worksheets(Worksheets.Count)) 
     GetSheet.Name = shtName 
    Else 
     If okClear Then GetSheet.Cells.Clear 
     newSheet = False 
    End If 
End Function 

从结果:

  • 避免On Error Resume Next执政超过严格需要
  • 无需环路两次
+0

亲爱的@ user3598756,我的代码创建范围与月manes工作表时,我运行你的代码它会给我错误在这个代码'monthSht.Range(。行(2),.Rows(Rows.Count))。删除'< - | ...从第2行向下清除...' – daniel

+0

将''删除'变为'清除'(参见编辑答案)。如果错误再次出现,请指定它是哪种错误 – user3598756

+0

请参阅编辑的代码,其中我还更改了将“SortTemp”表单复制到“月份”表单中的语句 – user3598756