Board logo

標題: [發問] 篩選後新增工作表 [打印本頁]

作者: missbb    時間: 2016-11-14 19:55     標題: 篩選後新增工作表

本帖最後由 missbb 於 2016-11-14 19:56 編輯

我試驗列最後一步了, 就是可以用部門或員工編號篩選, 在篩選部門是完全無問題, 但歸選員工編號, 又多出一張工作表, 想來想去想不通, 請大大幫忙:'(

[attach]25801[/attach]
  1. Sub copytosheetok02()
  2. 'step select dept -> create appraisal form based on sheet result

  3. With Sheets("list").Activate

  4. Dim yn As Integer

  5. yn = MsgBox(prompt:="如果篩選部門, 請按是", Buttons:=vbYesNo + vbQuestion)
  6. If yn = vbYes Then

  7.     dept = InputBox("篩選部門:")
  8.     Range("a1").AutoFilter Field:=2, Criteria1:=dept
  9.     ActiveSheet.UsedRange.Select
  10.     Selection.copy
  11.     Sheets.Add After:=Sheets(Sheets.Count)
  12.     Sheets(Sheets.Count).Name = "result"
  13.     Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
  14.         xlNone, SkipBlanks:=False, Transpose:=False
  15.         
  16. Else
  17.     ID = InputBox("篩選編號:")
  18.     Range("a1").AutoFilter Field:=1, Criteria1:=ID
  19.     ActiveSheet.UsedRange.Select
  20.     Selection.copy
  21.     Sheets.Add After:=Sheets(Sheets.Count)
  22.     Sheets(Sheets.Count).Name = "result"
  23.     Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
  24.         xlNone, SkipBlanks:=False, Transpose:=False

  25. End If

  26. End With

  27. Dim MyCell As Range, MyRange As Range

  28. Set MyRange = Sheets("result").Range("A2")
  29. Set MyRange = Range(MyRange, MyRange.End(xlDown))

  30.     Application.ScreenUpdating = False
  31.     Application.DisplayAlerts = False

  32. For Each MyCell In MyRange
  33. Sheets("form").copy After:=Sheets(Sheets.Count) 'Create a new worksheet
  34. [color=Red]Sheets(Sheets.Count).Name = MyCell.Value 'Renames the new worksheet[/color]s
  35. '主要問題就出在這句了??????

  36. For i = 4 To Sheets.Count
  37.         With Sheets(i).Range("A1:E7")
  38.                  .Value = .Value
  39.         End With
  40.    
  41.           With Sheets(i).Range("A10:B11")
  42.                  .Value = .Value
  43.         End With
  44.     Next i
  45. Next MyCell

  46.     Application.ScreenUpdating = True
  47.     Application.DisplayAlerts = True

  48. End Sub
複製代碼

作者: 葉國洲    時間: 2016-11-15 18:44

37行改為
  1. Set MyRange = Sheets("result").Range("A2")
  2. If MyRange.Count > 1 Then Set MyRange = Range(MyRange, MyRange.End(xlDown))
複製代碼

作者: 葉國洲    時間: 2016-11-15 18:58

抱歉,樓上是錯的37行是
  1. If yn = vbYes Then Set MyRange = Range(MyRange, MyRange.End(xlDown))
複製代碼

作者: missbb    時間: 2016-11-16 14:19     標題: RE: 篩選後新增工作表

回復 3# 葉國洲

感謝!:lol




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)