Board logo

標題: [發問] 請問此段巨集如何簡化或加快速度?? [打印本頁]

作者: t8899    時間: 2013-9-6 06:36     標題: 請問此段巨集如何簡化或加快速度??

本帖最後由 t8899 於 2013-9-6 06:44 編輯

Sub zzzzz()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False

With Sheets("Sheet6")

     Range("D16").Select
    Selection.Copy
    Range("b65536").End(xlUp).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False

           Range("C16").Select
    Selection.Copy
    Range("O65536").End(xlUp).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  
    Range("C18").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("P65536").End(xlUp).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

             Range("B16").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("Q65536").End(xlUp).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
end with
end sub
---------------------------------------------------------------------------------------------
另外 此兩個可否放在with 裡面
1.執行另一個巨集
2.Sheets("Sheet3").Range("Z2:Z111").Value = Sheets("Sheet3").Range("C2:C111").Value (是否非同一個物件?)
作者: GBKEE    時間: 2013-9-6 07:15

回復 1# t8899
此兩個可否放在with 裡面,試試看吧!
  1. Option Explicit
  2. Sub zzzzz()
  3. Application.ScreenUpdating = False
  4. Application.EnableEvents = False
  5. Application.DisplayStatusBar = False
  6. ActiveSheet.DisplayPageBreaks = False
  7. With Sheets("Sheet1")
  8.      .Range("B65536").End(xlUp).Offset(1) = .Range("D16").Value
  9.      .Range("O65536").End(xlUp).Offset(1) = .Range("C16").Value
  10.      .Range("P65536").End(xlUp).Offset(1) = .Range("C18").Value
  11.      .Range("Q65536").End(xlUp).Offset(1) = .Range("B16").Value
  12. End With
  13. End Sub
複製代碼

作者: t8899    時間: 2013-9-6 08:35

本帖最後由 t8899 於 2013-9-6 08:42 編輯
回復  t8899
此兩個可否放在with 裡面,試試看吧!
GBKEE 發表於 2013-9-6 07:15

選擇性貼上有10個參數
您的寫法只有貼上"值" 的部份
一般用的是
1值
2值與數字格式
3格式
4全部
這4個個別"語法"是?(套在你寫的裡面)
Option Explicit 檢查,如果未指定 On 或 Off,則預設值為 On ,應該不用掛?
另外 下面兩個可否放在with 裡面
1.執行另一個巨集
2.Sheets("Sheet3").Range("Z2:Z111").Value = Sheets("Sheet3").Range("C2:C111").Value (是否非同一個物件?)
作者: GBKEE    時間: 2013-9-6 09:25

本帖最後由 GBKEE 於 2013-9-6 12:42 編輯

回復 3# t8899
另外 下面兩個可否放在with 裡面   試試就知道(你試過嗎?)
是否非同一個物件? 請再說明白
  1. Option Explicit 陳述式
  2. 在模組層次中強迫每個在模組�堛瘍僂くㄔ眸楨�確的宣告。
  3. 語法
  4. Option Explicit
  5. 請注意
  6. Option Explicit 陳述式必出現在模組�堙A且在任何程序之前。
  7. 當 Option Explicit 在模組中使用時,必須以 Dim, Private、Public、ReDim 或 Static 陳述式來明確宣告所有的變數。如果使用未宣告的變數名稱,則在編譯階段時,會發生錯誤。
  8. 如果未使用 Option Explicit 陳述式,則所有未宣告的變數都是 Variant 型態,除非預設的型態另以 Deftype 陳述式所指定過。
  9. 附註 Option Explicit 可避免打錯變數名稱或在有效範圍內相同的變數名稱。
複製代碼
  1. Option Explicit
  2. Sub Ex()
  3.     [F8:F14].Clear
  4.     With Range("D4")
  5.         .Cells = "=888"
  6.         .NoteText "註解文字"                                     '註解
  7.         .Interior.ColorIndex = 8
  8.         .Copy Range("F8")                                        '全部
  9.         Range("F9").Value = .Value                               '值
  10.         Range("F10").Formula = .Formula                          '公式
  11.         Range("F11").NumberFormatLocal = .NumberFormatLocal      '數字格式
  12.         Range("F12").Interior.ColorIndex = .Interior.ColorIndex  '圖樣 色彩
  13.         Range("F13") = .Value
  14.         Range("F13").Font.Size = .Font.Size + 2                  '字型大小
  15.         Range("F14").NoteText .NoteText                          '註解
  16.     End With
  17. End Sub
