2014-04-25 72 views
0
Sub Macro5() 
    Dim filename As String 
    Dim MyRefAddr As String 
    Dim MyCopyAddr As String 
    Dim MyDestAddr As String 
    Dim NoOfCols As Integer 
    Dim lastRow As Long 
    Dim Distributor As String 
    Dim Z As Double 
    Dim Davg As Double 
    Dim LT As Long 
    Dim temp As Long 

    Worksheets.Add(Before:=Worksheets(1)).Name = "Norms" 
    Sheets("Norms").Select 
    Range("A1").Select 
    ActiveCell.Value = "SKU" 
    MyCopyAddr = Range("A1").Offset(1, 0).Address 
    Range("A1").Offset(1, 0).Select 
    ActiveCell.FormulaR1C1 = "=Sheet3!RC" 
    Range(MyCopyAddr).AutoFill Destination:=Range(MyCopyAddr + ":A69") 

    For Each ws In ActiveWorkbook.Sheets 
     If ws.Name <> "Sheet1" And ws.Name <> "Norms" And ws.Name <> "Sheet3" And ws.Name <> "Sheet4" Then 
      Range("B5").Select 
      Selection.End(xlToRight).Select 
      MyRefAddr = ActiveCell.Address 
      NoOfCols = Application.WorksheetFunction.Count(Range("B5:" + MyRefAddr)) 
      'STDEV 
      MyCopyAddr = Range(MyRefAddr).Offset(-1, 1).Address 
      Range(MyRefAddr).Offset(-1, 1).Select 
      ActiveCell.Value = "Sigma D" 
     End If 
    Next ws 
End Sub 

什麼是代碼中的錯誤。代碼將「規範」表並在那裏計算,儘管排除它停止循環在少數工作表上工作

+0

@simoco你能plzz解釋或者試試吧.. 我新來這個東西。 – user3564381

+0

有什麼解釋?我不明白你的問題 –

+0

我已經使用 如果ws.Name <>「Sheet1」和ws.Name <>「Norms」和ws.Name <>「Sheet3」和ws.Name <>「Sheet4」然後 以防止代碼去像「規範」提到的表。但 它沒有發生 – user3564381

回答

0

代碼在Norm表中計算,因爲它是活動工作表。循環中的所有範圍均指活動工作表。您應該避免使用選擇和激活:

How to avoid using Select in Excel VBA macros

編輯:提出了一些清理並刪除所有「選擇」和「激活」

Sub Macro5() 
    Dim ws As Worksheet 
    Dim NoOfCols As Integer 
    Dim workSheetNorm As Worksheet, cell As Range 

    Set workSheetNorm = Worksheets.Add(Before:=Worksheets(1)) 
    'workSheetNorm.Name = "Norms" 


    Set cell = workSheetNorm.Range("A1") 
    cell.Value = "SKU" 

    Set cell = cell.Offset(1, 0) 
    cell.FormulaR1C1 = "=Sheet3!RC" 
    cell.Offset(1, 0).AutoFill Destination:=workSheetNorm.Range(cell.Offset(1, 0), "A69") 

    For Each ws In ActiveWorkbook.Sheets 
     If ws.Name <> "Sheet1" And ws.Name <> "Norms" And ws.Name <> "Sheet3" And ws.Name <> "Sheet4" Then 
      Set cell = ws.Range("B5", ws.Cells(5, ws.Columns.Count).End(xlToLeft)) 

      NoOfCols = Application.WorksheetFunction.Count(cell) 
      'STDEV 
      cell.Offset(-1, 1).Value = "Sigma D" 
     End If 
    Next ws 
End Sub 
+0

我嘗試使用With命令 With Sheets(「Norms」)執行相同的操作。範圍(「A1」) ActiveCell.Value =「SKU」 MyCopyAddr = Range(「A1」)。Offset(1,0).Address 範圍(「A1」)。Offset(1,0).Select ActiveCell.FormulaR1C1 = 「=表Sheet 3 RC!」 範圍(MyCopyAddr).AutoFill目的地:=範圍(MyCopyAddr + 「:A69」) 完隨着 MSGBOX(ActiveSheet.Name) 對於每個WS在的ThisWorkbook。表 如果ws.Name <>「Sheet1」和ws.Name <>「Norms」和ws.Name <>「Sheet3」和ws.Name <>「Sheet4」Then 表(ActiveSheet.Name).Select MsgBox(ActiveShee t.Name) 無法解除 – user3564381

+0

你能幫我解決這個問題嗎? – user3564381

+0

我在回答中做了一個編輯 – Seb

0

你只需要引用所有範圍,以ws
這樣的事情?:

For Each ws In ActiveWorkbook.Sheets 
    If ws.Name <> "Sheet1" And ws.Name <> "Norms" And ws.Name <> "Sheet3" And ws.Name <> "Sheet4" Then 
     ws.Range("B5").Select 
     Selection.End(xlToRight).Select 
     MyRefAddr = ActiveCell.Address 
     NoOfCols = Application.WorksheetFunction.Count(ws.Range("B5:" + MyRefAddr)) 'STDEV 
     MyCopyAddr = ws.Range(MyRefAddr).Offset(-1, 1).Address 
     ws.Range(MyRefAddr).Offset(-1, 1).Select 
     ActiveCell.Value = "Sigma D" 
    End If 
Next ws