|First, a posting tip: |
Please click on the blue line at the end of this post and read the instructions on how to post VBA code in this forum so that it is easier for us to read. The How-To will explain the use of the "pre" tags. Thanks!
As far as your question, the following code is kind of a brute force method to achieve your goal. If I knew more about the layout of your spreadsheet or the process you are using, I'm sure I could streamline it a bit. You asked for "the base" and this is pretty basic.
The first thing you will notice is that am not individually hiding the columns I don't want to see. Instead, I am hiding all of them first and then making visible just the ones I want to see.
In addition, the code assumes your company names are in Row 3 and that there is a Company name above each month, as shown below. Feel free to correct me if I am wrong.
A G H ... R S T ... AD AE AF ... AP
3 Company NR NR ... NR BB BB ... BB MN MN ... MN
4 Month Jan Feb ... Dec Jan Feb ... Dec Jan Feb ... Dec
Finally, as I said earlier, this is a brute force method with the company names (e.g. NR and MN) and the months (e.g. Jan and Feb) hard coded into the macro. As written, you would need an additional "Or" clause for each additional company or month that you wanted to see. In real life, these values could probably be put into a range of cells and looped through/pulled into the code, but since I don't know enough about your spreadsheet layout I didn't offer any suggestions related to that method.
Let me know what you think...
Dim lastCol As Long, nxtCol As Long
'Determine last column with data in Row 4
lastCol = Cells(4, Columns.Count).End(xlToLeft).Column
'Hide all columns in table (G:Last Column
Range(Cells(1, 7), Cells(1, lastCol)).EntireColumn.Hidden = True
'Unhide Jan & Feb for Companies NR & MN
For nxtCol = 7 To lastCol
If (Cells(3, nxtCol).Value = "NR" Or _
Cells(3, nxtCol).Value = "MN") And _
(Cells(4, nxtCol).Value = "Jan" Or _
Cells(4, nxtCol).Value = "Feb") Then
Cells(4, nxtCol).EntireColumn.Hidden = False
Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.