clear contents cells for specific sheets except last sheet when save new file

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
Hi
I want to export file as xlsm with keep the same sheets names and formatting , but should replace data form summary sheet with stock sheet and clear sheets (sales,pur,RETURNS,SUMMARY) with keep the same formatting and when save the file should be file name STOCK-& current year . and should replace data every time run the macro based on current year.

original data

INVEN with single search v0 c.xlsm
ABCDEF
1itemCODEBRANDTYPEMANUFACTUREQTY
21AA-110W40 208LQ8EU2222
32AA-215W40 208LCASSU400
43AA-35W30 208LQ8EU800
54AA-45W30 12x1LQ8EU600
65AA-510W40 208LENIIT300
76AA-65W30 4x4LQ8EU200
87AA-710W40 12x1LQ8EU120
98AA-815W40 12x1LCASSU450
109AA-910W40 12x1LENIIT890
1110AA-1010W40 4x4LQ8EU345
1211AA-1110W40 4x4LCASSU78
1312AA-1210W40 4x4LENIIT123
1413AA-135W40 4x4LQ8EU456
1514AA-145W40 4x4LCASSU678
1615AA-155W40 4x4LENIIT1234
1716AA-1620W50 4x4LQ8EU456
STOCK


INVEN with single search v0 c.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTURESALES
201/01/2021AA-110W40 208LQ8EU100
302/01/2021AA-215W40 208LCASSU50
403/01/2021AA-35W30 208LQ8EU280
504/01/2021AA-45W30 12x1LQ8EU300
605/01/2021AA-510W40 208LENIIT80
706/01/2021AA-65W30 4x4LQ8EU20
807/01/2021AA-710W40 12x1LQ8EU20
908/01/2021AA-815W40 12x1LCASSU20
1009/01/2021AA-910W40 12x1LENIIT876
1110/01/2021AA-1010W40 4x4LQ8EU345
1211/01/2021AA-1110W40 4x4LCASSU123
1312/01/2021AA-1210W40 4x4LENIIT78
1413/01/2021AA-135W40 4x4LQ8EU300
1514/01/2021AA-145W40 4x4LCASSU34
1615/01/2021AA-155W40 4x4LENIIT23
1716/01/2021AA-1620W50 4x4LQ8EU56
1817/01/2021AA-110W40 208LQ8EU100
sales


INVEN with single search v0 c.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTUREPURCHASE
204/02/2021AA-110W40 208LQ8EU55
305/02/2021AA-215W40 208LCASSU20
406/02/2021AA-35W30 208LQ8EU10
507/02/2021AA-45W30 12x1LQ8EU10
608/02/2021AA-510W40 208LENIIT3
709/02/2021AA-65W30 4x4LQ8EU4
810/02/2021AA-710W40 12x1LQ8EU45
911/02/2021AA-815W40 12x1LCASSU8
1012/02/2021AA-910W40 12x1LENIIT1
1113/02/2021AA-1010W40 4x4LQ8EU100
1214/02/2021AA-1110W40 4x4LCASSU20
1315/02/2021AA-1210W40 4x4LENIIT100
1416/02/2021AA-135W40 4x4LQ8EU44
1517/02/2021AA-145W40 4x4LCASSU20
1618/02/2021AA-155W40 4x4LENIIT50
1719/02/2021AA-1620W50 4x4LQ8EU12
1820/02/2021AA-1720W50 4x4LCASSU9
1921/02/2021AA-1820W50 4x4LENIIT4
2022/02/2021AA-110W40 208LQ8EU55
pur



INVEN with single search v0 c.xlsm
ABCDEF
1itemCODEBRANDTYPEMANUFACTUREreturns
205/04/2021AA-910W40 12x1LENIIT20
306/04/2021AA-1010W40 4x4LQ8EU30
407/04/2021AA-1110W40 4x4LCASSU40
508/04/2021AA-45W30 12x1LQ8EU10
609/04/2021AA-45W30 12x1LQ8EU11
RETURNS


INVEN with single search v0 c.xlsm
ABCDEFGHIJ
1itemCODEBRANDTYPEMANUFACTURESTOCKSALESPURRETURNSBALANCE
21AA-110W40 208LQ8EU22222001102132
32AA-215W40 208LCASSU4005020370
43AA-35W30 208LQ8EU80028010530
54AA-45W30 12x1LQ8EU6003001021331
65AA-510W40 208LENIIT300803223
76AA-65W30 4x4LQ8EU200204184
87AA-710W40 12x1LQ8EU1202045145
98AA-815W40 12x1LCASSU450208438
109AA-910W40 12x1LENIIT89087612035
1110AA-1010W40 4x4LQ8EU34534510030130
1211AA-1110W40 4x4LCASSU78123204015
1312AA-1210W40 4x4LENIIT12378100145
1413AA-135W40 4x4LQ8EU45630044200
1514AA-145W40 4x4LCASSU6783420664
1615AA-155W40 4x4LENIIT123423501261
1716AA-1620W50 4x4LQ8EU4565612412
1817AA-1720W50 4x4LCASSU99
1918AA-1820W50 4x4LENIIT44
SUMMARY



result should save file as xlsm
STOCK-2023.xlsm
ABCDEFGHIJ
1itemCODEBRANDTYPEMANUFACTURESTOCKSALESPURRETURNSBALANCE
21AA-110W40 208LQ8EU22222001102132
32AA-215W40 208LCASSU4005020370
43AA-35W30 208LQ8EU80028010530
54AA-45W30 12x1LQ8EU6003001021331
65AA-510W40 208LENIIT300803223
76AA-65W30 4x4LQ8EU200204184
87AA-710W40 12x1LQ8EU1202045145
98AA-815W40 12x1LCASSU450208438
109AA-910W40 12x1LENIIT89087612035
1110AA-1010W40 4x4LQ8EU34534510030130
1211AA-1110W40 4x4LCASSU78123204015
1312AA-1210W40 4x4LENIIT12378100145
1413AA-135W40 4x4LQ8EU45630044200
1514AA-145W40 4x4LCASSU6783420664
1615AA-155W40 4x4LENIIT123423501261
1716AA-1620W50 4x4LQ8EU4565612412
1817AA-1720W50 4x4LCASSU99
1918AA-1820W50 4x4LENIIT44
STOCK


STOCK-2023.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTURESALES
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sales


STOCK-2023.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTUREPURCHASE
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pur


STOCK-2023.xlsm
ABCDEF
1itemCODEBRANDTYPEMANUFACTUREreturns
2
3
4
5
6
RETURNS


STOCK-2023.xlsm
ABCDEFGHIJ
1itemCODEBRANDTYPEMANUFACTURESTOCKSALESPURRETURNSBALANCE
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SUMMARY

I hope this make sense
thanks
 
Last edited:
I believe that I have code for you that does what you asked for.

Note that I used something called "code names" for the worksheets so that if someone changes the name for a worksheet in the worksheet's tab the code will still work.

A decent characterization of code names for worksheets is HERE.

The workbook is HERE.

Here is the code

VBA Code:
Sub UpdateStock()

    Dim rRangeToClear As Range
    
    Dim iDataColumns As Long
    
    Dim iDataRows As Long
    
    [Stock].Range("A1").CurrentRegion.Clear '<=uses code name for Stock worksheet.
    
    With [Sales] '<= code name for Sales worksheet
        
'       Count of rows to clear.
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1

'       Count of columns to clear.
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
        
'       Range whose data is cleared. Does not include headers.
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        With rRangeToClear
            .Interior.Color = xlNone  'clear interior colors
            .Value = "" 'clear the data colors
        End With
    
    End With

    With [Pur] '<= code name for Pur worksheet
        
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1
        
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
        
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        With rRangeToClear
            .Interior.Color = xlNone
            .Value = ""
        End With
    
    End With

    With [Returns] '<= uses code name for Returns worksheet
        
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1
        
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
        
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        With rRangeToClear
            .Interior.Color = xlNone
            .Value = ""
        End With
    
    End With

    With [Summary] '<= uses code name for Summary worksheet
            
'       Copy Summary worksheet contents to Stock worksheet.
'       Includes headers.
        .Range("A1").CurrentRegion.Copy [Stock].Range("A1")
        
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1
        
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
            
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        rRangeToClear.Value = ""
        
    End With

End Sub
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
first thanks for the code & file
second I'm so confused when I put your code in my real file it gives error object required in this line
VBA Code:
[Stock].Range("A1").CurrentRegion.Clear '<=uses code name for Stock worksheet.
despite of I made sure from sheets names :confused:
third as I said
I want to export file as xlsm with keep the same sheets names and formatting
should keep original data for source workbook and add new file when clear data with rename the file as I said
when save the file should be file name STOCK-& current year
 
