How to delete column(s) with particular values in Excel?

  • Context: Calculators 
  • Thread starter Thread starter Adel Makram
  • Start date Start date
  • Tags Tags
    Delete Excel
Click For Summary

Discussion Overview

The discussion revolves around methods to delete specific columns in Excel based on certain criteria related to the values in designated rows. The focus includes both macro-based solutions and manual methods, with varying conditions for column deletion.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant seeks a way to automatically delete columns that contain a value of (0) in a specific row.
  • Another participant suggests using a VBA macro to delete columns based on the presence of (0) in a specified row, providing a code snippet for implementation.
  • A participant clarifies their requirements, stating they want to delete columns that do not contain (1) in either row 1 or 2, and also those that contain (1) in rows 1 and 2 but also have (1) in row 4.
  • A different participant proposes a manual method using the find function to locate (1) in row 4 and delete the corresponding columns, which they find simpler.
  • Another participant critiques the manual method as tedious and prone to error, advocating for the macro approach as more efficient for repeated tasks.
  • There is a correction regarding the attribution of the macro suggestion, clarifying that it was made by BvU and not the original poster, Adel.

Areas of Agreement / Disagreement

Participants express differing opinions on the best method to achieve the desired column deletion, with some favoring the macro approach for its efficiency and others preferring a manual method for its simplicity. No consensus is reached on a single preferred solution.

Contextual Notes

Participants discuss specific criteria for column deletion that may depend on the structure of their data, indicating that solutions may vary based on individual use cases and data arrangements.

Adel Makram
Messages
632
Reaction score
15
In Excel, I wish to automatically delete multiple columns that have (0) in particular row location. How to do this? After the deletion, the data array should shrink to only columns that do not have (0) at that row.
 
Computer science news on Phys.org
Hi Adel,

Easiest is a Macro like (assuming data is in C4:I10 and the row with the criterion zeroes is Row 10):
Code:
Private Sub CommandButton1_Click()

    For iColumn = 9 To 3 Step -1
    
    If ActiveSheet.Cells(10, iColumn).Value = 0 Then
        ActiveSheet.Columns(iColumn).Delete Shift:=xlToLeft
    End If
    
    Next

End Sub
 
BvU said:
Hi Adel,

Easiest is a Macro like (assuming data is in C4:I10 and the row with the criterion zeroes is Row 10):
Code:
Private Sub CommandButton1_Click()

    For iColumn = 9 To 3 Step -1
 
    If ActiveSheet.Cells(10, iColumn).Value = 0 Then
        ActiveSheet.Columns(iColumn).Delete Shift:=xlToLeft
    End If
 
    Next

End Sub
Hi BvU, may be I was not clear in describing what I wish to do. I attach a data that I want to operate on. In this data I wish to conditionally delete any columns that does not contain the value (1) in either row 1 or 2 and any column that contains (1) in row 1 and 2 but contain other (1) in row 4. After the deletion, only columns A and B remain.
 

Attachments

  • Snap 2017-01-01 at 18.14.26.png
    Snap 2017-01-01 at 18.14.26.png
    3.1 KB · Views: 629
Last edited:
Adel Makram said:
Hi BvU, may be I was not clear in describing what I wish to do. I attach a data that I want to operate on. In this data I wish to conditionally delete any columns that does not contain the value (1) in either row 1 or 2 and any column that contains (1) in row 1 and 2 but contain other (1) in row 4. After the deletion, only columns A and B remain.
I found a simple solution. I highlight row 4 then use the function (find), this will find the value (1) in row 4 and then I delete all columns containing (1) leaving only columns A and B as desired.
 
Adel Makram said:
I found a simple solution. I highlight row 4 then use the function (find), this will find the value (1) in row 4 and then I delete all columns containing (1) leaving only columns A and B as desired.
Yes, but that's a relatively tedious manual method, will need to be repeated if the data changes, and is subject to human error if repeated. Adel's suggestion of a macro is much more versatile and easily repeatably. If you need this sort of thing very often, you would do well to learn a small amount of VBA.
 
phinds said:
Adel's suggestion...
Adel is the OP. The suggestion was from BvU.
 
Mark44 said:
Adel is the OP. The suggestion was from BvU.
OOPS. Thanks for catching that Mark & my apologies to BvU
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
Replies
27
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
11K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K