• Beginner Marco (Excel 2003)

    Author
    Topic
    #439395

    Hi Hans and all

    I have the following marco, Is there a better way to simplifies it? What is the last line Set rg = Nothing? Do I need to put
    also Set rg2 = Nothing.

    Sub InputRange()

    Dim rg As Range
    Dim rg2 As Range
    Set rg = ActiveSheet.Range(“A2”)
    Set rg2 = ActiveSheet.Range(“A3”)
    rg.Value = 500
    rg2.Value2 = 5000
    rg.Font.ColorIndex = 3
    rg2.Font.Bold = True
    Set rg = Nothing

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #1049904

      whisper It’s called a macro, not a marco. smile

      The macro can be simplified to

      Sub InputRange()
      With Range(“A2”)
      .Value = 500
      .Font.ColorIndex = 3
      End With
      With Range(“A3”)
      .Value2 = 5000
      .Font.Bold = True
      End With
      End Sub

      In this version, no object variables are used, so you don’t have to set anything to Nothing.

      In general, it is good programming practice to set all object variables to Nothing at the end of a Sub or Function. An object variable is the kind of variable that is assigned a value using Set.

      • #1049915

        Hans, As always, thanks you. The codes I wrote look clumsy as compare to yours.

    Viewing 0 reply threads
    Reply To: Beginner Marco (Excel 2003)

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

    Your information: