Clear data from specific sheets and move data from the last sheet to the first sheet

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want clearing data for specific sheets , not all of the sheets in workbook because of I have others sheets shouldn't clear data .
so what I want clearing data for theses sheets and before clear data for summary sheet should move data except columns F,G,H,I to ATS sheet .

Clear sheetss.xlsm
ABCDEFG
1ITEMIDBRANDBTMBTSBUYINGSELLING
21CCR1-CMB1C-BM-100BBS-001BTR1-001233.00100.00
32CCR1-CMB2C-BM-101BBS-002BTR1-002123.00
43CCR1-CMB3C-BM-102BBS-003BTR1-003124.00
54CCR1-CMB4C-BM-103BBS-004BTR1-004123.002.00
65CCR1-CMB5C-BM-104BBS-005BTR1-00544.003.00
76CCR1-CMB6C-BM-105BBS-006BTR1-00655.004.00
87CCR1-CMB7C-BM-106BBS-007BTR1-00766.00
98CCR1-CMB8C-BM-107BBS-008BTR1-00877.00700.00
109CCR1-CMB9C-BM-108BBS-009BTR1-00988.0012.00
ATS


Clear sheetss.xlsm
FGHIJKLM
1ITEMIDBRANDBTMBTSBUYINGRETURNSNET
21CCR1-CMB4C-BM-103BBS-004BTR1-004233.00100.00133.00
32CCR1-CMB5C-BM-104BBS-005BTR1-005123.00123.00
43CCR1-CMB6C-BM-105BBS-006BTR1-006124.00124.00
54CCR1-CMB7C-BM-106BBS-007BTR1-007123.002.00121.00
65CCR1-CMB8C-BM-107BBS-008BTR1-00844.003.0041.00
76CCR1-CMB9C-BM-108BBS-009BTR1-00955.004.0051.00
87CCR1-CMB10C-BM-109BBS-010BTR1-01066.0066.00
98CCR1-CMB11C-BM-110BBS-011BTR1-01177.0077.00
109CCR1-CMB12C-BM-111BBS-012BTR1-01288.0012.0076.00
ASS


Clear sheetss.xlsm
EFGHIJ
1ITEMIDBRANDBTMBTSBUYING
21CCR1-CMB41C-BM-112BBS-013BTR1-013200.00
32CCR1-CMB42C-BM-113BBS-014BTR1-014120.00
43CCR1-CMB43C-BM-114BBS-015BTR1-015100.00
54CCR1-CMB44C-BM-115BBS-016BTR1-016110.00
65CCR1-CMB45C-BM-116BBS-017BTR1-0173.00
76CCR1-CMB9C-BM-108BBS-009BTR1-0094.00
87CCR1-CMB10C-BM-109BBS-010BTR1-01011.00
98CCR1-CMB11C-BM-110BBS-011BTR1-01111.00
109CCR1-CMB12C-BM-111BBS-012BTR1-01212.00
ATR


Clear sheetss.xlsm
BCDEFG
1ITEMIDBRANDBTMBTSSELLING
21CCR1-CMB41C-BM-100BBS-013BTR1-013200.00
32CCR1-CMB42C-BM-101BBS-014BTR1-014120.00
43CCR1-CMB43C-BM-102BBS-015BTR1-015100.00
54CCR1-CMB44C-BM-103BBS-016BTR1-016110.00
65CCR1-CMB45C-BM-104BBS-017BTR1-0173.00
BTS



Clear sheetss.xlsm
ABCDEFGHIJ
1ITEMIDBRANDBTMBTSBUYINGSELLINGRSRBNET
21CCR1-CMB1C-BM-100BBS-001BTR1-001233.00100.0012.00145.00
32CCR1-CMB2C-BM-101BBS-002BTR1-002123.0011.0011.00123.00
43CCR1-CMB3C-BM-102BBS-003BTR1-003124.0011.0011.0011.00113.00
54CCR1-CMB4C-BM-103BBS-004BTR1-004123.002.0011.00110.00
65CCR1-CMB5C-BM-104BBS-005BTR1-00544.003.0041.00
76CCR1-CMB6C-BM-105BBS-006BTR1-00655.004.0051.00
87CCR1-CMB7C-BM-106BBS-007BTR1-00766.001.0067.00
98CCR1-CMB8C-BM-107BBS-008BTR1-00877.0012.002.0067.00
109CCR1-CMB9C-BM-108BBS-009BTR1-00988.0012.0076.00
SUMMARY



result

Clear sheetss.xlsm
ABCDEFG
1ITEMIDBRANDBTMBTSBUYINGSELLING
21CCR1-CMB1C-BM-100BBS-001BTR1-001145.00
32CCR1-CMB2C-BM-101BBS-002BTR1-002123.00
43CCR1-CMB3C-BM-102BBS-003BTR1-003113.00
54CCR1-CMB4C-BM-103BBS-004BTR1-004110.00
65CCR1-CMB5C-BM-104BBS-005BTR1-00541.00
76CCR1-CMB6C-BM-105BBS-006BTR1-00651.00
87CCR1-CMB7C-BM-106BBS-007BTR1-00767.00
98CCR1-CMB8C-BM-107BBS-008BTR1-00867.00
109CCR1-CMB9C-BM-108BBS-009BTR1-00976.00
ATS


