Cleaning non continuous range

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hi experts,

Before adding the data into col. K, I have to clean the previous records from the col. K. The issue is that is not a continuos range becasue when I am adding the values I filter based on other col. So anyone has a sugestions how can I clear the data from column K considering the empty cells?

'declare object variable to hold reference to cells to clear
Dim PORange As Range
Dim i As Long
Dim LastRow As Long

'Selecting the correct sheet
Worksheets("MRPReqLive").Activate

'identify cells to clear
Set PORange = ThisWorkbook.Worksheets("MRPReqLive").Range("K2", Range("K1").End(xlDown))

LastRow = ThisWorkbook.Worksheets("MRPReqLive").Cells(Rows.Count, "K").End(xlDown).Row

Application.ScreenUpdating = False

'Inputbox to insert the date factor
DaysToMRPDate = TextBox1.Value
If DaysToMRPDate = "" Then
Exit Sub
End If

'Selecting the correct sheet
Worksheets("MRPReqLive").Activate

'Deleting previous values from AddDayFactor
For i = 1 To LastRow Step -1
If Cell.Value <> " " Then
Cell.Clear
End If
Next i


'Filter all the Purchase Order <> 0
ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).AutoFilter Field:=5, Criteria1:="<>0", _
Operator:=xlAnd

'Copy the value from the user form into the AddDayFactor
Range("K2", Range("K1").End(xlDown)).Value = TextBox1.Value

'Remove filter
ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).AutoFilter Field:=5

'Refreshing worksheet
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False

Worksheets("MRPReqPivot").Activate

MsgBox ("Data is added successfully")

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
@davesexcel
- minor typo - should be " , " (after rows.count)
Range("K2:K" & Cells(Rows.Count , "K").End(xlUp).Row).Clear

@Guinaba
.ClearContents clears value only
.Clear also removes formatting
- choose whichever is best for you

Are you trying to clear only the filtered row items in K?
If there is NOTHING below your data, then the same code could be simplified slightly

filter the data first and then ...
Code:
Range("K2:K" & Rows.Count).ClearContents

Personally (especially when reviewing the code later) I prefer it to be obvious that only filtered cells are being cleared
Code:
Range("K2:K" & Rows.Count).SpecialCells(xlCellTypeVisible).ClearContents
 
Upvote 0
Thanks @Yongle for your suggestion, I have added the code below but didn't work as expected, not sure if I am missing any thing :unsure:


Private Sub CommandButton1_Click()

'declare object variable to hold reference to cells to clear
Dim PORange As Range

Application.ScreenUpdating = False

'Selecting the correct sheet
Worksheets("MRPReqLive").Activate

'Inputbox to insert the date factor
DaysToMRPDate = TextBox1.Value
If DaysToMRPDate = "" Then
Exit Sub
End If

'Filter all the Purchase Order <> 0
ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).AutoFilter Field:=5, Criteria1:="<>0", _
Operator:=xlAnd

'Clear range before adding the new records
Set PORange = ThisWorkbook.Worksheets("MRPReqLive").Range("K2", Range("K1").End(xlDown))
PORange.ClearContents



'Copy the value from the user form into the AddDayFactor
Range("K2", Range("K1").End(xlDown)).Value = TextBox1.Value

'Remove filter
ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).AutoFilter Field:=5
 
Upvote 0
@Yongle forgot to mention...have tried both options:

'Clear range before adding the new records
Set PORange = ThisWorkbook.Worksheets("MRPReqLive").Range("K2", Range("K1").End(xlDown))
PORange.ClearContents

and

'Clear range before adding the new records
Set PORange = ThisWorkbook.Worksheets("MRPReqLive").Range("K2", Range("K1").End(xlDown))
PORange.SpecialCells(xlCellTypeVisible).ClearContents


Cheers,

Gilly
 
Upvote 0
@Yongle forgot to mention...have tried both options:
You asked for help but did not try ANY of the solutions we provided :unsure:
- your issue is caused by using End(xlDown) which is an unreliable way find the last used cell in a column

I leave it to you to work out why ...
Range("K1").End(xlDown)
might give a different result to ...
Range("K" & Rows.Count).End(xlUp)

This may help ...
put cursor in K1 and then {End}{Down Arrow}
put cursor in K10484576 (or K65536 if using Excel 2003) and then {End}{Up Arrow}

This should work for you assuming that there is nothing below the data - last used cell is not used, all visible cells in the column below row 1 are cleared
VBA Code:
ThisWorkbook.Worksheets("MRPReqLive").Range("K2:K" & Rows.Count).SpecialCells(xlCellTypeVisible).ClearContents
 
Upvote 0
@Yongle I did trid both otpions suggested...anyway the last code proposed by you worked perfectly...thanks!
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

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