+ Reply to Thread
Results 1 to 12 of 12

Copy Paste Macro with Values & Skipping Blanks

Hybrid View

  1. #1
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Copy Paste Macro with Values & Skipping Blanks

    Hi all

    I have just joined here having recently discovered the potential of macros. I came across this thread in my initial search: http://www.excelforum.com/excel-prog...ro-values.html

    The code posted by Leith Ross worked perfectly except I would like it to only paste cell values as opposed to the formulas in them. Is this possible or am I looking at building a new macro?

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Paste Macro with Values & Skipping Blanks

    There is no code there from Leith Ross. Did you post the link to the right thread? Or did you mean the code by jeffreybrown?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Copy Paste Macro with Values & Skipping Blanks

    Yes I meant jefferybrown. Sorry was reading another thread with code from Leith Ross.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Paste Macro with Values & Skipping Blanks

    This is the code from that thread
    Sub MoveNumbers()
        Dim i As Long
        Dim j As Long
        j = 12
        For i = 5 To 22
            If Cells(i, 38).Value > 0 Then
                Cells(j, 1).Value = Cells(i, 38).Value
                j = j + 1
            End If
        Next i
    End Sub
    It is already pasting the values. See this line
      Cells(j, 1).Value = Cells(i, 38).Value

  5. #5
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Copy Paste Macro with Values & Skipping Blanks

    Oh god I'm an idiot... I did have the wrong thread. The code is here: http://www.excelforum.com/excel-prog...ing-macro.html by Leith Ross.

    Sorry for the wild goose chase, this is doing my head in. The code by jefferybrown isn't suitable for what I'm trying to achieve. I tried building a code from scratch but found myself over complicating it and running into issues. The code by Leith works, but I can't seem to adapt it to paste values only.

    Thanks for your attention so far mate, any ideas?

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Paste Macro with Values & Skipping Blanks

    Try this
    Sub CopyAmdPaste()
    
      Dim C As Long
      Dim Col As Object
      Dim DstRng As Range
      Dim R As Long
      Dim RngRow As Range
      Dim SrcRng As Range
      
        Set SrcRng = Worksheets("Sheet1").Range("New_Range2")
        Set DstRng = Worksheets("Sheet2").Range("A1")
        
        Application.ScreenUpdating = False
        
         'Copy all non empty rows to the destination range
          For Each RngRow In SrcRng.Rows
            If WorksheetFunction.CountA(RngRow) <> 0 Then
               RngRow.Copy 
               DstRng.Offset(R, 0).pastespecial(xlpastevalues)
               R = R + 1
            End If
          Next RngRow
          
          Set DstRng = DstRng.Resize(R, SrcRng.Columns.Count)
          
           'Remove all empty columns except "B"
            For C = DstRng.Columns.Count To 3 Step -1
              Set Col = DstRng.Columns(C)
              If WorksheetFunction.CountA(Col.Cells) = 0 Then
                 Col.EntireColumn.Delete
              End If
            Next C
            
        Application.ScreenUpdating = True
        
    End Sub

  7. #7
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Copy Paste Macro with Values & Skipping Blanks

    Unfortunately that didn't remove the blank cells.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Paste Macro with Values & Skipping Blanks

    Do you have a sample file that i could test on?

  9. #9
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Copy Paste Macro with Values & Skipping Blanks

    I've attached a quick sample from the data I'm using. The formula is different but it shouldn't affect the test given it's a formula?

    Testing.xlsm

  10. #10
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Copy Paste Macro with Values & Skipping Blanks

    Perhaps I'm being too ambitious, this has been doing my head in for a couple of days. I think a slightly more manual system will have to do for this in the short term. Going to continue to learn until I get this fully automated though.

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy Paste Macro with Values & Skipping Blanks

    I got it to work for you - Replace this line
     If WorksheetFunction.CountA(RngRow) <> 0 Then
    with
     If RngRow <> "" Then

  12. #12
    Registered User
    Join Date
    05-30-2012
    Location
    LONDON
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Copy Paste Macro with Values & Skipping Blanks

    Wow you are a legend. I knew I was over complicating things. Thanks for all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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