Consulting

Results 1 to 5 of 5

Thread: Paste column format only through iteration

  1. #1

    Paste column format only through iteration

    Hi, I have a code that copies data from one workbook to another. Now, the 2nd column in my destination workbook has a certain color format that I need to be applied till the last column with data in it.

    This is my code snippet to find the last non-empty column :

    Dim rLastCell As Range
    Set ws = ThisWorkbook.Sheets(DestName)

    Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)

    Dim LastCol As Long

    LastCol = rLastCell.Column

    MsgBox LastCol

    So, now I want to iterate and paste the format from the 2nd column to all the columns until LastCol.

    Any help would be appreciated. Thank you

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
        Dim x As Long
        Columns(2).Copy
        For x = 3 To LastCol
            Columns(x).PasteSpecial Paste:=xlPasteFormats
        Next
    Semper in excretia sumus; solum profundum variat.

  3. #3
    Thanks! It works Perfectly I had figured out an indirect method but this is so much better.

    This was my method :
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    MsgBox "Last Column: " & lCol

    Dim CopyRng As Range
    Set CopyRng = Wb.Sheets(DestName).Range("B1:B57")
    CopyRng.Copy

    For j = 2 To lCol

    Wb.Sheets(DestName).Cells(1, j).PasteSpecial xlPasteFormats

    Next j

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    With ThisWorkbook.Sheets(DestName)
        LastRow = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        LastCol = .Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        .Range(.Cells(1, 2), .Cells(LastRow, 2)).Copy
        .Range(.Cells(1, 3), .Cells(LastRow, LastCol)).PasteSpecial xlPasteFormats
    End With
    Application.CutCopyMode = False
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Hi mancubus Thank you for answering my query. Even though this solution works, it is a slightly lengthier solution than the one paulked gave.

Tags for this Thread

Posting Permissions

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