+ Reply to Thread
Results 1 to 10 of 10

Macro problem for absolute reference

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-29-2011
    Location
    South Korea
    MS-Off Ver
    Excel 2019
    Posts
    178

    Macro problem for absolute reference

    Dear big brother and sister,

    I have a tricky task. I hope you can give me reply. appreciate your help.

    I have the following macro. However, sometimes, it doesn't work, it may be because it doesn't applied on specific sheet. May I ask how can I modify the following macro to let it run on specific sheet only.

    Secondly, as you go to sheet "before macro", after I use macro for Sub Absolute() (all cells selected are absolute reference", it returns to VALUE. May I know why?

    Thanks




    
    Sub Absolute()
    Dim cell As Range
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next
    End Sub
    
    Sub AbsoluteRow()
    Dim cell As Range
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsRowRelColumn)
    End If
    Next
    End Sub
    
    Sub AbsoluteCol()
    Dim cell As Range
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlRelRowAbsColumn)
    End If
    Next
    End Sub
    
    Sub Relative()
    Dim cell As Range
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlRelative)
    End If
    Next
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Macro problem for absolute reference

    Hi,

    You could use
    Sub Absolute()
    Dim cell As Range
     
    With Sheets(1) 'the number being the sheet you want to run code on
     
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next
    end with
     
    End Sub
    Sub Absolute()
    Dim cell As Range
     
    'Sheets(1).Select or Sheets(1).Activate The number being the sheet you want to run the code on.
     
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  3. #3
    Forum Contributor
    Join Date
    04-29-2011
    Location
    South Korea
    MS-Off Ver
    Excel 2019
    Posts
    178

    Re: Macro problem for absolute reference

    Thanks for your reply.

    After i put the following code, it still doesn't work.
    On the sheet "before macro", it still turn to value# (cell A 1to B7)
    By the way, should i change sheets(1) of the macro to sheet(before macro)?

    With Sheets(before macro) 'the number being the sheet you want to run code on
    instead of
    With Sheets(1) 'the number being the sheet you want to run code on


    Sub Absolute()
    Dim cell As Range
     
    With Sheets(1) 'the number being the sheet you want to run code on
     
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next
    end with
     
    End Sub

    Moreover, the 2nd macro you write may be incomplete. what is the purpose of macro?

    Sub Absolute()
    Dim cell As Range
     
    'Sheets(1).Select or Sheets(1).Activate The number being the sheet you want to run the code on.
     
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next

  4. #4
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Macro problem for absolute reference

    Hi,

    I was only just giving you examples to try not complete scripts you only need to try one of the ways to see if it works. After the Dim statement try one of these methods:

    Sheets(1).Select 
     
    OR 
     
    Sheets("THE NAME OF THE SHEET YOU WANT THE CODE TO RUN ON").Select
     
    Or
     
    Sheets(1).Activate
     
    or
     
    With Sheets("THE NAME OF THE SHEET YOU WANT THE CODE TO RUN ON")
     
    And place End With After the current End With in the script.
    .

    Also
    Sheets(before macro)
    .
    Should be like this:

    Sheets("before macro")
    The number I was talking about is the count of the worksheets you want to run your code on. For example you open a new workbook you have 3 sheet tabs with the names sheet1 ,sheet2 and sheet3 Say you want to run code on sheet2 you could use sheets("sheet2") by the name of the worksheet or sheets(2) by the index number placement of the worksheet.

    You dont really need to activate or select a worksheet to make a script work but it may help you to see what your script is doing when you step through it.

    Hope that helps some.

  5. #5
    Forum Contributor
    Join Date
    04-29-2011
    Location
    South Korea
    MS-Off Ver
    Excel 2019
    Posts
    178

    Re: Macro problem for absolute reference

    I tried to use the following macro, its work.
    However, A1 to B7 still return to value# after I use macro (on sheet "before macro")

    I believe I may need to modify the following macro....

    Sub Absolute()
    Dim cell As Range
     
    With Sheets("before macro")
     
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next
    End With
     
    End Sub

  6. #6
    Forum Contributor
    Join Date
    04-29-2011
    Location
    South Korea
    MS-Off Ver
    Excel 2019
    Posts
    178

    Re: Macro problem for absolute reference

    Dear big brother and sister,

    Can anyone help me? It seems the formula is right, but I don't know why the result will return to VALUE#. Before using macro, the answer is right. I would like to have macro for absolute reference

    I search in google for two hours.

  7. #7
    Forum Contributor
    Join Date
    04-29-2011
    Location
    South Korea
    MS-Off Ver
    Excel 2019
    Posts
    178

    Re: Macro problem for absolute reference

    Dear all,

    After I search the macro for absolute reference in search engine for several days, I still can't get the answer why the macro for the following doesn't work

    Sub Absolute()
    Dim cell As Range
     
    With Sheets("before macro")
     
    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula _
    (cell.Formula, xlA1, xlA1, xlAbsolute)
    End If
    Next
    End With
     
    End Sub
    I tried several times, the macro still doesn't work. I appreciate your reply.
    Attached Files Attached Files
    Last edited by ronlau123; 07-15-2011 at 12:39 AM.

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro problem for absolute reference

    hi, ronlau, please check attachment, select cell or cells with formula and run code "Absolute"
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    04-29-2011
    Location
    South Korea
    MS-Off Ver
    Excel 2019
    Posts
    178

    (solved) Re: Macro problem for absolute reference

    Dear Watersev,

    Thanks thanks for your reply. It is so awesome. I don't think I can get better answer from forum.

    Sub Absolute()
    Dim cell As Range
    With Application: .ScreenUpdating = 0: .Calculation = xlManual
    For Each cell In Selection
        If cell.HasFormula Then cell = Application.ConvertFormula(cell.Formula, xlA1, , xlAbsolute)
    Next: .ScreenUpdating = 1: .Calculation = xlCalculationAutomatic: End With: End Sub
    May I know which vba knowledge used in this macro (looping)? I tried to search in the web site, still can't get the solution. It seems that you develop the macro by yourself.
    Last edited by ronlau123; 07-18-2011 at 01:23 AM. Reason: solved

  10. #10
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro problem for absolute reference

    hi, I'm glad it helped though the code was slightly changed from the your's one.

    The code uses:

    - For Each...Next Statement
    - ConvertFormula method

    See VB help file for details.

    If you are happy with the offered help, please mark the thread as solved

+ Reply to Thread

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