+ Reply to Thread
Results 1 to 5 of 5

Copy and Paste Number Formatting Only

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-30-2010
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2010
    Posts
    100

    Copy and Paste Number Formatting Only

    I've got this to work for copying a single cell and pasting over a selection of cells:

    Dim format As String
    
    Sub CopyNumberFormat()
    format = ActiveCell.NumberFormat
    End Sub
    
    Sub PasteNumberFormat()
    Selection.NumberFormat = format
    End Sub
    Then I tried changing
    format = ActiveCell.NumberFormat
    to
    format = Selection.NumberFormat
    and got an error code.

    Anybody know how to copy or store number formats over a selection?

    I'm trying to just copy the number formatting from a row to new rows below it.
    I don't need the values or colors and most the time there is data already in the rows below so I'd like to not have it messed with.

  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 Number Formatting Only

    Hi tricky,

    I'm trying to just copy the number formatting from a row to new rows below it.
    Try the following. It copies number formats and formulas. If any of your source rows have formulas you don't want to replicate, the formulas can be removed from the destination rows.
    Sub CopyRowNumberFormatForOneRow()
    
      Dim iSourceRow As Long
      
      'Get the Source Row
      iSourceRow = ActiveCell.Row
    
      'Copy the Entire Row
      Rows(iSourceRow).Copy
      
      'Paste the number formats to the next row
      With Rows(iSourceRow + 1)
         .PasteSpecial xlPasteFormulasAndNumberFormats
      End With
      
      
      'Clear the Clipboard buffer
      Application.CutCopyMode = False
    
    End Sub
    
    
    Sub CopyRowNumberFormatForSeveralRows(iRowCountToFormat As Long)
    
      Dim iDestinationStartRow As Long
      Dim iDestinationEndRow As Long
      Dim iSourceRow As Long
      Dim sRange As String
      
      'Get the Source Row
      iSourceRow = ActiveCell.Row
    
      'Copy the Entire Row
      Rows(iSourceRow).Copy
      
      'Determine the range of rows needed as a destination
      iDestinationStartRow = iSourceRow + 1
      iDestinationEndRow = iDestinationStartRow + iRowCountToFormat - 1
      sRange = iDestinationStartRow & ":" & iDestinationEndRow
      
      
      'Paste the number formats to the next row
      With Range(sRange)
         .PasteSpecial xlPasteFormulasAndNumberFormats
      End With
      
      
      'Clear the Clipboard buffer
      Application.CutCopyMode = False
    
    End Sub
    
    Sub TestCopyRowNumberFormatForSeveralRows()
    
      Call CopyRowNumberFormatForSeveralRows(3)
    
    End Sub
    Lewis

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy and Paste Number Formatting Only

    Maybe :

    Public arrNumberFormat() As String
    Public Sub CopyNumberFormat()
      Dim i As Long, j As Long
      If TypeName(selection) <> "Range" Then Exit Sub
      ReDim arrNumberFormat(1 To selection.Rows.Count, 1 To selection.Columns.Count)
      Application.ScreenUpdating = False
      For i = 1 To selection.Rows.Count
          For j = 1 To selection.Columns.Count
              arrNumberFormat(i, j) = selection.Cells(i, j).NumberFormat
          Next j
      Next i
      Application.ScreenUpdating = True
    End Sub
    Public Sub PasteNumberFormat()
      Dim i As Long, j As Long
      If TypeName(selection) <> "Range" Then Exit Sub
      Application.ScreenUpdating = False
      If UBound(arrNumberFormat, 1) = 1 Then
         For j = 1 To Application.Min(selection.Columns.Count, UBound(arrNumberFormat, 2))
             If Len(arrNumberFormat(1, j)) Then selection.Columns(j).Cells.NumberFormat = arrNumberFormat(1, j)
         Next j
      ElseIf UBound(arrNumberFormat, 2) = 1 Then
         For i = 1 To Application.Min(selection.Rows.Count, UBound(arrNumberFormat, 1))
             If Len(arrNumberFormat(i, 1)) Then selection.Rows(i).Cells.NumberFormat = arrNumberFormat(i, 1)
         Next i
      Else
         For i = 1 To Application.Min(selection.Rows.Count, UBound(arrNumberFormat, 1))
             For j = 1 To Application.Min(selection.Columns.Count, UBound(arrNumberFormat, 2))
                 If Len(arrNumberFormat(i, j)) Then selection.Cells(i, j).NumberFormat = arrNumberFormat(i, j)
             Next j
         Next i
      End If
      Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Contributor
    Join Date
    10-30-2010
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: Copy and Paste Number Formatting Only

    Thanks to both of you for your time. karedog your code seems to be working good. I think it will do.

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Copy and Paste Number Formatting Only

    You are welcome, and thanks for the reputation points.


    Regards

+ 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. [SOLVED] Number formatting when using an array instead of copy and paste(?)
    By wonderdunder in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2014, 12:28 PM
  2. Replies: 4
    Last Post: 08-26-2013, 05:31 PM
  3. [SOLVED] Copy Paste Formatting??
    By Jiptastic in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-17-2013, 01:48 PM
  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. [SOLVED] Copy & paste conditional formatting?
    By Ltat42a in forum Excel General
    Replies: 15
    Last Post: 10-02-2012, 01:55 AM
  6. Copy/paste formatting
    By BBoydAnchor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-05-2012, 04:34 PM
  7. Conditional Formatting Copy & Paste
    By adam2308 in forum Excel General
    Replies: 2
    Last Post: 06-07-2010, 06:03 PM
  8. Copy and Paste Formatting on Used Rows
    By mashley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2009, 01:33 PM

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