Results 1 to 7 of 7

Setting Range based on LastCell

Threaded View

  1. #1
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182

    Setting Range based on LastCell

    Hello Again,

    I am updating some code that allows me to ask users when they change a cell if they "are sure they want to make the change". Current code is posted below for your reference. I am interested in figuring out how to make a static range actually based on the LastCell.

    See in the below code the rng is = to A8:BB50000. I would like it to be set for A8:LastCell

    Set rng = Intersect(Me.Range("A8:BB5000"), Target)
    Below is the code in it's entirety.
    Thanks for your help!

    Cheers,
    Matt

    '=============>>
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim res As Long
    Dim oldVal As Variant
    Dim newVal As Variant
    Dim sAdd As String
    
    If Target.Count > 1 Then Exit Sub
    
    On Error GoTo XIT
    
    With Application
    .EnableEvents = False
    .ScreenUpdating = False
    End With
    
    Set rng = Intersect(Me.Range("A7:BB5000"), Target)
    
    If Not rng Is Nothing Then
    sAdd = ActiveCell.Address
    newVal = Target.Formula
    Application.Undo
    oldVal = rng.Formula
    
    With rng
    
    If Not IsEmpty(.Value) And newVal <> oldVal Then
    res = MsgBox( _
    Prompt:="Are you sure you want " & _
    "to change the value of " & _
    "Cell " & rng.Address(0, 0) & "?", _
    Buttons:=vbYesNo)
    
    If res = vbNo Then
    .Formula = oldVal
    '.Font = oldFont
    Else
    .Formula = newVal
    .Font.Bold = True
    .Font.ColorIndex = 51
    End If
    Else
    .Formula = newVal
    .Font.ColorIndex = 51
    End If
    End With
    
    End If
    
    If Not res = vbNo Then Me.Range(sAdd).Activate
    
    XIT:
    With Application
    .EnableEvents = True
    .ScreenUpdating = True
    End With
    
    'With Target
    '.Font.Bold = True
    '.Font.ColorIndex = 51
    'End With
    
    End Sub
    Last edited by VBA Noob; 11-21-2008 at 04:46 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1