+ Reply to Thread
Results 1 to 9 of 9

Macro For Dividing a Named Range by 10

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2010
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010
    Posts
    85

    Macro For Dividing a Named Range by 10

    Hi All

    I am hoping that someone would help me with some code.

    I have a named range called TempData and the reference used is =OFFSET(TestData!$C$2,0,0,COUNT(TestData!$C:$C),12).

    I would like to use a macro to divide each individual cell within this range by 10, but only the once. Then the macro will recognise the operation has been completed and will not calculate again.

    Many thanks



    Diane

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Macro For Dividing a Named Range by 10

    Hi Diane,

    You may be able to do what you want manually in Excel:
    a. Put 10 in an empty cell that is not in your 'TestData' range.
    b. Copy that cell (either CTRL C or Edit > Copy).
    c. Select the range you want to divide by 10 (Highlight the range with the mouse).
    d. Edit > PasteSpecial. Select 'Values' and 'Divide' (both selected at the same time), then click on 'OK'.
    e. Manual range division is complete.

    I have a named range called TempData and the reference used is =OFFSET(TestData!$C$2,0,0,COUNT(TestData!$C:$C),12).
    I had problems with 'COUNT(TestData!$C:$C)'. It always returned a value of 0 for me. I assumed you wanted all the rows in the range that are in Column C.

    I would like to use a macro to divide each individual cell within this range by 10, but only the once. Then the macro will recognise the operation has been completed and will not calculate again.
    There are two Macros that follow:
    a. The first divides all non-zero cells in the entire 'TestData' range by 10.
    b. The second divides all non-zero cells in the 'TestData' range by 10 starting at cell 'C2' for all rows in the range (row 2 thru n) and for exactly 12 columns.

    In order to do this only once I had to borrow a cell (I arbitrarily selected 'A24') in your worksheet to act as a flag to indicate whether or not the division had taken place.

    There are instructions in the Macros on what to modify if you want to:
    a. Change the flag sentinel cell.
    b. Hide the flag sentinel cell.
    c. Change the number of columns or starting column.
    d. Change the number of rows or starting row.

    Lewis
    Option Explicit
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'The following item defines where the 'Sentinel Cell' is to go.
    'When the cell is BLANK, then division is allowed.
    'After division is performed, the Macro puts a value in the cell that prevents future division.
    '
    'You can make this cell hidden by using custom format of three semi-colons (;;;)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Const sDivisionAllowedSentinel_CELL = "A24"
    
    Sub DivideContentsOfEntireNamedRangeByTenOnlyOnce()
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'This divides the Entire Contents of range 'TestData' by 10
      'The sentinel cell defined in the beginning allows division to occur only once
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
    
      Dim r As Variant    'r is each cell in the range
      Dim myRange As Range
      
      Dim sSentinelValue As String
      
      'All the calculations are to be done on the Active Sheet
      ActiveSheet.Select
      
      'Get the value from the Sentinel Cell (Blank means Ok to divide/ anything else means DO NOT DIVIDE)
      sSentinelValue = Trim(Range(sDivisionAllowedSentinel_CELL).Text)
      If Len(sSentinelValue) > 0 Then
        MsgBox "TERMINATING.  Division Not Allowed." & vbCrLf & _
               "Division already occurred according to the value in Cell '" & sDivisionAllowedSentinel_CELL & "'."
        Exit Sub
      End If
      
      'Assign the range object
      Set myRange = Range("TestData")
      
      
      'Loop thru the range
      'Divide all non-zero values by 10
      For Each r In myRange
        'Debug.Print r.Address(False, False)
        If r.Value <> 0 Then
          r.Value = r.Value / 10#
        End If
      Next r
      
      'Set the Sentinel Cell
      Range(sDivisionAllowedSentinel_CELL).Value = "Divided by 10 on " & Now()
      
      'Clear the object
      Set myRange = Nothing
    
    End Sub
    
    
    Sub DivideContentsOfPartOfNamedRangeByTenOnlyOnce()
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'This divides Part of the Contents of range 'TestData' by 10
      'The sentinel cell defined in the beginning allows division to occur only once
      '
      'The goal is to replace the following Excel function:
      '=OFFSET(TestData!$C$2,0,0,COUNT(TestData!$C:$C),12).
      '
      'The division range starts at cell C2 (row 2, column 3).
      'To change the start location change the following two lines as required.
      Const iFirstColumnInCalculation = 3      'Column C
      Const iFirstRowinCalculation = 2         'Row 2
      '
      '
      'The division is supposed to be for 'COUNT(TestData!$C:$C)' rows.
      'This function always returned 0 for me.
      'This function uses all the rows in the range starting at the first row defined above.
      'To change the number of rows the calculation of the variable 'iRowCount' MUST BE MODIFIED.
      '
      'The division is supposed to be for 12 columns.
      'To change the NUMBER OF COLUMNS change the following line.
      Const iColumnCount = 12
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
      Dim r As Variant    'r is each cell in the range
      Dim myRange As Range
      
      Dim iRowCount As Integer
      Dim iFirstColumnInRange As Integer
      Dim iFirstRowInRange As Integer
      Dim iLastColumnInCalculation  As Integer
      Dim iLastColumnInRange As Integer
      Dim iLastRowInCalculation  As Integer
      Dim iLastRowInRange As Integer
      Dim iRowsInRange As Integer
      Dim iColumnsInRange As Integer
      
      
      Dim sSentinelValue As String
      
      'All the calculations are to be done on the Active Sheet
      ActiveSheet.Select
      
      'Get the value from the Sentinel Cell (Blank means Ok to divide/ anything else means DO NOT DIVIDE)
      sSentinelValue = Trim(Range(sDivisionAllowedSentinel_CELL).Text)
      If Len(sSentinelValue) > 0 Then
        MsgBox "TERMINATING.  Division Not Allowed." & vbCrLf & _
               "Division already occurred according to the value in Cell '" & sDivisionAllowedSentinel_CELL & "'."
        Exit Sub
      End If
      
      'Assign the range object
      'Find the starting row and column in the range
      'Find the ending row and column in the range
      Set myRange = Range("TestData")
      iFirstRowInRange = myRange.Row
      iFirstColumnInRange = myRange.Column
      
      iRowsInRange = myRange.Rows.Count
      iColumnsInRange = myRange.Columns.Count
    
      iLastRowInRange = iFirstRowInRange + iRowsInRange - 1
      iLastColumnInRange = iFirstColumnInRange + iColumnsInRange - 1
       
       
       
      
      
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Find the number of rows in the range
      'The number of columns is defined at the beginning of the function
      '
      'The number of rows calculated is determined by variable 'iRowCount'
      'The number of columns calculated is determined by variable 'iColumnCount'
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      iRowCount = iRowsInRange
        
        
        
        
        
        
      'Redefine the range to start at 'C2' (row 2, column 3)
      'and go on for the number of rows and columns
      'If the number of rows and columns is too many - can't go outside the range
      iLastRowInCalculation = iFirstRowinCalculation + iRowCount - 1
      iLastColumnInCalculation = iFirstColumnInCalculation + iColumnCount - 1
      
      If iLastRowInCalculation > iLastRowInRange Then
        iLastRowInCalculation = iLastRowInRange
      End If
      
      If iLastColumnInCalculation > iLastColumnInRange Then
        iLastColumnInCalculation = iLastColumnInRange
      End If
      
      
      'Set the redefined range
      Set myRange = _
        Range(Cells(iFirstRowinCalculation, iFirstColumnInCalculation), _
        Cells(iLastRowInCalculation, iLastColumnInCalculation))
      
      'Loop thru the range
      'Divide all non-zero values by 10
      For Each r In myRange
        'Debug.Print r.Address(False, False)
        If r.Value <> 0 Then
          r.Value = r.Value / 10#
        End If
      Next r
      
      'Set the Sentinel Cell
      Range(sDivisionAllowedSentinel_CELL).Value = "Divided by 10 on " & Now()
      
      'Clear the object
      Set myRange = Nothing
    
    End Sub
    Last edited by LJMetzger; 01-23-2014 at 09:30 AM. Reason: Deleted bad line in DivideContentsOfPartOfNamedRangeByTenOnlyOnce() - 'dim iColumnCount as Integer' (duplicate declaration)

  3. #3
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Macro For Dividing a Named Range by 10

    Quote Originally Posted by LJMetzger View Post
    I had problems with 'COUNT(TestData!$C:$C)'. It always returned a value of 0 for me.
    Perhaps you only had text in column C? COUNT will only count numbers (COUNTA will count both).
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  4. #4
    Registered User
    Join Date
    04-19-2010
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Macro For Dividing a Named Range by 10

    Morning Lewis

    I seem to be having an error message with the first code...... Compile Error: Duplicate Declaration in Current Scope (highlighted Dim iColumnCount As Integer)

    Thanks


    Diane

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Macro For Dividing a Named Range by 10

    Sorry, when I was adding comments, I did not test the program again.

    Please delete the following line or copy the code again (I deleted the bad line in post #2).
     Dim iColumnCount As Integer
    Lewis

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Macro For Dividing a Named Range by 10

    Hi Izandol,

    Thanks for taking a look at my problem.

    It was my lack of formula knowledge. The User had a range named 'TestData' and I probably assumed wrongly that the 'TestData' in the formula:
    COUNT(TestData!$C:$C)'.
    referred to the 'Range'. It is probably a SHEET REFERENCE, and my Sheet has a different name. When I removed the 'TestData' from the formula, the COUNT function worked, as all the cells were numeric.

    Lewis
    Last edited by LJMetzger; 01-23-2014 at 12:25 PM. Reason: Changed 'File Reference' to 'Sheet Reference'

  7. #7
    Registered User
    Join Date
    04-19-2010
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Macro For Dividing a Named Range by 10

    Temperature Graph Draft.xlsm

    Hi Lewis,

    I am having trouble implementing the code. I have attached a shortened version of the workbook, and appologise in advance if I have been doing something wrong.

    Kindest regards always


    Di

  8. #8
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Macro For Dividing a Named Range by 10

    Sorry again Diane,

    It is all my fault. Lewis can't read. He got confused between your Sheet 'TestData' and your range 'TempData'. Try the following Macro, which I tested on your sample workbook. In the future, it's always easier for the solvers, if you provide a sample workbook, so they can test what they're doing on your data.

    Sorry again.

    Lewis

    Option Explicit
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'The following item defines where the 'Sentinel Cell' is to go.
    'When the cell is BLANK, then division is allowed.
    'After division is performed, the Macro puts a value in the cell that prevents future division.
    '
    'You can make this cell hidden by using custom format of three semi-colons (;;;)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Const sSheetName = "TestData"
    Const sDivisionAllowedSentinel_CELL = "A24"
    
    Sub DivideContentsOfEntireNamedRangeByTenOnlyOnce()
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'This divides the Entire Contents of range 'TempData' by 10
      'The sentinel cell defined in the beginning allows division to occur only once
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
    
      Dim r As Variant    'r is each cell in the range
      Dim myRange As Range
      
      Dim sSentinelValue As String
      
      'All the calculations are to be done on the "TestData" Sheet
      Sheets("TestData").Select
      
      'Get the value from the Sentinel Cell (Blank means Ok to divide/ anything else means DO NOT DIVIDE)
      sSentinelValue = Trim(Range(sDivisionAllowedSentinel_CELL).Text)
      If Len(sSentinelValue) > 0 Then
        MsgBox "TERMINATING.  Division Not Allowed." & vbCrLf & _
               "Division already occurred according to the value in Cell '" & sDivisionAllowedSentinel_CELL & "'."
        Exit Sub
      End If
      
      'Assign the range object
      Set myRange = Range("TempData")
      
      
      'Loop thru the range
      'Divide all non-zero values by 10
      For Each r In myRange
        'Debug.Print r.Address(False, False)
        If r.Value <> 0 Then
          r.Value = r.Value / 10#
        End If
      Next r
      
      'Set the Sentinel Cell
      Range(sDivisionAllowedSentinel_CELL).Value = "Divided by 10 on " & Now()
      
      'Clear the object
      Set myRange = Nothing
    
    End Sub

  9. #9
    Registered User
    Join Date
    04-19-2010
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Macro For Dividing a Named Range by 10

    Hi Lewis

    Thats perfect......

    My fault I should have explained it a little better....I will add a sample in future, but hopefully this little project will be finished.

    Thanks

    Di

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. excel macro range define to another sheet / named range
    By koi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-21-2013, 10:25 AM
  2. [SOLVED] Determine what Named Range the Target Address is and return Named Range Name
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-18-2012, 10:49 PM
  3. Replies: 3
    Last Post: 06-04-2011, 10:56 AM
  4. Macro code to change cell range to named range
    By Orlic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2008, 11:51 AM
  5. Update range:How do I edit a Named Range using macro's
    By Tom Ogilvy in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-06-2005, 06:05 PM

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