+ Reply to Thread
Results 1 to 10 of 10

divide selected cell by 100

Hybrid View

  1. #1
    Registered User
    Join Date
    08-15-2008
    Location
    denver
    Posts
    68

    divide selected cell by 100

    I'm lost on VBA, but I have been successful at recording a few extremely simple macros. I have a number of pdf statements that I have run through OCR, and the OCR often misses the decimal point so that 43.85 comes out as 4385. (this is in dollars, so everything has 2 decimals).

    These cells are usually pretty easy to spot, so I want to select that cell, press a keystroke or 2, and divide the value in that cell by 100. I assumed that by selecting "relative cell references", and recording a macro with the keystrokes "<F2>/100<HOME>=" (without the quotes, of course), I would accomplish that. But instead, it places a constant in that cell, with the value obtained in the cell where I recorded the macro.

    Any help, please?
    Last edited by jmhultin; 12-17-2009 at 01:07 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: divide selected cell by 100

    Use the double click event
    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    ActiveCell.Value = ActiveCell.Value / 100
    End Sub
    Copy the code
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste
    Last edited by royUK; 12-16-2009 at 06:45 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: divide selected cell by 100

    Hello jmhultin,

    This macro will allow you to change 1 or more cells. The cells need not be contiguous. Copy this code into a VBA module. You can then assign a key combination to run the macro. Press ALT+F8 to bring up the Macro Dialog to assign the key combination to run the macro.
    Sub DivideBy100()
    
      Dim Cell As Range
      
        For Each Cell In Selection
          Cell = Cell / 100
        Next Cell
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: divide selected cell by 100

    You don't need to loop, select the cells then run this
    Option Explicit
    
    Sub x()
    Selection.Value = Selection.Value / 100
    End Sub
    Last edited by royUK; 12-17-2009 at 03:34 AM.

  5. #5
    Registered User
    Join Date
    08-15-2008
    Location
    denver
    Posts
    68

    Re: divide selected cell by 100

    I'm sure both solutions worked, but I used Leith's and it worked great. Thanks.

    In the future (because I'm not into coding), is there a was to do this by recording a macro?

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: divide selected cell by 100

    Roy, I'm not sure your code would work on multiple cells (and/or non contiguous selections), another approach avoiding Iteration on a cell by cell basis which would also account for mixed data types would be:

    Dim rngArea As Range
    For Each rngArea In Selection.Areas
        With rngArea
            .Value = Evaluate("IF(ISNUMBER(" & .Address & ")," & .Address & "/100,REPT(" & .Address & ",1))")
        End With
    Next rngArea

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: divide selected cell by 100

    You can record a macro, but you would need to edit the resulting code to be dynamic. The recorded code would only work exactly as you record it, i.e. not work on different ranges.

    Not: the second code that I posted will do exactly what Leith's does, but without the loop and therefore potentially faster.
    Last edited by royUK; 12-17-2009 at 03:36 AM.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: divide selected cell by 100

    It worked when I tested it on natches of single cells which is what I thought the OP had. It will fail if the selection contains blocks of cells

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: divide selected cell by 100

    Would it not simply apply the first result to all single cells within the non-contiguous selection though ? (regardless of their own values should they differ from the first)

  10. #10
    Registered User
    Join Date
    08-15-2008
    Location
    denver
    Posts
    68

    Re: divide selected cell by 100

    Thanks - appreaciate all your input

+ 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