複製代碼

作者: t8899    時間: 2013-9-6 10:07

回復  t8899
另外 下面兩個可否放在with 裡面   試試就知道(你試過嗎?)
GBKEE 發表於 2013-9-6 09:25


我試過下面這一段放在With Sheets("Sheet1")裡,是不會出現錯誤,但會造成程式跑的不對(會跑到非指定的工作表做動作)
run clear ======>應該是這個出問題
For i = 350 To 21 Step -1
  If IsError(Cells(i, "h")) Then Run "clear": Exit For
next
所以答案是不行
作者: GBKEE    時間: 2013-9-6 10:23

回復 5# t8899
  1. For i = 350 To 21 Step -1
  2.   If IsError(Cells(i, "h")) Then Run "clear": Exit For
  3. next
複製代碼
IsError(Cells(i, "h"))  是 With Sheets("Sheet1") 裡的Cells(i, "h") 嗎?,
不是要指明工作表  IsError(Sheets("工作表名稱").Cells(i, "h"))

Run "clear"  這是 VBA的關鍵字 Clear 方法,最好換個字串
另外 Run 的程序在物件模組(不在一般模組裡)      要指明模組 Run "Sheet1.程序"
作者: t8899    時間: 2013-9-6 10:50

回復  t8899 IsError(Cells(i, "h"))  是 With Sheets("Sheet1") 裡的Cells(i, "h") 嗎?,
不是要指明工作表 ...
GBKEE 發表於 2013-9-6 10:23


IsError(Cells(i, "h"))  是 With Sheets("Sheet1") 裡的Cells(i, "h") 嗎?  

是的!
Run "clear"  這是 VBA的關鍵字 Clear 方法,最好換個字串  

謝謝!已改為 ACLEAR

有無必要用WITH ? 我用的結果感覺不出有比較快
作者: GBKEE    時間: 2013-9-6 10:55

回復 7# t8899
是With Sheets("Sheet1") 裡的Cells(i, "h") 就可用With. (快不快??? 不知你的想法)
作者: t8899    時間: 2013-9-6 12:31

本帖最後由 t8899 於 2013-9-6 12:33 編輯
回復  t8899
是With Sheets("Sheet1") 裡的Cells(i, "h") 就可用With. (快不快??? 不知你的想法)
GBKEE 發表於 2013-9-6 10:55


  Range("F10").Formula = .Formula           '公式
  Range("F11").FormulaLocal = .FormulaLocal  '數字格式
試的結果,這兩個都是公式????
這下面兩個的語法?
1.copy 值與數字格式
2.copy 格式

Sub Macro1()
With Range("D2")
Range("B65536").End(xlUp).Offset(1) = ????
End With
End Sub
作者: GBKEE    時間: 2013-9-6 12:41

回復 9# t8899
就這樣多練習,會進步的
更正(燒餅吃多了)
  1. Range("F11").NumberFormatLocal = .NumberFormatLocal      '數字格式
複製代碼

作者: acdx    時間: 2013-9-6 15:35

回復 2# GBKEE
我也有一段巨集該如簡化?
Private Sub format()

Dim ws As Worksheet
Dim sName As String

sName = "PTAVS"
On Error Resume Next
Set ws = Sheets(sName)

If ws Is Nothing Then
    Worksheets.Add after:=Worksheets(Worksheets.Count)
    Worksheets(Worksheets.Count).Name = sName
    ws.Activate
Else
    MsgBox sName & "工作表已存在。"
    Sheets("Result").Select
    Exit Sub
End If

Cells.Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection.Font
        .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With

Range("B1:M1").Merge
    Range("A1:A4").Select
    With Selection
        .WrapText = False
        .MergeCells = True
        .Value = "C1~C5"
    End With
   
    Range("B2:G2").Select
    With Selection
        .WrapText = False
        .MergeCells = True
        .Value = "(sone)"
    End With
   
    Range("B3:D3").Select
    With Selection
        .WrapText = False
        .MergeCells = True
        .Value = "H"
    End With
   
    Range("E3:G3").Select
    With Selection
        .WrapText = False
        .MergeCells = True
        .Value = "M"
    End With
   
    Range("B4:G4").Select
    With Selection
        .WrapText = True
        .MergeCells = False
    End With
    Range("B4").Value = "mean"
    Range("C4").Value = "standard deviation"
    Range("D4").Value = "mean+CV*stdev"
   
    Range("B4:D4").Copy
    Range("E4").Select
    ActiveSheet.Paste
   
    Range("B2:G4").Copy
    Range("H2").Select
    ActiveSheet.Paste
   
    Range("H2").Value = "(tu)"
   
    Range("A1:M4").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Font
        .Name = "Arial"
        .Size = 10
    End With
