Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default If Value in Range then Delete Column

Hi,

I have values in the range C1:J149 on Sheet 2.
Via code, I wish to look at the values in the range C1:J1 and if the
value is also in the range D27:D34 on Sheet 1, then delete the
respective column accomodating that value in Sheet 2.

Other Info:
Both ranges are dynamic, in that not all cells will have values.


eg
Sheet 2 Range "C1" = 200535
Sheet 2 Range "D1" = 200536
Sheet 2 Range "E1" = 200537
Sheet 1 Range "D27" = 200535
Sheet 1 Range "D28" = 200536

Result:
Sheet 2 Column "C" (200535) deleted.
Sheet 2 Column "D" (200536) deleted.

All other data in Sheet 2 remains intact.


Any help appreciated.

Cheers

Bob

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default If Value in Range then Delete Column

Try:

Sub Test()
Dim r As Range, c As Range
Dim DRng As Range

Set r = Sheets("Sheet1").Range("D27:D34")
For Each c In Sheets("Sheet2").Range("C1:J1")
If Len(c) 0 And Application.CountIf(r, c.Value) 0 Then
If DRng Is Nothing Then Set DRng = c Else Set DRng = Union(DRng, c)
End If
Next
If Not DRng Is Nothing Then DRng.EntireColumn.Delete
End Sub

Regards,
Greg

"Ozbobeee" wrote:

Hi,

I have values in the range C1:J149 on Sheet 2.
Via code, I wish to look at the values in the range C1:J1 and if the
value is also in the range D27:D34 on Sheet 1, then delete the
respective column accomodating that value in Sheet 2.

Other Info:
Both ranges are dynamic, in that not all cells will have values.


eg
Sheet 2 Range "C1" = 200535
Sheet 2 Range "D1" = 200536
Sheet 2 Range "E1" = 200537
Sheet 1 Range "D27" = 200535
Sheet 1 Range "D28" = 200536

Result:
Sheet 2 Column "C" (200535) deleted.
Sheet 2 Column "D" (200536) deleted.

All other data in Sheet 2 remains intact.


Any help appreciated.

Cheers

Bob


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete some column headings from an autofiltered range. Champ Excel Worksheet Functions 1 February 17th 10 06:39 AM
Copy column header to next column, delete & delete every nth colum genehunter New Users to Excel 1 June 2nd 09 03:57 PM
How can I insert and delete rows within a locked cell range column Gordon Cole Excel Worksheet Functions 0 October 1st 06 11:30 AM
How to Delete a Range in Closed Workbook (to Replace Delete Query) [email protected] Excel Discussion (Misc queries) 1 March 8th 06 10:10 AM
Delete row from range using key column onedaywhen Excel Programming 1 August 22nd 03 06:05 PM


All times are GMT +1. The time now is 06:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"