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!
 
It work but I'm having a problem on this part "Range("A:A,C:C").EntireColumn.Hidden = True" as I have added more columns (now range from A1 to AH1) and sometimes the column positions differ. Thanks in advance for the help.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
In what way are you "having a problem"?
 
Upvote 0
I'm just having a problem on hiding the columns part. Example, the Sports column is in C1 but since the column is movable sometimes the macro hide the wrong column. So I was thinking instead of using the "Range("C:C")" maybe we can replace other code that specify the column name.

______________________________

Sub MoveHideCols()

Dim Fnd As Range

Set Fnd = Range("1:1").Find("Sports", , , xlWhole, , , False, , False)
If Not Fnd Is Nothing Then
Range("C:C").EntireColumn.Hidden = True
End If
End Sub

______________________________

Also, I have added columns like Time, Hours, Place, Coach & Result that I need to hide as well. Is there any way where I can hide all of these by avoiding using repetitive codes.

Let me know what you think. Thanks!
 
Last edited:
Upvote 0
You'll need to put it in a loop, along the lines of
Code:
   ary = Array("Time", "Hours")
   For i = LBound(ary) To UBound(ary)
      Set Fnd = Range("1:1").Find(ary(i), , , xlWhole, , , False, , False)
   Next i
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,628
Members
449,240
Latest member
lynnfromHGT

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