+ Reply to Thread
Results 1 to 5 of 5

Disallow macro to function within range of rows

Hybrid View

  1. #1
    Registered User
    Join Date
    09-15-2022
    Location
    Georgia
    MS-Off Ver
    365
    Posts
    72

    Disallow macro to function within range of rows

    Hello Experts,

    by using below code, i am able to duplicate a selected row and copy it below by removing some of the data in defined areas,

    Sub insert_new_item ()
    
    ActiveCell.EntireRow.Copy
    ActiveCell.Offset(1, 0).EntireRow.Insert
    Application.CutCopyMode = False
    
    Dim r As Long: r = ActiveCell.Offset(1, 0).Row
    Cells(r, "I").Resize(, 6).Value = ""
    Cells(r, "Q").Formula = _
        "=IF(OR(P" & r & "="""",P" & r & "=""Insert Date""),"""",IF(P" & r & ">M" & r & ",""Define reason for delay"",""""))"
    
    End Sub
    Is it possible to revise the code and do not allow to copy/work/function within range of 1 to 5? as my headers and macro buttons are located within this range.

    lets say, user selects row 4 and when click's on the macro button, error message would popup by telling the user to select table range etc... and macro would not do anything.

    appreciate your valued support.

    Thanks,
    N

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,082

    Re: Disallow macro to function within range of rows

    How about.

    Sub insert_new_item()
    
    If ActiveCell.Row <= 5 Then MsgBox "Please select a cell on a valid row.", vbCritical: Exit Sub
    
    ActiveCell.EntireRow.Copy
    ActiveCell.Offset(1, 0).EntireRow.Insert
    Application.CutCopyMode = False
    
    Dim r As Long: r = ActiveCell.Offset(1, 0).Row
    Cells(r, "I").Resize(, 6).Value = ""
    Cells(r, "Q").Formula = _
        "=IF(OR(P" & r & "="""",P" & r & "=""Insert Date""),"""",IF(P" & r & ">M" & r & ",""Define reason for delay"",""""))"
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-15-2022
    Location
    Georgia
    MS-Off Ver
    365
    Posts
    72

    Re: Disallow macro to function within range of rows

    Hi ByteMarks, that was a nice solution! worked like a charm.

    Would also be possible to let it do the same within specific rows of the worksheet?

    such as, my other subheaders are located in below rows, their location on row is not fixed, due to this macro adding new items for tasks and headers are shifting down and down.

    So, the macro would stop again when it finds a header which is located in Column B, header=Management, these various subheaders with different text do also appear on below rows.

    Thanks,
    N

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,082

    Re: Disallow macro to function within range of rows

    Thanks for the feedback.

    I guess we'd need some way to test if column B was a header.

    Maybe if we can assume that there'll be no formula in column Q if it's a header row?

    Sub insert_new_item()
    
    If ActiveCell.Row <= 5 Or Range("Q" & ActiveCell.Row).HasFormula Then MsgBox "Please select a cell on a valid row.", vbCritical: Exit Sub
    
    ActiveCell.EntireRow.Copy
    ActiveCell.Offset(1, 0).EntireRow.Insert
    Application.CutCopyMode = False
    
    Dim r As Long: r = ActiveCell.Offset(1, 0).Row
    Cells(r, "I").Resize(, 6).Value = ""
    Cells(r, "Q").Formula = _
        "=IF(OR(P" & r & "="""",P" & r & "=""Insert Date""),"""",IF(P" & r & ">M" & r & ",""Define reason for delay"",""""))"
    
    End Sub

  5. #5
    Registered User
    Join Date
    09-15-2022
    Location
    Georgia
    MS-Off Ver
    365
    Posts
    72

    Re: Disallow macro to function within range of rows

    Hi ByteMarks,

    my apologies for the late reply, was traveling and just got back.

    the solution provided has worked, as i just created a dummy hidden formula at the other column and all went well after the test.

    Thanks so much!

+ 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. Building a Macro that will allow or disallow a cell from responding to a search function
    By Dieforasandwich in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2021, 06:37 PM
  2. [SOLVED] Data validation in cell D1 to disallow data already enterd in Range J3:J20000
    By go3go3go in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2019, 03:19 PM
  3. Function inside Macro that move a range rows if cells are used
    By oguilamo in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 08-29-2012, 11:51 PM
  4. Macro to delete row within a specified range or disallow if out of range
    By Mayweed in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-07-2011, 01:50 PM
  5. [SOLVED] [SOLVED] Disallow paste in range of cell containing validation
    By al007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2005, 03:55 AM
  6. Search and Disallow macro in Excel
    By ChristalClarity in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-02-2005, 12:05 AM
  7. how to create macro in excel to disallow imput of numbers
    By yummy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2005, 09:34 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