Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Delete column if a cell is not named

I would like to do the following within VBA:
- Select range from first cell to last cell in first row of a database.
- Find arguments in this range (e.g. if cell value = "Sales" then name the
cell = "Sales"). I have several arguments.
- Afterwards, I want to delete all columns that do not contain these
arguments (in the example above: If the first cell of a column is not
"Sales", then delete the column).

My main problem: How do I check if a cell is named or not?

Thank you in advance!
Mirja

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Delete column if a cell is not named

Hi Mirja,

Sub test()
On Error Resume Next
sName = ""
sName = Range("A1").Name.Name
On Error GoTo 0
If sRangeName = "" Then
MsgBox "not named"
Else
MsgBox sName
End If
End Sub

Regards,
Peter T

"Mirja" wrote in message
...
I would like to do the following within VBA:
- Select range from first cell to last cell in first row of a database.
- Find arguments in this range (e.g. if cell value = "Sales" then name the
cell = "Sales"). I have several arguments.
- Afterwards, I want to delete all columns that do not contain these
arguments (in the example above: If the first cell of a column is not
"Sales", then delete the column).

My main problem: How do I check if a cell is named or not?

Thank you in advance!
Mirja



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Delete column if a cell is not named

Hi Peter,
thanks for replying. Unfortunately, this did not work. The command is for a
special name, isn't it? But I like to check all cells of the first row for
ANY name. So if a cell has any name, then don't delete the column, if a cell
is not named, delete the column ... Do you know how to do that?
Thanks again!
Mirja

"Peter T" wrote:

Hi Mirja,

Sub test()
On Error Resume Next
sName = ""
sName = Range("A1").Name.Name
On Error GoTo 0
If sRangeName = "" Then
MsgBox "not named"
Else
MsgBox sName
End If
End Sub

Regards,
Peter T

"Mirja" wrote in message
...
I would like to do the following within VBA:
- Select range from first cell to last cell in first row of a database.
- Find arguments in this range (e.g. if cell value = "Sales" then name the
cell = "Sales"). I have several arguments.
- Afterwards, I want to delete all columns that do not contain these
arguments (in the example above: If the first cell of a column is not
"Sales", then delete the column).

My main problem: How do I check if a cell is named or not?

Thank you in advance!
Mirja




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Delete column if a cell is not named

The example code is not looking for a "special name", it simply returns the
name of A1 if it has a name, otherwise the empty string confirms A1 is not a
named cell.

If I understand your original correctly you want to delete entire columns of
header cells that are not named. Following should delete all columns between
B1:J1 (header range) if the cell in row 1 is not a named cell.

Sub test3()
Dim col As Long
Dim rHeaders As Range

Set rHeaders = ActiveSheet.Range("B1:J1")
For col = rHeaders.Count To 1 Step -1
On Error Resume Next
sname = ""
sname = rHeaders(1, col).Name.Name
On Error GoTo 0

If sname = "" Then
ActiveSheet.Columns(rHeaders(1, col).Column).Delete
End If
Next
End Sub

Use with caution, might accidently delete data if the header cell is not
named.

Regards,
Peter T



"Mirja" wrote in message
...
Hi Peter,
thanks for replying. Unfortunately, this did not work. The command is for

a
special name, isn't it? But I like to check all cells of the first row for
ANY name. So if a cell has any name, then don't delete the column, if a

cell
is not named, delete the column ... Do you know how to do that?
Thanks again!
Mirja

"Peter T" wrote:

Hi Mirja,

Sub test()
On Error Resume Next
sName = ""
sName = Range("A1").Name.Name
On Error GoTo 0
If sRangeName = "" Then
MsgBox "not named"
Else
MsgBox sName
End If
End Sub

Regards,
Peter T

"Mirja" wrote in message
...
I would like to do the following within VBA:
- Select range from first cell to last cell in first row of a

database.
- Find arguments in this range (e.g. if cell value = "Sales" then name

the
cell = "Sales"). I have several arguments.
- Afterwards, I want to delete all columns that do not contain these
arguments (in the example above: If the first cell of a column is not
"Sales", then delete the column).

My main problem: How do I check if a cell is named or not?

Thank you in advance!
Mirja






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Delete column if a cell is not named

Thanks, Peter! Now it worked out :-)

"Peter T" wrote:

The example code is not looking for a "special name", it simply returns the
name of A1 if it has a name, otherwise the empty string confirms A1 is not a
named cell.

