Sub SumCheck()
Dim MR As Long, MC As Long, MR1 As Long
With ActiveSheet
MR = .Range("a" & Rows.Count).End(xlUp).Row
MR1 = .Range("b" & Rows.Count).End(xlUp).Row
MC = .Cells(7, .Columns.Count).End(xlToLeft).Column
End With
ActiveSheet.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ActiveSheet.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
If ActiveSheet.Cells(MR + 4, 1) <> ActiveSheet.Cells(MR + 4, 2) Then
ActiveSheet.Range("xfd1") = "False"
Else: ActiveSheet.Range("xfd1") = "True"
End If
End Sub
Sub Sum_Check_All()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long, myrow As Long, myrow1 As Long
Dim mybook As Workbook, BaseWks As Workbook, Sh As Worksheet
Dim sourceRange As Range, destrange As Range, Rng As Range
Dim rnum As Long, CalcMode As Long
' Change this to the path\folder location of your files.
MyPath = Worksheets("sheet2").Range("A2").Value
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xlsx*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Add a new workbook with one sheet.
Set BaseWks = ThisWorkbook
' Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
For Each Sh In mybook.Worksheets
myrow1 = BaseWks.Sheets("Sum Err").Range("a" & Rows.Count).End(xlUp).Row
SumCheck
If Sh.Range("xfd1") = "False" Then
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 1) = mybook.Name
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 2) = Sh.Name
End If
Next
'mybook.Save
Application.DisplayAlerts = False
mybook.Close SaveChanges:=False
Application.DisplayAlerts = True
On Error GoTo 0
Next FNum
On Error Resume Next
BaseWks.Save
If Err.Number = 1004 Then
MsgBox "Please Save This New Workbook With New Name :-)", vbInformation
End If
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
MsgBox "Thanks for using :-)"
End Sub
Sub Demo()
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
Debug.Print Ws.Name
Next
End Sub
With ActiveSheet
MR = .Range("a" & Rows.Count).End(xlUp).Row
MR1 = .Range("b" & Rows.Count).End(xlUp).Row
MC = .Cells(7, .Columns.Count).End(xlToLeft).Column
End With
ActiveSheet.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ActiveSheet.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
If ActiveSheet.Cells(MR + 4, 1) <> ActiveSheet.Cells(MR + 4, 2) Then
ActiveSheet.Range("xfd1") = "False"
Else: ActiveSheet.Range("xfd1") = "True"
End If
Sub SumCheck()
Dim MR As Long, MC As Long, MR1 As Long
Dim ws As Worksheet
For Each ws In Worksheets
With ws
MR = .Range("a" & Rows.Count).End(xlUp).Row
MR1 = .Range("b" & Rows.Count).End(xlUp).Row
MC = .Cells(7, .Columns.Count).End(xlToLeft).Column
End With
ws.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ws.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
If ws.Cells(MR + 4, 1) <> ws.Cells(MR + 4, 2) Then
ws.Range("xfd1") = "False"
Else: ws.Range("xfd1") = "True"
End If
Next ws
End Sub
Sub Sum_Check_All()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long, myrow As Long, myrow1 As Long
Dim mybook As Workbook, BaseWks As Workbook, sh As Worksheet
Dim sourceRange As Range, destrange As Range, Rng As Range
Dim rnum As Long, CalcMode As Long
' Change this to the path\folder location of your files.
MyPath = Worksheets("sheet2").Range("A2").Value
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xlsx*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Add a new workbook with one sheet.
Set BaseWks = ThisWorkbook
' Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
For Each sh In mybook.Worksheets
myrow1 = BaseWks.Sheets("Sum Err").Range("a" & Rows.Count).End(xlUp).Row
SumCheck
If sh.Range("xfd1") = "False" Then
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 1) = mybook.Name
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 2) = sh.Name
End If
Next
'mybook.Save
Application.DisplayAlerts = False
mybook.Close SaveChanges:=False
Application.DisplayAlerts = True
On Error GoTo 0
Next FNum
On Error Resume Next
BaseWks.Save
If Err.Number = 1004 Then
MsgBox "Please Save This New Workbook With New Name :-)", vbInformation
End If
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
MsgBox "Thanks for using :-)"
End Sub
Sub SumCheck()
Dim MR As Long, MC As Long, MR1 As Long
Dim ws As Worksheet
For Each ws In Worksheets
If ws.UsedRange.Address = "$A$1" And Range("A1") = "" Then GoTo nextws
'If WorksheetFunction.CountA(Cells) = 0 Then GoTo nextws
With ws
MR = .Range("a" & Rows.Count).End(xlUp).Row
MR1 = .Range("b" & Rows.Count).End(xlUp).Row
MC = .Cells(7, .Columns.Count).End(xlToLeft).Column
End With
ws.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ws.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
If ws.Cells(MR + 4, 1) <> ws.Cells(MR + 4, 2) Then
ws.Range("xfd1") = "False"
Else: ws.Range("xfd1") = "True"
End If
nextws: Next
End Sub
Sub Sum_Check_All()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long, myrow As Long, myrow1 As Long
Dim mybook As Workbook, BaseWks As Workbook, sh As Worksheet
Dim sourceRange As Range, destrange As Range, Rng As Range
Dim rnum As Long, CalcMode As Long
' Change this to the path\folder location of your files.
'MyPath = Worksheets("sheet2").Range("A2").Value
Application.ScreenUpdating = False
'Put this macro in folder remove that folder all files
'myPath = Application.ThisWorkbook.Path & "\"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
MyPath = .SelectedItems(1) & "\"
End With
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xlsx*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Add a new workbook with one sheet.
Set BaseWks = ThisWorkbook
' Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
For Each sh In mybook.Worksheets
myrow1 = BaseWks.Sheets("Sum Err").Range("a" & Rows.Count).End(xlUp).Row
SumCheck
If sh.Range("xfd1") = "False" Then
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 1) = mybook.Name
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 2) = sh.Name
End If
Next
'mybook.Save
Application.DisplayAlerts = False
mybook.Close SaveChanges:=False
Application.DisplayAlerts = True
On Error GoTo 0
Next FNum
On Error Resume Next
BaseWks.Save
If Err.Number = 1004 Then
MsgBox "Please Save This New Workbook With New Name :-)", vbInformation
End If
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
MsgBox "Thanks for using :-)"
End Sub
ws.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ws.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
I know what u are telling me but i use ws.Select so each loop every worksheet is selectAs I yet wrote, result is wrong 'cause it seems you forgot in Sum calculation
from which workbook and which worksheet are the Cells properties !
So with WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC))
Cells alone just means cells from active worksheet !
Just check what returns .Address(External:=True) on this Range
to see if it's good or not …
If not, just add correct worksheet before Cells properties !
TBTO rule : respect Excel object model !
An easy model : Application / Workbook / Worksheet / Range or Cells …
but problem is 2 times loop each sheet
For Each ws In Worksheets
If ws.UsedRange.Address = "$A$1" And Range("A1") = "" Then GoTo nextws
'If WorksheetFunction.CountA(Cells) = 0 Then GoTo nextws
With ws
MR = .Range("a" & Rows.Count).End(xlUp).Row
MR1 = .Range("b" & Rows.Count).End(xlUp).Row
MC = .Cells(7, .Columns.Count).End(xlToLeft).Column
End With
ws.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ws.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
If ws.Cells(MR + 4, 1) <> ws.Cells(MR + 4, 2) Then
ws.Range("xfd1") = "False"
Else: ws.Range("xfd1") = "True"
End If
nextws: Next
For Each sh In mybook.Worksheets
myrow1 = BaseWks.Sheets("Sum Err").Range("a" & Rows.Count).End(xlUp).Row
SumCheck
If sh.Range("xfd1") = "False" Then
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 1) = mybook.Name
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 2) = sh.Name
End If
Next
Sub SumCheck()
Dim MR As Long, MC As Long, MR1 As Long
Dim ws As Worksheet
For Each ws In Worksheets
If ws.UsedRange.Address = "$A$1" And Range("A1") = "" Then GoTo nextws
'If WorksheetFunction.CountA(Cells) = 0 Then GoTo nextws
With ws
MR = .Range("a" & Rows.Count).End(xlUp).Row
MR1 = .Range("b" & Rows.Count).End(xlUp).Row
MC = .Cells(7, .Columns.Count).End(xlToLeft).Column
End With
ws.Cells(MR + 4, 1) = WorksheetFunction.Sum(Range(Cells(MR1, 2), Cells(MR1, MC)))
ws.Cells(MR + 4, 2) = WorksheetFunction.Sum(Range(Cells(8, 2), Cells(MR, MC)))
If ws.Cells(MR + 4, 1) <> ws.Cells(MR + 4, 2) Then
ws.Range("xfd1") = "False"
Else: ws.Range("xfd1") = "True"
End If
nextws: Next
End Sub
Sub Sum_Check_All()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long, myrow As Long, myrow1 As Long
Dim mybook As Workbook, BaseWks As Workbook, sh As Worksheet
Dim sourceRange As Range, destrange As Range, Rng As Range
Dim rnum As Long, CalcMode As Long
' Change this to the path\folder location of your files.
'MyPath = Worksheets("sheet2").Range("A2").Value
Application.ScreenUpdating = False
'Put this macro in folder remove that folder all files
'myPath = Application.ThisWorkbook.Path & "\"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
MyPath = .SelectedItems(1) & "\"
End With
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xlsx*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Add a new workbook with one sheet.
Set BaseWks = ThisWorkbook
' Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
For Each sh In mybook.Worksheets
myrow1 = BaseWks.Sheets("Sum Err").Range("a" & Rows.Count).End(xlUp).Row
SumCheck
If sh.Range("xfd1") = "False" Then
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 1) = mybook.Name
BaseWks.Sheets("Sum Err").Cells(myrow1 + 1, 2) = sh.Name
End If
Next
'mybook.Save
Application.DisplayAlerts = False
mybook.Close SaveChanges:=False
Application.DisplayAlerts = True
On Error GoTo 0
Next FNum
On Error Resume Next
BaseWks.Save
If Err.Number = 1004 Then
MsgBox "Please Save This New Workbook With New Name :-)", vbInformation
End If
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
MsgBox "Thanks for using :-)"
End Sub