Results 1 to 4 of 4

Thread: Macro to Copy Values and fill colors.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Question Macro to Copy Values and fill colors.

    Having issues with this code. I have a spreadsheet template that I need to copy to a new sheet with just the values then copy the fill colors to this new sheet.
    If it is passible also name the new sheet the date.

    Sub CopyDataToNewSheet()
    ' Create a new worksheet.
        Dim newSheet As Worksheet
        Set newSheet = ThisWorkbook.Worksheets.Add
    ' Set the source range.
        Set sourceRange = activeSheet.UsedRange
    ' Set the destination range.
        Dim destinationRange As Range
        Set destinationRange = newSheet.Range("A1")
    ' Copy all data from the active worksheet to the new worksheet.
        activeSheet.UsedRange.Copy newSheet.Range("A1")
    ' Copy the values and fill colors from the source range to the destination range.
        destinationRange.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ' Copy the fill patterns from the source range to the destination range.
        For Each cell In destinationRange
            cell.Interior.Color = sourceRange(cell.Row, cell.Column).Interior.Color
            cell.Interior.Fill.Pattern = sourceRange(cell.Row, cell.Column).Interior.Fill.Pattern
        Next cell
    ' Rename the new worksheet.
        newSheet.Name = "New Data Sheet"
    End Sub
    Last edited by Aussiebear; 10-26-2023 at 12:52 PM. Reason: Added code tags to supplied code

Posting Permissions

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