Upvote 0
Please see the link about worksheet code names that I provided. I use "code names" not "sheet/tab names". That is the issue. With code names if someone changes the sheet/tab names the code still works.
 
Upvote 0
this is what I have in my real file
1.PNG
 
Upvote 0
Sorry, my misunderstanding. So code should save the existing workbook as-is and only create and save another workbook with the new data in it and the other sheets cleared?

Because I cannot test your workbook I am not sure how to address the issue with my code. Can you take out sensitive data and provide a link to the workbook? I'll try to fix the problem.
 
Upvote 0
fantastic !
can you modify a little things in stock sheet after copy data from SUMMARY sheet, please?
I want deleting columns STOCK,SALES,PUR,RETURNS , also change header BALANCE to QTY
thanks for your assistance .
 
Upvote 0
This code does what you want. Note use of absolute cell/range references that will have to be changed if the structure of the worksheet changes.

VBA Code:
Option Explicit

' ----------------------------------------------------------------
' Procedure Name: UpdateStock
' Purpose: Udate workbook then saveas, file name = Stock-Year
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jim
' Date: 4/20/2023
' ----------------------------------------------------------------

Sub UpdateStock()

    Dim rRangeToClear As Range
    
    Dim iDataColumns As Long
    
    Dim iDataRows As Long
    
    Dim sPathToFile As String
    
    Dim sFileToSaveName As String
    
    Dim sMsg As String
    
    Dim vAns As Variant
    
    Application.ScreenUpdating = False
    
    sPathToFile = ThisWorkbook.Path & "\"
    
    sFileToSaveName = "Stock-" & Year(Now) & ".xlsm"
    
'   If the file already exists tell user and ask whether
'   to overwrite the existing file with the new one.
    If Dir(sPathToFile & sFileToSaveName) <> "" _
     Then
        sMsg = "The file named " & sFileToSaveName & " already" _
        & Chr(10) _
        & "exists in " & sPathToFile & "." & Chr(10) & "Replace?"
        
        vAns = MsgBox(sMsg, vbYesNo, "Saving the file.")
        
        If vAns = vbNo Then Exit Sub
        
'       Delete the file before saving it (again)
        Kill (sPathToFile & sFileToSaveName)
        
    End If
    
    [Stock].Range("A1").CurrentRegion.Clear '<=uses code name for Stock worksheet.
    
    With [sales] '<= code name for Sales worksheet
        
'       Count of rows to clear.
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1

'       Count of columns to clear.
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
        
'       Range whose data is cleared. Does not include headers.
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        With rRangeToClear
            .Interior.Color = xlNone  'clear interior colors
            .Value = "" 'clear the data colors
        End With
    
    End With

    With [pur] '<= code name for Pur worksheet
        
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1
        
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
        
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        With rRangeToClear
            .Interior.Color = xlNone
            .Value = ""
        End With
    
    End With

    With [RETURNS] '<= uses code name for Returns worksheet
        
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1
        
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
        
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        With rRangeToClear
            .Interior.Color = xlNone
            .Value = ""
        End With
    
    End With

    With [SUMMARY] '<= uses code name for Summary worksheet
    
        .Activate
            
'       Copy Summary worksheet contents to Stock worksheet.
'       Includes headers.
        .Range("A1").CurrentRegion.Copy [Stock].Range("A1")
        
        .Range("A1").Select
        
        iDataRows = .Range("A1").Offset(100000).End(xlUp).Row - 1
        
        iDataColumns = .Range("A1").Offset(0, 1000).End(xlToLeft).Column
            
        Set rRangeToClear = .Range("A1").Offset(1).Resize(iDataRows, iDataColumns)
        
        rRangeToClear.Value = ""
        
    End With
        
    With [Stock]
    
        .Activate
        .Columns("F:I").Delete Shift:=xlToLeft  '<= may need change if worksheet structure changes.
        .Range("F1").Value = "QTY"              '<= may need change if worksheet structure changes.
        .Range("A1").Activate                   '<= may need change if worksheet structure changes.
    
    End With
    
    ThisWorkbook.SaveAs sPathToFile & sFileToSaveName

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

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