If I understand your original correctly you want to delete entire columns of
header cells that are not named. Following should delete all columns between
B1:J1 (header range) if the cell in row 1 is not a named cell.

Sub test3()
Dim col As Long
Dim rHeaders As Range

Set rHeaders = ActiveSheet.Range("B1:J1")
For col = rHeaders.Count To 1 Step -1
On Error Resume Next
sname = ""
sname = rHeaders(1, col).Name.Name
On Error GoTo 0

If sname = "" Then
ActiveSheet.Columns(rHeaders(1, col).Column).Delete
End If
Next
End Sub

Use with caution, might accidently delete data if the header cell is not
named.

Regards,
Peter T



"Mirja" wrote in message
...
Hi Peter,
thanks for replying. Unfortunately, this did not work. The command is for

a
special name, isn't it? But I like to check all cells of the first row for
ANY name. So if a cell has any name, then don't delete the column, if a

cell
is not named, delete the column ... Do you know how to do that?
Thanks again!
Mirja

"Peter T" wrote:

Hi Mirja,

Sub test()
On Error Resume Next
sName = ""
sName = Range("A1").Name.Name
On Error GoTo 0
If sRangeName = "" Then
MsgBox "not named"
Else
MsgBox sName
End If
End Sub

Regards,
Peter T

"Mirja" wrote in message
...
I would like to do the following within VBA:
- Select range from first cell to last cell in first row of a

database.
- Find arguments in this range (e.g. if cell value = "Sales" then name

the
cell = "Sales"). I have several arguments.
- Afterwards, I want to delete all columns that do not contain these
arguments (in the example above: If the first cell of a column is not
"Sales", then delete the column).

My main problem: How do I check if a cell is named or not?

Thank you in advance!
Mirja









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Delete column if a cell is not named

For completeness I see I forgot to declare the variable 'sname'

Dim sName As String

Regards,
Peter T

PS glad you got it working


"Peter T" <peter_t@discussions wrote in message
...
The example code is not looking for a "special name", it simply returns

the
name of A1 if it has a name, otherwise the empty string confirms A1 is not

a
named cell.

If I understand your original correctly you want to delete entire columns

of
header cells that are not named. Following should delete all columns

between
B1:J1 (header range) if the cell in row 1 is not a named cell.

Sub test3()
Dim col As Long
Dim rHeaders As Range

Set rHeaders = ActiveSheet.Range("B1:J1")
For col = rHeaders.Count To 1 Step -1
On Error Resume Next
sname = ""
sname = rHeaders(1, col).Name.Name
On Error GoTo 0

If sname = "" Then
ActiveSheet.Columns(rHeaders(1, col).Column).Delete
End If
Next
End Sub

Use with caution, might accidently delete data if the header cell is not
named.

Regards,
Peter T



"Mirja" wrote in message
...
Hi Peter,
thanks for replying. Unfortunately, this did not work. The command is

for
a
special name, isn't it? But I like to check all cells of the first row

for
ANY name. So if a cell has any name, then don't delete the column, if a

cell
is not named, delete the column ... Do you know how to do that?
Thanks again!
Mirja

"Peter T" wrote:

Hi Mirja,

Sub test()
On Error Resume Next
sName = ""
sName = Range("A1").Name.Name
On Error GoTo 0
If sRangeName = "" Then
MsgBox "not named"
Else
MsgBox sName
End If
End Sub

Regards,
Peter T

"Mirja" wrote in message
...
I would like to do the following within VBA:
- Select range from first cell to last cell in first row of a

database.
- Find arguments in this range (e.g. if cell value = "Sales" then

name
the
cell = "Sales"). I have several arguments.
- Afterwards, I want to delete all columns that do not contain these
arguments (in the example above: If the first cell of a column is

not
"Sales", then delete the column).

My main problem: How do I check if a cell is named or not?

Thank you in advance!
Mirja








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
Reference Column of Named Cell vba Isis[_2_] Excel Discussion (Misc queries) 6 May 3rd 10 01:44 PM
Named range/cell, sort & delete nc Excel Discussion (Misc queries) 1 July 11th 09 12:55 AM
delete numbers in each cell in same column Anna Huber Excel Worksheet Functions 7 June 11th 09 10:33 PM
Find first blank cell in single column named range tig Excel Programming 9 February 9th 06 05:39 PM
VBA Excel Macro to delete contents in named cell reaa Excel Discussion (Misc queries) 1 January 3rd 06 08:16 PM


All times are GMT +1. The time now is 04:09 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"