2016-09-08 3 views
0

Ich arbeite mit mehreren Arbeitsblättern (dynamisch) und möchte einige einfache Berechnungen auf jedem Blatt (VBA Beginner) vornehmen. Ich kann das Makro auf jedem Blatt einzeln tun, aber die Schleife wird nicht funktionieren. Keine Fehlermeldung, sondern nur auf einem Blatt ausgeführt. Code ist unten. Jede Hilfe wird geschätzt.Schleife durch Arbeitsblätter in aktiver Arbeitsmappe funktioniert nicht

Sub Counts() 

Dim Last As Integer 
Dim Have As Long 
Dim Miss As Long 
Dim Total As Long 
Dim Value As Currency 
Dim Cost As Currency 
Dim TVal As Currency 
Dim ws As Worksheet 



For Each ws In ActiveWorkbook.Worksheets 

Last = Range("B1").CurrentRegion.Rows.Count 

Miss = Application.WorksheetFunction.CountBlank(Range("A2:A" & Last)) 
Total = Last - 1 
Have = Total - Miss 

Value = Application.WorksheetFunction.SumIf(Range("A2:A" & Last), "X",Range ("G2:G" & Last)) 
TVal = Application.WorksheetFunction.Sum(Range("G2:G" & Last)) 
Cost = TVal - Value 

Range("J2").Value = "Have" 
Range("J3").Value = "Missed" 
Range("J4").Value = "Total Cards" 
Range("J6").Value = "Value" 
Range("J7").Value = "Cost to Complete" 
Range("J8").Value = "Set Value" 

Range("k2").Value = Have 
Range("k3").Value = Miss 
Range("k4").Value = Total 
Range("k6").Value = Value 
Range("k7").Value = Cost 
Range("k8").Value = TVal 

Next ws 

End Sub 

Antwort

2

standardmäßig Range bezieht sich auf die aktive Blatt, und durch die Blätter Looping bedeutet nicht, jedes dieser Blätter wird automatisch aktiviert. Sie sollten immer versuchen, genau anzugeben, auf welches Blatt Sie sich beziehen. Der folgende Code verwendet ws.Range, wo immer Sie Range verwenden. (Und verwendet einen With ws Block, so dass ws.Range kann nur .Range shortcutted werden.)

Sub Counts() 

Dim Last As Integer 
Dim Have As Long 
Dim Miss As Long 
Dim Total As Long 
Dim Value As Currency 
Dim Cost As Currency 
Dim TVal As Currency 
Dim ws As Worksheet 



For Each ws In ActiveWorkbook.Worksheets 

    With ws 
     'I modified the next line to not use "CurrentRegion" as I wasn't 
     'sure whether "CurrentRegion" would be meaningful without first 
     'selecting a cell. 
     Last = .Range("B" & .Rows.Count).End(xlUp).Row 

     Miss = Application.WorksheetFunction.CountBlank(.Range("A2:A" & Last)) 
     Total = Last - 1 
     Have = Total - Miss 

     Value = Application.WorksheetFunction.SumIf(.Range("A2:A" & Last), "X",.Range("G2:G" & Last)) 
     TVal = Application.WorksheetFunction.Sum(.Range("G2:G" & Last)) 
     Cost = TVal - Value 

     .Range("J2").Value = "Have" 
     .Range("J3").Value = "Missed" 
     .Range("J4").Value = "Total Cards" 
     .Range("J6").Value = "Value" 
     .Range("J7").Value = "Cost to Complete" 
     .Range("J8").Value = "Set Value" 

     .Range("k2").Value = Have 
     .Range("k3").Value = Miss 
     .Range("k4").Value = Total 
     .Range("k6").Value = Value 
     .Range("k7").Value = Cost 
     .Range("k8").Value = TVal 

    End With  
Next ws 

End Sub 
Verwandte Themen