Consulting

Results 1 to 3 of 3

Thread: Help with paste special

  1. #1

    Help with paste special

    Hi all,
    I am having problems with this bit of code, the paste bit could someone help please?
    I would like it to keep the original formatting, I have had it working without the formatting

    Sub CopyFilteredValuesToActiveWorkbook()
    
    Dim wbSource As Workbook, wbDest As Workbook
    Dim wsSource As Worksheet, wsDest As Worksheet
    Dim rngSource As Range, rngDest As Range
    
    Set wbSource = Workbooks.Open("C:\Users\peter\Desktop\excel test\test.xlsm", , True) 'Readonly = True
    
    Set wsSource = wbSource.Worksheets("Sheet1")
    wsSource.Range("A1:H3").AutoFilter Field:=2, Criteria1:="Line 6"
    
    Set rngSource = wsSource.Range("A1:K10")
    wsSource.Range("A1:f10").SpecialCells(xlCellTypeVisible).Copy
    
    Set wbDest = ThisWorkbook
    Set wsDest = wbDest.Worksheets("Sheet1")
    'Set rngDest = wsDest.Range("A1:h20")
    
    Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
            , SkipBlanks:=False, Transpose:=False
    
    wbSource.Close (False) 'Close without saving changes
    
    End Sub
    Last edited by SamT; 09-15-2017 at 06:03 AM. Reason: Aded Code Formatting Tags with # Icon. Added White Space to code.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    : Try this

    Sub CopyFilteredValuesToActiveWorkbook() 
         
        Dim wbSource As Workbook, wbDest As Workbook 
        Dim wsSource As Worksheet, wsDest As Worksheet 
         
           Set wbDest = ThisWorkbook 
           Set wbSource = Workbooks.Open("C:\Users\peter\Desktop\excel test\test.xlsm", , True) 'Readonly = True
         
        Set wsSource = wbSource.Worksheets("Sheet1") 
        wsSource.Range("A1:H3").AutoFilter Field:=2, Criteria1:="Line 6" 
         
        wsSource.Range("A1:f10").SpecialCells(xlCellTypeVisible).Copy 
         
        wbDest.Worksheets("Sheet1").Range("A1").PasteSpecial _
     Paste:=xlPasteAllUsingSourceTheme
         
        wbSource.Close (False) 'Close without saving changes
         
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Thanks so much that seems to have worked better.

Posting Permissions

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