+ Reply to Thread
Results 1 to 3 of 3

Adjusting code to check column, and insert formula

Hybrid View

  1. #1
    Registered User
    Join Date
    08-05-2009
    Location
    Winnipeg, Canada
    MS-Off Ver
    Excel 2003
    Posts
    61

    Adjusting code to check column, and insert formula

    Here is a snippet of code I have. What it is doing is looking at the row which was just pasted by the earlier code and clearing out everything that isn't a formula. The problem is in columns "N, P, R, T" the formula was "=if(isblank($L2),"",$L2)", which is just there to make data entry easier. If someone puts a date in L2, the sheet fills that into columns NPRT because the date will usually be the same for all those, and if it does change, it's only by a day, so it's easier to edit than punch in yyyy-mm-dd again.

    Problem being sometimes people do change it, which means the formula I have in row two is probably gone by row ten. So I think I need to add another check to the code below, to see if we're currently in a cell that is in one of my watched columns, and if so, paste in the formula that would be something like:

    =if(isblank($L_ourCurrentRow),"",$L_ourCurrentRow)

    I am requesting assistance modifying the code below to achieve this.

    For Each Cell In Range(Cells(Rows.Count, 2).End(xlUp).Offset(0, -1), _
        Cells(Rows.Count, 2).End(xlUp).Offset(0, 66))
         If Cell <> "" Then
            If Left(Cell.Formula, 1) <> "=" Then Cell.ClearContents
         End If     
      On Error Resume Next
    Next Cell
    This thread here is the closest thing I can find so far to what I'm trying to do, but my attempts to change the row check "r" to a column check aren't working.

  2. #2
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: Adjusting code to check column, and insert formula

    Try this out

    For Each Cell In Range(Cells(Rows.Count, 2).End(xlUp).Offset(0, -1), _
        Cells(Rows.Count, 2).End(xlUp).Offset(0, 66))
         If Cell <> "" Then
            If Cell.HasFormula Then Cell.ClearContents
         End If     
      On Error Resume Next
    Next Cell
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  3. #3
    Registered User
    Join Date
    08-05-2009
    Location
    Winnipeg, Canada
    MS-Off Ver
    Excel 2003
    Posts
    61

    Re: Adjusting code to check column, and insert formula

    Greetings contaminated,

    That code wouldn't do what I need, so I must have explained it badly. This is the formula in columns N,P,R, and T on row 2: "=if(isblank($L2),"",$L2". The cell isn't protected though, so a user can enter another value. When my code runs to copy the last row, paste it to the next available row, in this case it copies row two and pastes it into row three, it won't necessarily copy the formula itself, if the user has changed it. But I always want it to copy the formula, regardless of whether a user over-writes it.

    Here is the full code that is running, in case it helps:

    'This code executes any time you move the cursor
    'on the Records sheet.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    'Check to see if user has selected more than one row
    'If yes, stop processing and exit
    If Target.Rows.Count > 1 Then Exit Sub
    
    'Check to see if the row is empty, if not, stop
    'processing and exit
    If Application.CountA(Target.EntireRow) <> 0 Then Exit Sub
    
    'Check to see if the cell is in the third column
    'if not, stop processing and exit. Otherwise, continue
    If Target.Column = 3 Then
    
    'Disable features and unprotect worksheet
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    ActiveSheet.Unprotect Password"secret"
    
    'Copy the row above the first empty row, and paste
    'it into the next available empty row
    Range(Cells(Rows.Count, 2).End(xlUp).Offset(0, -1), _
        Cells(Rows.Count, 2).End(xlUp).Offset(0, 66)).Copy _
        Destination:=Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)
    
    'In the row you just pasted, clear the contents unless it
    'starts with an equal sign, because then it's a formula
    For Each Cell In Range(Cells(Rows.Count, 2).End(xlUp).Offset(0, -1), _
        Cells(Rows.Count, 2).End(xlUp).Offset(0, 66))
         If Cell <> "" Then
            If Left(Cell.Formula, 1) <> "=" Then Cell.ClearContents
         End If
         
         'HERE
         
      On Error Resume Next
    Next Cell
    
    'Insert the record creation date/time stamp
    Cells(Rows.Count, 2).End(xlUp).Offset(0, -1).Value = Format(Now, "yyyy-mm-dd HH:mm")
    
    'Re-enable features, and re-protect worksheet
    ActiveSheet.Protect Password:="secret"
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    End If
    
    'End of program
    End Sub

+ 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