+ Reply to Thread
Results 1 to 11 of 11

HELP! Adding Rows with a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    01-25-2021
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2016
    Posts
    11

    HELP! Adding Rows with a macro

    I am still struggling with this....
    I have attached a spread sheet where I want to add the number "1" under "Issue No" and "1" under "Action Step" by clicking a button and subsequently if additional actions are needed then I would like to click the other button to add additional Action Steps as needed.

    Ok I have figured it out the above

    BUTTTT..... is there a way to auto format the cell that contains the issue if there are multiple actions.... ie. if there are 2 actions steps then the issue needs to be merged into 2 cell, if 3 action steps then merge issue into 3 cells.

    thanks
    Kelly
    Attached Files Attached Files
    Last edited by kkavanagh; 01-27-2021 at 01:31 PM.

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: HELP! Adding Rows with a macro

    Try the AddAction code as follows, but you should know, mixing Cell merge with VBA is not something one should do.

    Sub AddAction()
    Dim r, k As Long
    
    r = Selection.Row
    k = 0
    While Cells(r, 4).Value < Cells(r, 4).Offset(k + 1, 0)
        k = k + 1
    Wend
    Cells(r + k + 1, 2).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D" & r + k + 1).Value = Range("D" & r + k).Value + 1
    Range("E" & r + k + 1).Value = "Action Step " & Range("D" & r + k).Value + 1
    Range("F" & r + k + 1).Value = Date
    
    Range(Cells(r, 2), Cells(r + k + 1, 2)).Merge
    Range(Cells(r, 3), Cells(r + k + 1, 3)).Merge
    
    With Range(Cells(r + k + 1, 4), Cells(r + k + 1, 11)).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    
    With Range("F" & r + k + 1).Borders(xlEdgeLeft)
        .LineStyle = xlDouble
        .Weight = xlThick
    End With
    
    With Range("K" & r + k + 1).Borders(xlEdgeLeft)
        .LineStyle = xlDouble
        .Weight = xlThick
    End With
    End Sub
    Last edited by maniacb; 01-27-2021 at 03:56 PM. Reason: Fix Line width

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: HELP! Adding Rows with a macro

    Here is code updated for formatting

    Sub AddAction()
    Dim r, k As Long
    
    r = Selection.Row
    k = 0
    While Cells(r, 4).Value < Cells(r, 4).Offset(k + 1, 0)
        k = k + 1
    Wend
    Cells(r + k + 1, 2).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D" & r + k + 1).Value = Range("D" & r + k).Value + 1
    Range("E" & r + k + 1).Value = "Action Step " & Range("D" & r + k).Value + 1
    Range("F" & r + k + 1).Value = Date
    Range("D" & r + k + 1).Font.Bold = True
    
    Range("D" & r + k + 1).HorizontalAlignment = xlCenter
    
    Range(Cells(r, 2), Cells(r + k + 1, 2)).Merge
    Range(Cells(r, 3), Cells(r + k + 1, 3)).Merge
    Range(Cells(r, 2), Cells(r + k + 1, 2)).Font.Bold = True
    Range(Cells(r, 2), Cells(r + k + 1, 2)).VerticalAlignment = xlCenter
    Range(Cells(r, 3), Cells(r + k + 1, 3)).VerticalAlignment = xlCenter
    
    With Range("B" & r + k + 1 & ":K" & r + k + 1)
        With .Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .Weight = xlThick
        End With
        With .Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlMedium
        End With
    
        With .Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlMedium
        End With
        With .Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
    End With
    
    With Range(Cells(r + k + 1, 4), Cells(r + k + 1, 11)).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    
    With Range("F" & r + k + 1).Borders(xlEdgeLeft)
        .LineStyle = xlDouble
        .Weight = xlThick
    End With
    
    With Range("K" & r + k + 1).Borders(xlEdgeLeft)
        .LineStyle = xlDouble
        .Weight = xlThick
    End With
    
    With Range(Cells(r, 2), Cells(r + k + 1, 2)).Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    
    With Range(Cells(r, 2), Cells(r + k + 1, 2)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    End Sub
    
    
    Sub AddIssue()
    Dim lr, mx As Long
    
    lr = Cells(Rows.Count, 4).End(xlUp).Row
    'mx = Sheets("Action_Plan").Range("B" & lr).Value
    mx = WorksheetFunction.Max(Range("B1:B" & lr))
    'MsgBox lr & mx
    Range("B" & lr + 1).Value = mx + 1
    Range("C" & lr + 1).Value = "Issue " & mx + 1
    Range("D" & lr + 1).Value = 1
    Range("E" & lr + 1).Value = "Action Step 1"
    Range("F" & lr + 1).Value = Date
    
    Range("B" & lr + 1).Font.Bold = True
    Range("B" & lr + 1).HorizontalAlignment = xlCenter
    Range("B" & lr + 1).VerticalAlignment = xlBottom
    
    Range("D" & lr + 1).Font.Bold = True
    Range("D" & lr + 1).HorizontalAlignment = xlCenter
    
    With Range("B" & lr + 1 & ":K" & lr + 1)
    
        With Range("B" & lr + 1 & ":K" & lr + 1).Borders(xlEdgeTop)
            .LineStyle = xlDouble
            .Weight = xlThick
        End With
        With Range("B" & lr + 1 & ":K" & lr + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .Weight = xlThick
        End With
        With Range("B" & lr + 1 & ":K" & lr + 1).Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlMedium
        End With
    
        With Range("B" & lr + 1 & ":K" & lr + 1).Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlMedium
        End With
        With Range("B" & lr + 1 & ":K" & lr + 1).Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
    End With
    End Sub
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: HELP! Adding Rows with a macro

    Try this ...
    I use hidden 'tmp' sheet in VBA code.
    To add an 'Action Step' select a given line for a given 'Issue' and select the 'Add new action' button.
    Sub New_Issue()
        Dim sum_ As Integer
        
        lr = Cells(Rows.Count, "e").End(xlUp).Row
        Range("temp_row").Copy Cells(lr + 1, 2)
        sum_ = Application.Max(Range(Cells(lr, 2), Cells(lr - 5, 2))) + 1
        Cells(lr + 1, 2).Resize(, 5) = Array(sum_, "Issue " & sum_, 1, "Action Step 1", Date)
    End Sub
    
    Sub test_Action_Step()
        Dim rng As Range
        Dim rwsMrg As Integer, rw As Integer, lrw, acRow As Integer
        Dim adres As String
        
        Application.ScreenUpdating = False
        lr = Cells(Rows.Count, "e").End(xlUp).Row
        acRow = ActiveCell.Row
        If acRow < 8 Or acRow > lr Or ActiveCell.Column <> 5 Then
            MsgBox "Can't insert row in 'Action Required (Task)'"
            Exit Sub
        End If
        Set rng = ActiveCell.Offset(, -2)
        With rng
            rwsMrg = .MergeArea.Count
            adres = .MergeArea.Address
            rw = Range(adres).Row
            If acRow = lr And rwsMrg = 1 Then
                Range("temp_row").Copy rng.MergeArea.Offset(1, -1)          '<----'temp_row' - named range in the sheet 'tmp'.
                With .Offset(1, -1).Resize(, 10).Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
            Else
                .MergeArea.Offset(1).EntireRow.Insert Shift:=xlDown
                .MergeArea.Offset(1, -1).Borders.LineStyle = xlNone
                Range("temp_row").Copy rng.MergeArea.Offset(1, -1)
                With rng.Offset(1, -1).Resize(, 10).Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
                .UnMerge
            End If
        End With
        Range(adres).Resize(rwsMrg + 1).Merge
        Range(adres).Offset(, -1).Resize(rwsMrg + 1).Merge
        Cells(rw + rwsMrg, 4).Resize(, 3).Value = Array(rwsMrg + 1, "Action Step " & rwsMrg + 1, Date)
    End Sub
    Attached Files Attached Files
    Best Regards,
    Maras.

  5. #5
    Registered User
    Join Date
    01-25-2021
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: HELP! Adding Rows with a macro

    Hi Maras,

    Thank you! How do I remove the bright green colour on the date cell? I will add conditional formatting later...

    Thanks
    Kelly

  6. #6
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: HELP! Adding Rows with a macro

    You can like below.
    Select the cells that you will remove solid fill colors from, and click Home > Fill Color > No Fill.

  7. #7
    Registered User
    Join Date
    01-25-2021
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: HELP! Adding Rows with a macro

    But they come up every time I click the new issue button

  8. #8
    Registered User
    Join Date
    01-25-2021
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: HELP! Adding Rows with a macro

    That's perfect! Many thanks!

  9. #9
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: HELP! Adding Rows with a macro

    Glad to help.

+ 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. Macro Adding rows for you
    By Cbowman in forum Excel General
    Replies: 1
    Last Post: 11-10-2011, 09:51 AM
  2. Adding Conditional Rows in a Macro
    By succel in forum Excel General
    Replies: 10
    Last Post: 07-29-2010, 04:52 PM
  3. Adding multiple rows with macro
    By bruce71101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-14-2010, 03:29 AM
  4. Adding multiple rows with a Macro
    By Nienaber in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-25-2010, 06:52 PM
  5. Macro for adding rows after previous row?
    By xwishmasterx in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-17-2010, 07:28 PM
  6. Adding Rows and Calculation to Macro
    By waderless1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-08-2008, 06:34 PM
  7. Macro and Adding Rows
    By dreicer_Jarr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2008, 05:59 PM
  8. Problem with macro adding rows
    By bsmith27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2007, 08:06 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