VBA to hide columns based on Criteria and change a column's position

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Guys,

I'm trying to write a macro that will hide columns and change a columns position based on criteria. You refer to sample data below:

*What I need here is to hide "Sports" & "Day" columns

NameDaySportsScore
Ben1Basketball15
Sean2Basketball21
Ana1Tennis4

<tbody>
</tbody>


*Another, how will I force to move the "Score" column to the first column (Row A)

Thanks for any help!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How about
Code:
Sub movehidecols()

   Columns(4).Cut
   Columns(1).Insert
   Columns("C:D").Hidden = True
End Sub
 
Upvote 0
Thanks Fluff for quickly answering this. Each columns is actually interchangeable, is there any code where we can specify the header name like instead of Columns (4) we'll use "Score" and the like? Let me know any of your thoughts. :)
 
Upvote 0
How about
Code:
Sub MoveHideCols()

   Dim Fnd As Range
   
   Set Fnd = Range("1:1").Find("Score", , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      Fnd.EntireColumn.Cut
      Columns(1).Insert
   End If
End Sub
 
Upvote 0
Last question Fluff..what if I want to hide Day & Score columns as well, how do you show that in codes?
 
Upvote 0
How about
Code:
Sub MoveHideCols()

   Dim Fnd As Range
   
   Set Fnd = Range("1:1").Find("Score", , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then
      Fnd.EntireColumn.Cut
      Columns(1).Insert
      Range("A:A,C:C").EntireColumn.Hidden = True
   End If
End Sub
 
Upvote 0
Thanks Fluff..I forgot that the columns are interchangeable. Can we be more specific based on header names instead of using "A:A" & "C:C". Thank you in advance :)
 
Upvote 0
If you want to hide the columns by header, you'd need to replicate the first line to find the various headers.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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