copy data contains minus values for the last column

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hello ,
I want creating report by copy data contains minus numbers for the last column , but the macro should deal with last column that add every time and deal with change data in first sheet .

before
Bridgestone Stock Sales report (2).xls
ABCDEFGHIJKL
1DECEMBERJANUARYFEBRUARY
2SizePatternOriginArrivedSalesStockArrivedSalesStockArrivedSalesStock
3BS 175/70R13B25A32INDO---------
4BS 175/70R13EP150INDO---------
5BS 175/70R13EP150THI34420324--324-57267
6BS 175/70R13EP150TR3-3-4-1---1
7BS 185/70R13EP150INDO3420144117--17
8BS 185/70R13EP150THI-8-8---8---8
9BS 175/65R14EP150THI---------
10BS 175/65R14EP150JAP---------
11BS 175/65R14B25A32INDO---------
12BS 175/65R14B25A32THI-------150-150
13BS 175/70R14EP150THI-20-20---20---20
14BS 175/70R14MY02THI---------
15BS 185/65R14TECTHI---413-9-6
16BS 185/65R14EP150INDO9-9-18-9---9
17BS 185/65R14EP150TR---123123-13110
18BS 185/65R14150EZINDO---------
19BS 185/70R14B25A32THI---------
20BS 195/70R14150EZTHI-------100-100
In & Out Balance
Cell Formulas
RangeFormula
F3:F20F3=D3-E3
L3:L20,I3:I20I3=F3+G3-H3




result should be as this
Bridgestone Stock Sales report (2).xls
ABCD
1
2SizePatternOriginStock
3BS 175/70R13EP150TR-1
4BS 185/70R13EP150THI-8
5BS 175/65R14B25A32THI-150
6BS 175/70R14EP150THI-20
7BS 185/65R14TECTHI-6
8BS 185/65R14EP150INDO-9
9BS 195/70R14150EZTHI-100
RESULT
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
One option. Try on a copy of your data.
VBA Code:
Option Explicit
Sub abdoM()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("In & Out Balance")
    Set ws2 = Worksheets("RESULT")
    Dim LRow As Long, LCol As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    
    'Clear RESULT sheet first
    If ws2.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row > 2 Then
        ws2.Range(ws2.Cells(3, 1), ws2.Cells(ws2.Cells.Find("*", , xlFormulas, , 1, 2).Row, 4)).ClearContents
    End If
    
    Dim rng As Range, r As Range
    Set rng = ws1.Range(ws1.Cells(2, 1), ws1.Cells(LRow, LCol))
    With rng
        .AutoFilter LCol, "<0"
        If ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row > 2 Then
            Set rng = .Offset(1).Resize(.Rows.Count - 1)
            With rng
                Set r = Union(.Columns("A:C"), .Columns(LCol))
                r.Copy
                ws2.Range("A3").PasteSpecial xlPasteValues
                Application.CutCopyMode = False
            End With
        End If
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks
what does mean this error?
"object variable or with block variable not set " in this line
VBA Code:
 If ws2.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row > 2 Then
 
Upvote 0
What does your RESULT sheet look like before you run the code? Do you have headers in row 2 as in your original post?
 
Upvote 0
good catching !
there is no any header , I expect the macro does that without writing manually , is it possible?
 
Upvote 0
Yes, try this one instead:
VBA Code:
Option Explicit
Sub abdoM_V2()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("In & Out Balance")
    Set ws2 = Worksheets("RESULT")
    Dim LRow As Long, LCol As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    
    'Clear RESULT sheet first
    ws2.Cells.ClearContents
    ws2.Range("A2").Resize(, 4).Value = Array("Size", "Pattern", "Origin", "Stock")
    
    Dim rng As Range, r As Range
    Set rng = ws1.Range(ws1.Cells(2, 1), ws1.Cells(LRow, LCol))
    With rng
        .AutoFilter LCol, "<0"
        If ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row > 2 Then
            Set rng = .Offset(1).Resize(.Rows.Count - 1)
            With rng
                Set r = Union(.Columns("A:C"), .Columns(LCol))
                r.Copy
                ws2.Range("A3").PasteSpecial xlPasteValues
                Application.CutCopyMode = False
            End With
        End If
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try again
VBA Code:
Option Explicit
Sub abdoM_V3()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("In & Out Balance")
    Set ws2 = Worksheets("RESULT")
    Dim LRow As Long, LCol As Long
    LRow = ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    
    'Clear RESULT sheet first
    ws2.Cells.ClearContents
    ws2.Range("A2").Resize(, 4).Value = Array("Size", "Pattern", "Origin", "Stock")
    
    Dim rng As Range, r As Range
    Set rng = ws1.Range(ws1.Cells(2, 1), ws1.Cells(LRow, LCol))
    With rng
        .AutoFilter LCol, "<0"
        If ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row > 2 Then
            Set rng = .Offset(1).Resize(.Rows.Count - 1)
            With rng
                Set r = Union(.Columns("A:C"), .Columns(LCol))
                r.Copy
                With ws2.Range("A3")
                    .PasteSpecial xlPasteValues
                    .PasteSpecial xlPasteFormats
                End With
                Application.CutCopyMode = False
            End With
        End If
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
the same thing as in post#6
No it isn't, these lines have been added:

VBA Code:
With ws2.Range("A3")
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
End With

Did you run it again?
What format did it not copy?
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,610
Members
449,174
Latest member
ExcelfromGermany

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top