VBA to delete cells from columns preceding the lowest value in the column

mapa

New Member
Joined
Nov 8, 2024
Messages
2
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. MacOS
Dear All,
I've been passed a Macro to delete values after the highest within an array of values in a column. Now I'd like to do a similar operation, but deleting the values preceding the lowest.

The Macro I use for delete after the max is the following:
VBA Code:
Sub deleteaftermax()
Dim i As Integer

For i = 1 To ActiveSheet.UsedRange.Columns.Count

If WorksheetFunction.Count(Columns(i)) > 0 Then

Range(Columns(i).Find(What:=WorksheetFunction.Max(Columns(i)), LookIn:=xlFormulas, LookAt:=xlWhole).Offset(3), _

Cells(Rows.Count, i).End(xlUp)).Clear

End If

Next

End Sub

Many thanks,
marco
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi and welcome to MrExcel!

Try:

VBA Code:
Sub deleteafterMin()
  Dim i As Integer
  Dim f As Range
 
  For i = 1 To ActiveSheet.UsedRange.Columns.Count
    If WorksheetFunction.Count(Columns(i)) > 0 Then
      Set f = Columns(i).Find(WorksheetFunction.Min(Columns(i)), , xlFormulas, xlWhole)
      If Not f Is Nothing Then
        If f.Row > 3 Then
          Range(f.Offset(-3), Cells(1, i)).Clear
        End If
      End If
    End If
  Next
End Sub
 
Last edited:
Upvote 0
Solution
Welcome to the MrExcel board!

"Prefer not to say" for your Office version in your profile is not very helpful & I'm wondering why you wouldn't provide that information. :huh:
When people are trying to help you, it helps them to know what functions and features are available to you otherwise they may be wasting their time developing a suggestion that is not appropriate for you.

Also, when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

We do not know what is possible with your data, so is it possible that a column could have more than one equal maximum or minimum value?
Take the sample below. It has 4 equal maximum values (green). Your code would delete the values from 3 rows below the first maximum value as indicated in green in column B.
IF this sort of situation is possible, what do you want the code to do in relation to the minimum value (blue)?
  • Do you want it to delete from the last minimum value as indicated in blue in column B, or
  • Do you want it to delete from the first minimum value as indicated in blue in column C, or
  • Something else?
Also, is it possible that a column could have one max and one min but the min is below the max in the column like column F below?
If so, what should happen?

mapa.xlsm
ABCDEF
1
2
3
48Delete these?Or delete these?
58Delete these?Or delete these?
66Delete these?
712Delete these?8
85Delete these?8
9118
10813
1158
1268
13118
14158
15138
16151
1714These get deleted8
1815These get deleted
1914These get deleted
2015These get deleted
Sample
 
Upvote 0
@DanteAmor: It works, many thanks
@Peter_SSs: Sorry, I haven't specified the platform as I work on several releases, but if allowed, I'll update the profile to mention all of them. Concerning the type of data, I've experimental reads (typically 2k/sample) from which I need to extract the range of values corresponding to sigmoidal trend in the assay (performed on hundreds of samples). I've uploaded a picture to exemplify the process for one sample.
Presentation1.jpg
Many thanks again,
marco
 
Upvote 0
but if allowed, I'll update the profile to mention all of them.
Yes, that is certainly allowed. However, if you do show/have multiple versions and/or platforms you need to specify when you ask a question which of those versions/platforms any suggestion has to work in.


Concerning the type of data, I've experimental reads (typically 2k/sample) from which I need to extract the range of values corresponding to sigmoidal trend in the assay (performed on hundreds of samples). I've uploaded a picture to exemplify the process for one sample.
That didn't address my question about whether there could be multiple maximums or minimums etc. However, if Dante's suggestion does what you want, that doesn't matter so no need to revisit those questions.
 
Upvote 0

Forum statistics

Threads
1,223,836
Messages
6,174,921
Members
452,591
Latest member
Zai2809

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