وعليكم السلام
جرب الكود بهذا الشكل عله يفي بالغرض
CODE
Option Explicit
Sub Test()
Dim ws As Worksheet
Dim wsSales As Worksheet
Dim wsCash As Worksheet
Dim wsTemp As Worksheet
Dim x As Variant
Dim str As String
Dim m As Long
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Worksheets("ارصدة اول المدة")
Set wsSales = ThisWorkbook.Worksheets("مبيعات")
Set wsCash = ThisWorkbook.Worksheets("نقدي")
Set wsTemp = ThisWorkbook.Worksheets("temp")
str = ws.Range("I2").Value
x = Application.Match(str, ws.Columns(1), 0)
If Not IsError(x) Then
If ws.Cells(x, 3).Value <> 0 Then
MsgBox "Balance Does Not Equal Zero", 64: Exit Sub
Else
m = wsTemp.Cells(Rows.Count, 2).End(xlUp).Row + 1
With wsSales.Range("B3", wsSales.Range("B" & Rows.Count).End(xlUp))
.AutoFilter 1, str
If WorksheetFunction.CountA(wsSales.Columns(2).SpecialCells(xlCellTypeVisible)) = 1 Then MsgBox "No Sales": .AutoFilter: GoTo NX1
.Offset(1).EntireRow.Copy wsTemp.Range("A" & m)
.Offset(1).EntireRow.Delete
.AutoFilter
End With
m = wsTemp.Cells(Rows.Count, 2).End(xlUp).Row + 1
With wsTemp.Range("A" & m & ":G" & m)
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -1003520
.TintAndShade = 0
.Weight = xlThick
End With
End With
NX1:
m = wsTemp.Cells(Rows.Count, 2).End(xlUp).Row + 1
With wsCash.Range("B3", wsCash.Range("B" & Rows.Count).End(xlUp))
.AutoFilter 1, str
If WorksheetFunction.CountA(wsCash.Columns(2).SpecialCells(xlCellTypeVisible)) = 1 Then MsgBox "No Cash": .AutoFilter: GoTo NX2
.Offset(1).EntireRow.Copy wsTemp.Range("A" & m)
.Offset(1).EntireRow.Delete
.AutoFilter
End With
m = wsTemp.Cells(Rows.Count, 2).End(xlUp).Row + 1
With wsTemp.Range("A" & m & ":G" & m)
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -1003520
.TintAndShade = 0
.Weight = xlThick
End With
End With
End If
NX2:
End If
Application.ScreenUpdating = True
End Sub