Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

VBA Macro worksheet_change does not work properly

$
0
0

I have a problem with properly functioning macro. I wrote a short and simple code to either hide or unhide two last columns, depending on the selected month.

Month selection is possible with a combo box. Each month has assigned a number to it (1 - January, 2 - February, ..., etc.). The number shows in cell A1 and it changes upon selection of a month.

I want my macro to run properly depending on the month number which I choose.

    Private Sub Worksheet_Change(ByVal Target As Range)
          Dim nr_kol As Long
          If Target.Address = "$A$1" Then
            For nr_kol = 32 To 34
                If Month(Cells(6, nr_kol)) = Cells(1, 1) Then
                    Columns(nr_kol).Hidden = False
                Else
                    Columns(nr_kol).Hidden = True
                End If
            Next
        End If

    End Sub

EDIT(sorry, clicked submit too fast)

Cell A1 changes depending on combobox selection (box is located in a different row). What I have noticed is that columns will hide, but only if cell A1 is changed manually. But when I change the cell to month index containing 30 or 31 days, columns will remain hidden, thus I have two issues.

  1. A1 cell needs to be changed manually (macro is not working whatsoever when change is made by a combobox selection)

  2. When I make a manual change to February - two or three last columns will hide (as these are for the first two/three days of March). However, when I change the number to anything but February (2), columns are still hidden even though at least one should re-appear back again (if month contains 30 days).


Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>