2013-11-01 57 views
0

我有用於一個工作表的分隔列的代碼。我如何使這個代碼比循環表多張?用於多個工作表的Excel宏循環代碼

Sub Macro1() 

    Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _ 
     TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ 
     Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ 
     :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True 
    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select 
    Selection.NumberFormat = "dd-mm-yyyy;@" 

End Sub 

回答

1
Sub t() 
    Dim sh As Worksheet 
    For Each sh In ActiveWorkbook.Sheets 
     'do what you want, for example, 
     Range("A1:B10").TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _ 
      TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ 
      Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ 
      :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True 
     ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select 
     Selection.NumberFormat = "dd-mm-yyyy;@" 
    Next 
End Sub 

但是,你不應該使用Selection。您應該使用range來替代,例如Range("A1:B10")

0

嘗試像這樣

Sub forEachWs() 
Dim x As Long 
Dim ws As Sheets 

For x = 1 To 12'<--to as how many is required 
ThisWorkbook.Sheets(x).Activate 
    Call Macro1 '<---the name of your macro 
    Range("A2").Select 
End Sub