Results 1 to 3 of 3

Thread: how to lock all cell references in formulas?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    3

    how to lock all cell references in formulas?

    Is there a possibility to lock all cell references in a range without knowing the cell contents? For example, I have three cells:
    A1 = 3*B1/D$3
    A2 = $C4
    A3 = 125

    I would like my macro to change them:
    A1 = 3*$B$1/$D$3
    A2 = $C$4
    A3 = 125

    The problem is that I don't what formulas or values the cells contain or if they are even just empty cells. I just know the range that requires the action.

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: how to lock all cell references in formulas?

    It will be possible, but it won't be that easy to code up...

    you will need to loop around the cells in the range and then loop around the formula in each cell and scan the contents of the formula to check for cell reference...

    Try something like this

    VB Code:
    1. Dim i As Long
    2. Dim strTempVal As String
    3. Dim StartFlag As Boolean
    4. Dim NewForm As String
    5. Do While ActiveCell.Formula <> ""
    6.   NewForm = ""
    7.   For i = 1 To Len(ActiveCell.Formula)
    8.     If Mid(ActiveCell.Formula, i, 1) Like "[A-Za-z]" And Not StartFlag Then
    9.       StartFlag = True
    10.       strTempVal = "$" & Mid(ActiveCell.Formula, i, 1)
    11.     ElseIf StartFlag And Mid(ActiveCell.Formula, i, 1) Like "[1-9]" Then
    12.       strTempVal = strTempVal & "$" & Mid(ActiveCell.Formula, i, 1)
    13.       NewForm = NewForm & strTempVal
    14.     ElseIf StartFlag And Mid(ActiveCell.Formula, i, 1) Like "[!1-9]" Then
    15.       NewForm = NewForm & Mid(ActiveCell.Formula, i - 1, 1) & Mid(ActiveCell.Formula, i, 1)
    16.       StartFlag = False
    17.     Else
    18.       NewForm = NewForm & Mid(ActiveCell.Formula, i, 1)
    19.     End If
    20.   Next i
    21.   StartFlag = False
    22.   ActiveCell.Formula = NewForm
    23.   ActiveCell.Offset(1, 0).Select
    24. Loop
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: how to lock all cell references in formulas?

    You can try this add-in. It allows for a selected range of formulas to be changed to a ref of your choosing.

    Load it up, and it adds a menu item to you TOOLS menu
    Attached Files Attached Files
    Justin Labenne
    www.jlxl.net

Posting Permissions

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



Click Here to Expand Forum to Full Width