VBA Hiding Columns

DLRollings

New Member
Joined
Dec 19, 2014
Messages
6
Based on below being columns, can anyone do me a macro to hid columns contacting saf, nps and chn. I'd need 3 separate ones that I can run as and when I need. Other columns will have various other codes, trans, triage to name a few.

Any help would be much appreciated, i'm still new to this and not really getting it! :confused:

domaneetaannaanneashrafbjarne
gesafnpsgechnge

<tbody>
</tbody>
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

mas123

New Member
Joined
Apr 20, 2010
Messages
41
You can use this macro for searching to saf and hide it's column
Code:
Sub Macro1()
    Cells.Find(What:="saf", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
    ActiveCell.EntireColumn.Hidden = True
    Range("a1").Select
End Sub

you can repeat it for other words
 
Upvote 0

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
Try this:
Code:
Sub Hide_Word()
Dim Word As String
[A1].Select
Word = InputBox("What Word")
   For Each cell In ActiveSheet.UsedRange
    
    Cells.Find(What:=Word, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
    ActiveCell.EntireColumn.Hidden = True
    Next
[A1].Select

End Sub
 
Upvote 0

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,648
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
I'd also add to this code, to allow the user to Unhide ALL columns if no word is selected
AND
I wouldn't use WORD as the string....str or Wrd would be Safer, and less confusing
Code:
Sub Hide_Word()
Dim Word As String
Word = InputBox("What Word")
If Word = "" Then Cells.Columns.Hidden = False
   For Each cell In ActiveSheet.UsedRange
    Cells.Find(What:=Word, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
    ActiveCell.EntireColumn.Hidden = True
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,662
Messages
6,010,992
Members
441,579
Latest member
satishrazdhan

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
Top