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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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
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
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,214,561
Messages
6,120,234
Members
448,951
Latest member
jennlynn

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
Back
Top