• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Load user form if selection is in a used range and exclude if there is a heading on top of the column heading from adding to a combobox

anishms

Member
Hi,
Request guidance in the following areas-
I have the following code to stop loading user form if the selection is outside a data range. As I'm using the counta formula, it is stopping if the selection is in a blank cell even within the data range. I also tried If TypeName(Selection) = "Range" Then, but that is also not working

Code:
Public Sub ShowForm()
    If WorksheetFunction.CountA(Selection) = 0 Then
        MsgBox "Oops! Please select inside the data range.", vbInformation
        Exit Sub
    Else
        Set rData = ActiveCell.CurrentRegion
    End If
    Load frmMain
    frmMain.Show
End Sub

Secondly, I'm loading the column headers to a combobox. But if there is a heading on top of the column heading, then that heading gets added to the combobox. For example, if I have a column heading in cell "A1" in the attached file. I can offset the range by 1 row but how can I make it dynamic (heading may or may not be there)

Thanks in Advance!
 

Attachments

  • list unique values - Copy.xlsm
    923.8 KB · Views: 7
Use a table.
First question
Code:
Public Sub ShowForm()
If Intersect(ActiveCell, ActiveSheet.ListObjects(1).DataBodyRange) Is Nothing Then
      MsgBox "Oops! Please select inside the data range.", vbInformation
      Exit Sub
   Else
     Set rData = ActiveCell.CurrentRegion
   End If
    Load frmMain
    frmMain.Show
End Sub

Second question
Code:
Sub UserForm_Initialize()
Dim ws As Worksheet
Dim tbl As ListObject
Set ws = ActiveSheet
Init
    Set tbl = ws.ListObjects(1)
    With combx_Fields
        '.Clear
        .Column = tbl.HeaderRowRange.Value
    End With
End Sub
I didn't look at the rest.
 

Attachments

  • Kopie van list unique values.xlsm
    927.2 KB · Views: 6
Thanks Belleke!
The data set may not be a table always. It works fine with table except the column headers get counted in the list box
Is there any way to do it for a normal data range?
Thanks for your time.
 
Back
Top