Clear sheetss.xlsm
FGHIJKLM
1ITEMIDBRANDBTMBTSBUYINGRETURNSNET
2
3
4
5
6
7
8
9
10
ASS



Clear sheetss.xlsm
EFGHIJ
1ITEMIDBRANDBTMBTSBUYING
2
3
4
5
6
7
8
9
10
ATR



Clear sheetss.xlsm
BCDEFG
1ITEMIDBRANDBTMBTSSELLING
2
3
4
5
6
BTS


Clear sheetss.xlsm
ABCDEFGHIJ
1ITEMIDBRANDBTMBTSBUYINGSELLINGRSRBNET
2
3
4
5
6
7
8
9
10
SUMMARY


thanks
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi MKLAQ,

maybe to start with

VBA Code:
Public Sub MrE_1230300_1703110()
' https://www.mrexcel.com/board/threads/clear-data-from-specific-sheets-and-move-data-from-the-last-sheet-to-the-first-sheet.1230300/
Dim strSheetsClear As String
Dim lngCounter As Long
Dim varSplit As Variant
Dim varArr As Variant

strSheetsClear = "***,ATR,BTS,SUMMARY"

With Sheets("Summary").UsedRange
  varArr = .Offset(1).Resize(.Rows.Count - 1)
End With

For lngCounter = 1 To 5
  Sheets("ATS").Cells(2, lngCounter).Resize(UBound(varArr, 1), 1).Value = Application.Index(varArr, 0, lngCounter)
Next lngCounter
With Sheets("ATS").Range("F2").Resize(UBound(varArr, 1), 1)
  .Value = Application.Index(varArr, 0, 10)
  .Offset(, 1).Value = ""
End With

varSplit = Split(strSheetsClear, ",")
For lngCounter = LBound(varSplit) To UBound(varSplit)
  With Sheets(varSplit(lngCounter)).UsedRange
    .Offset(1).Resize(.Rows.Count - 1).ClearContents
  End With
Next lngCounter
End Sub

Ciao,
Holger
 
Upvote 0
thanks
gives error subscript out of range in this line
VBA Code:
  With Sheets(varSplit(lngCounter)).UsedRange
 
Upvote 0
Hi MKLAQ,

codeline

VBA Code:
strSheetsClear = "***,ATR,BTS,SUMMARY"

should be

VBA Code:
strSheetsClear = "A??,ATR,BTS,SUMMARY"

where ? must be replaced by S. It seems that the Forum Software has these three letters on the Blocklist as the content will only be changed on replying.

Holger
 
Upvote 0
It seems that the Forum Software has these three letters on the Blocklist as the content will only be changed on replying.
I was asking about stars . so should be ATS . even that still the error shows .
 
Upvote 0
Hi MKLAQ,

ATS was never part of the array as you wanted a "special" treatment to that sheet which would omitt the sheet from being cleared, I was talking about A S S which was the second sheet in the list you featured in the opening post.

Holger
 
Upvote 0
Perfect !
just I'm afraid after run the code and clearing data I press the macro again ,then will clear data for first sheet and losing data .
is there any way to keep data in first sheet after clear data for others sheets even if I run macro many time?
 
Upvote 0
Hi MKLAQ,

you could place a boolean value in a cell on sheet ATS outside the range on question for work which holds either True (only then the macro will run) or False (skip it) - I used a Data Validation list in that cell to only allow these values in my workbook:

VBA Code:
Public Sub MrE_1230300_1703110()
' https://www.mrexcel.com/board/threads/clear-data-from-specific-sheets-and-move-data-from-the-last-sheet-to-the-first-sheet.1230300/
Dim strSheetsClear As String
Dim lngCounter As Long
Dim varSplit As Variant
Dim varArr As Variant

'checking the value if macro should be run
If UCase(Sheets("ATS").Range("N1").Value) = "TRUE" Then
  
'  strSheetsClear = "***,ATR,BTS,SUMMARY"
  strSheetsClear = "A??,ATR,BTS,SUMMARY"
  
  With Sheets("Summary").UsedRange
    varArr = .Offset(1).Resize(.Rows.Count - 1)
  End With
  
  For lngCounter = 1 To 5
    Sheets("ATS").Cells(2, lngCounter).Resize(UBound(varArr, 1), 1).Value = Application.Index(varArr, 0, lngCounter)
  Next lngCounter
  With Sheets("ATS").Range("F2").Resize(UBound(varArr, 1), 1)
    .Value = Application.Index(varArr, 0, 10)
    .Offset(, 1).Value = ""
  End With
  
  varSplit = Split(strSheetsClear, ",")
  For lngCounter = LBound(varSplit) To UBound(varSplit)
    With Sheets(varSplit(lngCounter)).UsedRange
      .Offset(1).Resize(.Rows.Count - 1).ClearContents
    End With
  Next lngCounter
  'set value to skip macro if value isn*t changed manually
  Sheets("ATS").Range("N1").Value = "False"
End If
End Sub

Ciao,
Holger
 
Upvote 1
Solution
Hi MKLAQ,

what is the content of N1 on sheet "ATS"? If it is not true (written in either way) the code will do nothing.

Holger
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

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