LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Delete Columns Macro

Try what you think will work, BUT, do it on a test file, not your original
file. The best way to learn VBA is by trial and error. When what you think
should work does not work, step through the code one line at a time to see
what it is doing. The tool tips feature of the VBE will show the values of
variables and objects when you mouse over them. Or you can use the locals
window to see all variables and their values and the immediate window to
view debug.print messages. There are plenty of safeguards built into VBA
that prevents you from breaking anything by trying different code. But like
I said, do it on a test file because you can wipe out a file if you write
bad code, and in some cases, it might not be recoverable. So when you text
code, make sure you have any workbooks closed that are not needed to test
the code.
After you have tried to do it yuurself and cannot get it to work. Post in
the NG for help.



"cardan" wrote in message
...
On May 7, 10:30 am, Chip Pearson wrote:
I inserted the code per JLGWhiz's instructions and inserted it into
the module. It worked great during a test. Now I am getting the same
response- nothing.


By default, yellow is the background color of the pending line of code
to run in the debugger, so VBA is just sitting there waiting for you
to F8 to go to the next line or F5 to let it run through.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLCwww.cpearson.com
[email on web site]

On Fri, 7 May 2010 10:13:20 -0700 (PDT), cardan

wrote:
On May 7, 4:16 am, Chip Pearson wrote:
Hi Chip, Thank you for your response. I must apologize beforehand
because my macro skills are inferior. I pasted the code into my macro
and I cannot seem to get it work- or do anything.


Seehttp://www.cpearson.com/excel/WhereToPutTheCode.aspxfordetails
about where to put and how to use code that you find here and other
venues. It explains the differences between snippets, procedures, and
modules, how they relate to one another, and how to use them.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLCwww.cpearson.com
[email on web site]


On Thu, 6 May 2010 17:12:32 -0700 (PDT), cardan


wrote:
On May 6, 3:47 pm, Chip Pearson wrote:
The following code should do what you want. If your data is on more
than one worksheet, select all the relevant worksheets by holding
down
the CTRL key and clicking the sheet tab. The code will delete from
each selected sheet those columns whose value in row 1 is "delete".


Sub DeleteColumns()
Dim WS As Worksheet
Dim R As Range
Dim DeleteThese As Range
Dim LastCol As Long
Dim C As Long


For Each WS In _
Application.ActiveWindow.SelectedSheets
Set DeleteThese = Nothing
With WS
LastCol = .Cells(1, .Columns.Count) _
.End(xlToLeft).Column
For C = LastCol To 1 Step -1
If .Cells(1, C).Value = "delete" Then
If DeleteThese Is Nothing Then
Set DeleteThese = .Columns(C)
Else
Set DeleteThese = _
Application.Union(DeleteThese, .Columns(C))
End If
End If
Next C
If Not DeleteThese Is Nothing Then
DeleteThese.Delete
End If
End With
Next WS
End Sub


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLCwww.cpearson.com
[email on web site]


On Thu, 6 May 2010 14:18:11 -0700 (PDT), cardan


wrote:
Hello,


I have searched this forum for a macro that will just delete
columns
based on a value in row1. It seems everyone has a twist on what
they
need and I am not smart enough to extract just what I need from the
code. I am not looking for just the clearing of content, but the
actual deletion of the column.


I have a data set that may be over 200 columns long and on numerous
sheets. (the number of columns will be different in each sheet).


I am going to write a formula in Row 1 that will identify the
columns
I want to keep and the ones I want to delete. To make things
evident,
I am writing the formula in row 1 so the columns I want to keep
will
return the word "keep" and the ones I want to delete return the
word
"delete".


I would then like to delete the columns that have the word "delete"
in
row 1. I have heard that some of the best macros will identify all
the columns first and then delete them all at once. Unfortunately,
I
wouldn't even know where to begin. Any help would be greatly
appreciated. Thank you for your time.


Hi Chip, Thank you for your response. I must apologize beforehand
because my macro skills are inferior. I pasted the code into my macro
and I cannot seem to get it work- or do anything.
Usually when I input a macro, I will cheat and record a macro and then
immediately stop recording and input the code. I tried it on this and
nothing happens. This is what it looks like in my VB. Is there
anything telling that I am doing wrong? Thanks again for your
feedback.


Sub DeleteColumns1()
'
' DeleteColumns1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Dim WS As Worksheet
Dim R As Range
Dim DeleteThese As Range
Dim LastCol As Long
Dim C As Long


For Each WS In _
Application.ActiveWindow.SelectedSheets
Set DeleteThese = Nothing
With WS
LastCol = .Cells(1, .Columns.Count) _
.End(xlToLeft).Column
For C = LastCol To 1 Step -1
If .Cells(1, C).Value = "delete" Then
If DeleteThese Is Nothing Then
Set DeleteThese = .Columns(C)
Else
Set DeleteThese = _
Application.Union(DeleteThese, .Columns(C))
End If
End If
Next C
If Not DeleteThese Is Nothing Then
DeleteThese.Delete
End If
End With
Next WS
End Sub


I inserted the code per JLGWhiz's instructions and inserted it into
the module. It worked great during a test. Now I am getting the same
response- nothing. When I step into the Macro the first line "Sub
DeleteColumns()" is highlighted in yellow. Is there a problem with
the name? Any feedback is always helpful. Thanks


I figured it out my issue. My formula was using capital letters and
the macro code did not account for capitals. (totally my fault per my
question). I did not know macros were case sensitive.

On another note, could I use this same formula for rows by replacing
the word Column with Row and "C"'s with"R"'s? I ask now because my
data set still may be too large and I may have to scale back some of
the rows. I would do it the same way- by writing a formula in the A
column and the have the macro search that and then delete. Thank you
again for all your help!


 
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
create a macro to delete columns and then border remaining columns Jane777 Excel Programming 1 July 18th 07 12:08 AM
Need Help Delete Columns Macro PLEASE! Dan Thompson Excel Programming 1 November 1st 06 10:23 PM
Using a macro to delete last 4 rows and last 2 columns which vary Shirley Munro Excel Programming 3 September 1st 05 04:56 PM
Macro to Delete empty columns Darth Cor Excel Programming 1 June 2nd 05 02:08 PM
Macro to delete columns Luong Excel Programming 3 August 12th 03 07:13 PM


All times are GMT +1. The time now is 02:01 AM.

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"