Sheets("Result").Select
End Sub
'-------------------------------------
這段code有很大的部分是在做儲存格的合併以及畫框線 這該如何簡化呢?
作者: GBKEE    時間: 2013-9-6 17:04

回復 11# acdx
  1. Option Explicit
  2. Private Sub format()
  3.     Dim ws As Worksheet, sName As String, AR(1 To 2), I As Integer
  4.     sName = "PTAVS"
  5.     On Error Resume Next
  6.     Set ws = Sheets(sName)
  7.     If ws Is Nothing Then
  8.         Worksheets.Add after:=Worksheets(Worksheets.Count)
  9.         Worksheets(Worksheets.Count).Name = sName
  10.         Set ws = Sheets(sName)
  11.     Else
  12.         MsgBox sName & "工作表已存在。"
  13.         Sheets("Result").Select
  14.         Exit Sub
  15.     End If
  16.     With ws.Cells
  17.         .HorizontalAlignment = xlCenter
  18.         .VerticalAlignment = xlCenter
  19.         .Orientation = 0
  20.         .AddIndent = False
  21.         .IndentLevel = 0
  22.         .ShrinkToFit = False
  23.         .ReadingOrder = xlContext
  24.         .MergeCells = False
  25.         With .Font
  26.             .Name = "Arial"
  27.             .Size = 10
  28.             .Strikethrough = False
  29.             .Superscript = False
  30.             .Subscript = False
  31.             .OutlineFont = False
  32.             .Shadow = False
  33.             .Underline = xlUnderlineStyleNone
  34.             .ColorIndex = xlAutomatic
  35.             .TintAndShade = 0
  36.             '.ThemeFont = xlThemeFontNone  '2003沒這參數
  37.         End With
  38.         .Range("B1:M1").Merge
  39.         AR(1) = Array("A1:A4", "B2:G2", "B3:D3", "E3:G3", "B4:G4")
  40.         AR(2) = Array("C1~C5", "(sone)", "H", "M", "")
  41.         For I = 0 To UBound(AR(1))
  42.             With .Range(AR(1)(I))
  43.                 .WrapText = False
  44.                 .MergeCells = IIf(I < UBound(AR(1)), True, False)
  45.                 .Value = AR(2)(I)
  46.             End With
  47.         Next
  48.         .Range("B4:D4").Value = Array("mean", "standard deviation", "mean+CV*stdev")
  49.         .Range("B4:D4").Copy .Range("E4")
  50.         .Range("B2:G4").Copy .Range("H2")
  51.         .Range("H2").Value = "(tu)"
  52.         With .Range("A1:M4")
  53.             For I = 5 To 12   '畫框線
  54.                 With .Borders(I)
  55.                     .LineStyle = IIf(I >= 7, xlContinuous, xlNone)
  56.                     If I >= 7 Then .ColorIndex = 0
  57.                     'If I >= 7 Then .TintAndShade = 0       '2003框線沒這屬性
  58.                     If I >= 7 Then .Weight = xlThin
  59.                 End With
  60.             Next
  61.             .Font.Name = "Arial"
  62.             .Font.Size = 10
  63.         End With
  64.     End With
  65.     Sheets("Result").Select
  66. End Sub
複製代碼

作者: acdx    時間: 2013-9-8 15:32

回復 12# GBKEE
每次來這邊求救總是能從版上各位前輩的解答中獲得我未想過的寫法,讓我也從中學到不少
在這裡也想請G大介紹幾本不錯的工具書讓我能從中精進自己的功力
希望將來有一天不再只是上來這邊發問也能向板上幾位大大可以幫其他人解惑 謝謝
作者: GBKEE    時間: 2013-9-8 15:53

回復 13# acdx
工具書一般都是一樣的,多看多練習,日久候一定可以替後人解惑的.




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