Question

This is my first time asking a question, so please bear with me.

I have made a macro to hide columns in a named range based on a criteria using FOR-NEXT loop. The result always displays 2 columns.

With the resulting 2 columns, I make calculations.

I am facing problem to get the column number using vba of the resulting displayed columns and use them in the offset formula to make the calculations.

Can anybody help? Thanks

Public Sub Custom_Report_Monthly()
On Error Resume Next

If Len(Range("Rpt_Type_M").Value) < 1 Then
MsgBox "Select Report Type"
GoTo ExitSub
ElseIf Len(Range("Select_Month").Value) < 1 Then
MsgBox "Select Month"
GoTo ExitSub
End If

ActiveSheet.Range("D_columns").EntireColumn.Hidden = True

If Range("Rpt_Type_M").Value = "Quantity" Then
   For Each c In Range("Titles")
   If c.Value = "Quantity" Then
   c.Columns.EntireColumn.Hidden = False
   End If
   Next
ElseIf Range("Rpt_Type_M").Value = "Sales" Then
   For Each c In Range("Titles")
   If c.Value = "Sales" Then
   c.Columns.EntireColumn.Hidden = False
   End If
   Next
ElseIf Range("Rpt_Type_M").Value = "Cost" Then
   For Each c In Range("Titles")
   If c.Value = "Cost" Then
   c.Columns.EntireColumn.Hidden = False
   End If
   Next
ElseIf Range("Rpt_Type_M").Value = "Sales+Cost" Then
   For Each c In Range("Titles")
   If c.Value = "Sales" Then
   c.Columns.EntireColumn.Hidden = False
   End If
   Next
   For Each c In Range("Titles")
   If c.Value = "Cost" Then
   c.Columns.EntireColumn.Hidden = False
   End If
   Next
End If


For Each c In Range("P_Months")
   If Month(c.Value) <> Range("Select_Month_Num").Value Then
   c.Columns.EntireColumn.Hidden = True
   End If
Next

For Each col In Range("D_columns") ' **this is the block where i am having problem**
   If col.EntireColumn.Hidden = False Then
   MsgBox (Range("col").Column)
   End If
Next

Call Hide_Count_Columns
ExitSub:

End Sub
Was it helpful?

Solution

You need to store the column numbers to use them. Declare them first:

    Dim col As Object
    Dim intCol1 As Integer
    Dim intCol2 As Integer

Use this to get the variables

    For Each col In Range("D_columns").Columns
        If col.EntireColumn.Hidden = False Then
            If intCol1 = 0 Then
                intCol1 = col.Column
            Else
                intCol2 = col.Column
            End If
        End If
    Next

If you need to pass the variables to Hide_Count_Columns, do it like this:

    Call Hide_Count_Columns(intCol1, intCol2)

Then you would declare the sub Hide_Count_Columns like this:

Sub Hide_Count_Columns(ByVal intCol1 As Integer, ByVal intCol2 As Integer)

'do something with column numbers

End Sub

Or if you do want to put the column numbers on the worksheet, you would create range names for them intCol1 and intCol1 Then put the column numbers there like this:

ActiveSheet.Range("intCol1").Value = intCol1
ActiveSheet.Range("intCol2").Value = intCol2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top