Consulting

Results 1 to 3 of 3

Thread: Macro That Inserts a New Row and Defines Formulas to Cells

  1. #1
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    1
    Location

    Macro That Inserts a New Row and Defines Formulas to Cells

    I have created this Macro that inserts a new raw and preserves all of the conditional formatting, however, when I try to define formulas to specific cells it stops working. Please let me know where I'm going wrong.

    Private Sub CommandButton1_Click()
    Sheets("Main").Range("A7").Select
    ActiveCell.EntireRow.Insert Shift:=xlDown
    Sheets("Main").Range("A7").Select
    ActiveCell.EntireRow.Interior.ColorIndex = 0
    Sheets("Main").Range("A7").Select
    ActiveCell.EntireRow.Font.Bold = False
    Sheets("Main").Range("A7:B7,D7:G7").HorizontalAlignment = xlLeft
    Sheets("Main").Range("H7").HorizontalAlignment = xlRight
    Sheets("Main").Range("A8:AW8").Select
    Selection.Copy
    Sheets("Main").Range("A7:AW7").Select
    Selection.PasteSpecial Paste:=xlPasteAllMergingConditionalFormats
    Application.CutCopyMode = False
    Sheets("Main").Range("A7:AW7").ClearContents
    Sheets("Main").Range("p7").Select
    ActiveCell.Formula = "=IFS(AND($Q7<>"",$R7<>""),"Proposal Sent",AND(Q7="",R7<>""),"Error",AND($Q7="",$R7=""),"", _
    AND($Q7>=TODAY()),SUM(TODAY()-$Q7),AND($Q7<TODAY()),SUM(TODAY()-$Q7))"
    Sheets("Main").Range("b7").Select
    ActiveCell.Formula = "=p7"
    End Sub
    Thanks.
    Last edited by Aussiebear; 01-06-2023 at 02:16 PM. Reason: Added code tags to supplied code

  2. #2
    you "double" all your Quotes:
    ActiveCell.Formula = "=IFS(AND($Q7<>"""",$R7<>""""),""Proposal Sent"", _
    AND(Q7="""",R7<>""""),""Error"",AND($Q7="""",$R7=""""),"""", _
    AND($Q7>=TODAY()),SUM(TODAY()-$Q7), _
    AND($Q7<TODAY()),SUM(TODAY()-$Q7))"

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And do get rid of that horrible selecting

    Private Sub CommandButton1_Click()
    
        With Sheets("Main")
        
            With .Range("A7")
            
                .EntireRow.Insert Shift:=xlDown
                .EntireRow.Interior.ColorIndex = 0
                .EntireRow.Font.Bold = False
            End With
            .Range("A7:B7,D7:G7").HorizontalAlignment = xlLeft
            .Range("H7").HorizontalAlignment = xlRight
            .Range("A8:AW8").Copy
            .Range("A7:AW7").PasteSpecial Paste:=xlPasteAllMergingConditionalFormats
            .Range("A7:AW7").ClearContents ""
            .Range("P7").Formula = "=IFS(AND($Q7<>"",$R7<>""""),""Proposal Sent""," & _
                                        "AND(Q7="""",R7<>""""),""Error""," & _
                                        "AND($Q7="""",$R7=""""),""""," & _
                                        "AND($Q7>=TODAY()),SUM(TODAY()-$Q7)," & _
                                        "AND($Q7<TODAY()),SUM(TODAY()-$Q7))"
            .Range("B7").Formula = "=P7"
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •