+ Reply to Thread
Results 1 to 3 of 3

Selecting a non-specific range and inserting formula

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2007
    Posts
    30

    Selecting a non-specific range and inserting formula

    Hi everyone,
    I've created a macro that adds a new column to a worksheet, and then puts a title of "Minutes" on the top of the column. The column to the left of the minutes column is in HH:MM:SS time format, and the minutes column uses a formula (which works perfectly) to put the time format into minutes. The code for this is below:

    Sub Analysis2()
    '
    ' Analysis2 Macro
    ' Macro recorded 05/12/2007 by pwilson to convert duration into minutes
    '
    
    '
        Columns("H:H").Select
        Selection.Insert Shift:=xlToRight
        Selection.NumberFormat = "#,##0.00"
        Range("H1").Select
        ActiveCell.FormulaR1C1 = "Minutes"
        Range("G2").Select
        Selection.End(xlDown).Select
        Range("H25854").Select
        ActiveCell.FormulaR1C1 = _
            "=(HOUR(RC[-1])*60)+(MINUTE(RC[-1]))+(SECOND(RC[-1])/60)"
        Range("H25854").Select
        Selection.Copy
        Range("H25853").Select
        Range(Selection, Selection.End(xlUp)).Select
        Range("H2:H25853").Select
        Range("H25853").Activate
        ActiveSheet.Paste
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End Sub
    The problem with the code is that when I recorded the macro I used control+down to select the range that I needed. However, Excel took this to be that I was specifically refering to cell H25853. This cell was the last row to contain data (which is what happens when you navigate using ctrl+down).

    What I need is the formula to be inserted into every cell in the H column that has data in the adjacent G column. For instance, if the data goes to only 10 rows, then I want the formula applied to only those rows. If it goes to 64000 rows, the formula needs to be applied to all of these rows.
    Can anyone help?
    Last edited by NBVC; 12-10-2007 at 12:30 PM.

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

    Here is the amended macro code...
    Sub Analysis2()
    '
    ' Analysis2 Macro
    ' Macro recorded 05/12/2007 by pwilson to convert duration into minutes
    '
      Dim Cell As Range
      Dim LastRow As Long
      Dim Rng As Range
    
        LastRow = Cells(Rows.Count, "G").End(xlUp).Row
        Set Rng = Range("G2", Cells(LastRow, "G"))
    
        Range("H:H").NumberFormat = "#,##0.00"
        Range("H1").Value = "Minutes"
    
          For Each Cell In Rng
            If Cell <> "" Then Cell.Offset(0, 1).Formula = "=(HOUR(RC[-1])*60)+(MINUTE(RC[-1]))+(SECOND(RC[-1])/60)"
          Next Cell
    
    End Sub
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 12-10-2007 at 03:40 PM. Reason: Left out some code for column "H"

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Hers another version

    Sub Analysis2()
    '
    ' Analysis2 Macro
    ' Macro recorded 05/12/2007 by pwilson to convert duration into minutes
    ' Modified by mudraker at excelforum 11/12/2007
    ' in thread http://www.excelforum.com/showthread.php?t=626353
    '
       Dim lLR As Long
       Columns("H:H").Insert Shift:=xlToRight
       Columns("H:H").NumberFormat = "#,##0.00"
       Range("H1").Value = "Minutes"
       lLR = Cells(Rows.Count, "g").End(xlUp).Row
       Range("h2:h" & lLR).FormulaR1C1 = _
          "=(HOUR(RC[-1])*60)+(MINUTE(RC[-1]))+(SECOND(RC[-1])/60)"
    End Sub
    Last edited by mudraker; 12-10-2007 at 03:45 PM.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ 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