+ Reply to Thread
Results 1 to 3 of 3

Copy and Paste Values as Number Format

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2014
    Location
    Houston, Texas
    MS-Off Ver
    10
    Posts
    17

    Copy and Paste Values as Number Format

    Hello,

    I am trying to write a Macro where it will use date and time (protected fields), and copy and paste values in number format (not general) for only fields that have a formula. I believe I am close, but I keep getting a debug error ~1004 on the third to last line (NumberFormat). Any help would be appreciated.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Application.EnableEvents = False
       If Not Intersect(Target, Range("A:A")) Is Nothing Then
          If Target.Offset(0, 1).Locked = False Then
             ActiveSheet.Unprotect "isitontime"
             Target.Offset(0, 1).Value = Now
             Target.Offset(0, 1).Locked = True
             Target.Offset(0, 0).Locked = True
             ActiveSheet.Protect "isitontime"
          End If
          Dim r1 As Range, r2 As Range
       Set r1 = Range("C1:C1000000")
       Set r2 = Range("D1:D1000000")
       ActiveSheet.Unprotect "isitontime"
       r1.Copy
       r2.PasteSpecial Paste:=xlPasteValues
       ActiveSheet.Protect "isitontime"
       End If
       Application.EnableEvents = True
       Range("D1:D1000000").NumberFormat = "0"
       Range("A" & Cells(Rows.Count, "A").End(xlUp).Row + 1).Select
    End Sub

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Copy and Paste Values as Number Format

    Hi Daniel,

    I think you're getting the error when the sheet is protected when you access the line:
     Range("D1:D1000000").NumberFormat = "0"
    There does not seem to be an error when the sheet is NOT protected when you access the line.

    I can't suggest a fix, because I'm not sure what you're trying to do.

    Lewis

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,970

    Re: Copy and Paste Values as Number Format

    You are trying to modify formats of locked cells in a protected sheet. Try moving the formatting up above the call to ActiveSheet.Protect.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. Copy paste only the format and formulas not values
    By sudharshan86 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-26-2014, 07:53 AM
  2. Paste values in a column, then apply custom number format
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2013, 05:17 AM
  3. Replies: 3
    Last Post: 02-21-2013, 12:48 AM
  4. [SOLVED] Copy and paste values and number formats pastes formatting
    By Canthandlemyhandle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-27-2012, 06:21 PM
  5. Replies: 0
    Last Post: 01-18-2011, 08:47 AM

Tags for this Thread

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