• Range definition in VBA

    Author
    Topic
    #458195

    I’m trying to define a range of cells. Would the following code work?

    Dim rng As Range
    Set rng = ActiveWorkbook.Worksheets(“Sheet1”).Range(“A5:A20”)

    Viewing 0 reply threads
    Author
    Replies
    • #1151409

      Yes, that’s the way to set a Range variable in VBA. You can now do things with the variable rng, for example

      rng.Interior.ColorIndex = 3

      or loop through its cells:

      Dim oCell As Range
      For Each oCell In rng

      Next oCell

      • #1151446

        Any idea why I get a “Run-time error ‘1004’: Application-defined or Object-defined error” in this line of the module:

        Sub ShowDialog()

        With frmProperties ‘ <<< Error shows up here
        .cmbType.RowSource = ""
        .lstName.RowSource = ""
        .lstFinish.RowSource = ""
        .lstProperties_BOL.RowSource = ""
        .lstProperties_EOL.RowSource = ""
        .lstName.MultiSelect = fmMultiSelectMulti
        .lstFinish.MultiSelect = fmMultiSelectMulti
        .lstProperties_BOL.MultiSelect = fmMultiSelectExtended
        .lstProperties_EOL.MultiSelect = fmMultiSelectExtended
        End With

        ' Assign material names to ComboBox
        GetMaterialNames
        ' Show the UserForm
        frmProperties.Show
        End Sub

        When I use the following code in the userform module?

        Private Sub UserForm_Initialize()

        ' Worksheets("Properties Database").Activate
        ' Set rngAllMaterialTypes = ActiveWorkbook. _
        Worksheets("Properties Database"). _
        Range("A5", Range("A65536").End(xlUp)) ' <<<< WORKS when removing the two comment signs

        Set rngAllMaterialTypes = ActiveWorkbook. _
        Worksheets("Properties Database"). _
        Range("A5", Range("A65536").End(xlUp)) ' <<<< DOESN'T WORK

        • #1151485

          Try

          Dim wsh As Worksheet
          Set wsh = Worksheets(“Properties Database”)
          Set rngAllMaterialTypes = wsh.Range(wsh.Range(“A5”), wsh.Range(“A65536”).End(xlUp))

          or

          Dim wsh As Range Worksheet
          Dim m As Long
          Set wsh = Worksheets(“Properties Database”)
          m = wsh.Range(“A65536”).End(xlUp).Row
          Set rngAllMaterialTypes = wsh.Range(“A5:A” & m)

          • #1151503

            Thanks Hans. I now get a “Run-time error ’13’: Type mismatch” at this line:

            With frmProperties

            Do you know why?

            • #1151511

              In the second example I should have had

              Dim wsh As Worksheet

              instead of

              Dim wsh As Range

            • #1151516

              In the second example I should have had

              Dim wsh As Worksheet

              instead of

              Dim wsh As Range

              That did it. Thanks.

    Viewing 0 reply threads
    Reply To: Range definition in VBA

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: