VBA ; Clear Content From C2 to E8 and Move up

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all..
this code not fully working, how t o fix it to delete content with data up if there is blank row (above), and in col "name" automatic delete if data move up
this code:
VBA Code:
Sub clearcontent()
    Dim lngLastRow As Long
    Dim wsSrc As Worksheet
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With
 
    Set wsSrc = ActiveSheet
    lngLastRow = wsSrc.Range("C:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
    Dim RowsToDelete As Range
    ' Only delete the table area (If you know you have nothing to the right you could dispense
    ' with the intersect and just delete the entirerow)
    Set RowsToDelete = Intersect(wsSrc.Range("C2:E" & lngLastRow), _
                        wsSrc.Range("C2:A" & lngLastRow).SpecialCells(xlCellTypeBlanks).EntireRow)
    RowsToDelete.Delete Shift:=xlUp
 
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .CalculateFull
    End With
End Sub
this my table (before run macro):
Book1
ABCDEFG
1RefREF VerifnameNOMOR REGISTER LELANGREGISTER LELANGTahun LelangBulan Lelang
21AJohn1ok20207
32AJohn34good20207
43AJohn20207
54A1Johnexcelent20207
65A2John20207
76A3John5poor20205
87A4John4bad20205
Sheet1

expected result (after run macro)
Book1
ABCDEFG
11RefREF VerifnameNOMOR REGISTER LELANGREGISTER LELANGTahun LelangBulan Lelang
121AJohn1ok20207
132AJohn34good20207
143AJohnexcelent20207
154A1John5poor20207
165A2John4bad20207
176A320205
187A420205
Sheet1

thank in advance
susanto
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I can't get the use of Intersect to work. Maybe it evaluated cell by cell. My method would be this
VBA Code:
Sub clearcontent()
    Dim lngLastRow As Long, n As Long
    Dim wsSrc As Worksheet
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With
 
    Set wsSrc = ActiveSheet
    lngLastRow = wsSrc.Range("C:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
    Dim RowsToDelete As Range
    For n = 2 To lngLastRow
        If IsEmpty(wsSrc.Range("D" & n)) And IsEmpty(Range("E" & n)) Then
            If RowsToDelete Is Nothing Then
                Set RowsToDelete = wsSrc.Range("C" & n, "E" & n)
            Else
                Set RowsToDelete = Application.Union(RowsToDelete, wsSrc.Range("C" & n, "E" & n))
            End If
        End If
    Next
    RowsToDelete.Delete xlUp

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .CalculateFull
    End With
End Sub
 
Upvote 0
Solution
hi guys...i found new problem, actually my range in C2 to BU7130 to more fast running
i have modified that code with suitable change range
range c ---change C2
range e--- change BU7130

but the result is wrong show 400...
have you idea?
 
Upvote 0
Can you post your whole new code (with modification)?
 
Upvote 0
hi Zot...
like this
VBA Code:
Sub clearcontent()
    Dim lngLastRow As Long, n As Long
    Dim wsSrc As Worksheet
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With
 
    Set wsSrc = ActiveSheet
    lngLastRow = wsSrc.Range("C:BU7130").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
    Dim RowsToDelete As Range
    For n = 2 To lngLastRow
        If IsEmpty(wsSrc.Range("D" & n)) And IsEmpty(Range("BU7130" & n)) Then
            If RowsToDelete Is Nothing Then
                Set RowsToDelete = wsSrc.Range("C" & n, "BU7130" & n)
            Else
                Set RowsToDelete = Application.Union(RowsToDelete, wsSrc.Range("C" & n, "BU7130" & n))
            End If
        End If
    Next
    RowsToDelete.Delete xlUp

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .CalculateFull
    End With
End Sub

the core, for more faster clearing content ,i want to limited range only from cell C2 till BU7130
 
Upvote 0
In the "BU7130" & n, the n represent the row number increment by For loop. So, you should put BU only like this "BU" & n.

In my original code
If IsEmpty(wsSrc.Range("D" & n)) And IsEmpty(Range("E" & n)) Then
I was checking if range D and range E is empty, the condition to meet before registering range C to E to be deleted at the end of the loop with this line
wsSrc.Range("C" & n, "E" & n)

In your code
If IsEmpty(wsSrc.Range("D" & n)) And IsEmpty(Range("BU7130" & n)) Then
it means the condition to meet is that Range Dx and Range BUx (x is row number) to be empty. I believe that is not what you wanted.

Then you mark range from C to BU to be deleted
wsSrc.Range("C" & n, "BU7130" & n)

What is the condition to meet actually?
 
Upvote 0
actually, my range from C2 to BU7130, my problem when i use range wsSrc.Range("C" & n, "BU" & n), i feel very slow running
 
Upvote 0
actually, my range from C2 to BU7130, my problem when i use range wsSrc.Range("C" & n, "BU" & n), i feel very slow running
The condition is still D and E empty? Your code is not like that.

It is a big row but the code is not deleting row as it progress but delete all at once at the end of the loop. This is as fast as it can go.
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
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