+ Reply to Thread
Results 1 to 6 of 6

Copy cells in a defined range > 0, paste in a defined cell on other worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2018
    Location
    Perth
    MS-Off Ver
    2016
    Posts
    18

    Copy cells in a defined range > 0, paste in a defined cell on other worksheet

    Hi All,

    I am trying to copy cells in a range (sheet1) that are greater than zero, and paste to another worksheet (sheet2) in a defined cell. Currently the code I am using works, but it doesn't have the flexibility of choosing a defined range to copy from, and a defined cell to paste the values to.

    The copied range is starts in sheet 1, cell A1. The copied range pastes to Sheet2, cell A2 (by default). Wondering if there is a VBA wiz out there than can explain how to control where you copy and paste your ranges to between worksheets?

    Am new to this community and grateful for the responses I have had to my problems so far! Thanks in advance

    Here is my code:

    Sub trial()
    
    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    Sheets("Sheet1").Select
    
    
    For i = 1 To a
    
    If Worksheets("Sheet1").Cells(i, 1).Value > 0 Then
    
        Worksheets("Sheet1").Rows(i).Copy
        Worksheets("Sheet2").Activate
        b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
        Worksheets("Sheet2").Cells(b + 1, 1).Select
        ActiveSheet.Paste
    
    End If
    
    Next
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Copy cells in a defined range > 0, paste in a defined cell on other worksheet

    Hi,

    You could for instance use inputbox with type = 8 to ask user where input and output ranges are.
    And as you will work with ranges, then looping by range collection members will be probably a natural choice.

    See such code (I assumed that you need to copy only the cells with positive values - as described in the post, not whole rows, as it is done in your code);

    Sub trial()
    Dim inpRng As Range, outCell As Range, currCell As Range
    
    Set inpRng = Application.InputBox(prompt:="Please select input Range", Type:=8)
    Sheets("Sheet2").Activate
    Set outCell = Application.InputBox(prompt:="Please select output cell", Type:=8).Cells(1, 1)
    For Each currCell In inpRng
      If currCell.Value > 0 Then
        currCell.Copy outCell
        Set outCell = outCell.Offset(1, 0)
      End If
    Next currCell
    End Sub
    Attached Files Attached Files
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    06-26-2018
    Location
    Perth
    MS-Off Ver
    2016
    Posts
    18

    Re: Copy cells in a defined range > 0, paste in a defined cell on other worksheet

    Hi Kaper,

    Thank you for your post. I was able to modify this to exactly what I needed. Given this will be integrated into a very large spreadsheet which much more code before hand, I slightly changed it to paste in the specific cell on the next sheet I needed.

    Great work! This has made my job just a bit easier

    Included my edit to this:

    Sub trial()
    Dim inpRng As Range, outCell As Range, currCell As Range
    Set inpRng = Worksheets("Sheet1").Range("A1:A15")
    'Set outCell = Worksheets("Sheet1").Range("B5:B21") '.Cells(1, 1)
    'Set inpRng = Application.InputBox(prompt:="Please select input Range", Type:=8)
    Set outCell = Worksheets("Sheet2").Cells(5, 5)
    'Sheets("Sheet2").Activate
    
    
    For Each currCell In inpRng
      If currCell.Value > 0 Then
        Worksheets("Sheet1").Activate
        currCell.Copy outCell
        Worksheets("Sheet2").Activate
        Set outCell = outCell.Offset(1, 0)
        
      End If
    Next currCell
    End Sub
    Regards,
    Jackson

  4. #4
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Copy cells in a defined range > 0, paste in a defined cell on other worksheet

    try below code
    Sub trial()
    Dim r As Range, myRange As Range, i As Long, sht2 As Worksheet
    
    Set sht2 = Worksheets("Sheet2")
    Set myRange = Sheet1.Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    Sheet1.Select
    
    For Each r In myRange
    
        If r.Value > 0 Then
            i = i + 1
            r.Copy sht2.Range("A" & i + 1)
        End If
    Next r
        MsgBox "Ok"
    End Sub
    Regards,
    MohanS


    "Perfection is not attainable, but if we chase perfection we can catch excellence." - Vince Lombardi

    You can simply say thanks by clicking "*Add Reputation" icon

  5. #5
    Registered User
    Join Date
    06-26-2018
    Location
    Perth
    MS-Off Ver
    2016
    Posts
    18

    Re: Copy cells in a defined range > 0, paste in a defined cell on other worksheet

    Hi Mohan,

    I ran your code this morning and it also is applicable to the task I am trying to achieve. Has given me a second perspective on the same problem. Works well!

    Thank you for taking the time to help me

    Regards,
    Jackson

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Copy cells in a defined range > 0, paste in a defined cell on other worksheet

    Glad to hear so,

    So I overestimated the need for flexibility in : "flexibility of choosing a defined range to copy from"
    And indeed added not needed flexibility to what you wanted to have defined: "a defined cell to paste the values to"

    Just one side note:

    If you run code once, and there were 5 positive values in A1:A15 they will be copied to E5:E9
    but then when you change your data in A1:A15 and only 2 values are positive, then they will be copied to E5 and E6, but content of E7:E9 will remain unchanged.
    So I'd suggest adding clearing contents of possible output cells. for instance in such way:

    Sub trial()
    Dim inpRng As Range, outCell As Range, currCell As Range, lastrow as long
    Set inpRng = Worksheets("Sheet1").Range("A1:A15")
    With Worksheets("Sheet2")
      Set outCell = .Cells(5, 5)
      lastrow = .Cells(rows.count, 5).end(xlup).row
      if lastrow<5 then lastrow=5
      .range(.Cells(5, 5),.cells(lastrow,5)).clearcontents
    end with
    
    For Each currCell In inpRng
    '...
    Could be also easier just by adding to code from previous post (anywhere above the For loop).
    Worksheets("Sheet2").RAnge("E5:E104").Clearcontents

+ 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. Range.Copy limitation issue (1004 application-defined or object-defined error)
    By macro_student in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2016, 10:21 AM
  2. [SOLVED] If word entered not in defined name range, copy & paste it to bottom of defined name range
    By Butcher1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2014, 01:08 PM
  3. Copy, paste, replace- user defined range in all the defined sheets
    By aganesan99 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2014, 12:28 PM
  4. [SOLVED] Macro to copy a cell range and paste to a new sheet 'n' times where 'n' defined by formula
    By staminaboy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-26-2013, 11:39 AM
  5. EXCEL 2003 copy paste a defined range in a cell
    By EXCELNOOB123 in forum Excel General
    Replies: 1
    Last Post: 03-07-2011, 10:01 PM
  6. Finding a defined range of cells then Copy & Paste
    By bernborough in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-11-2010, 06:45 PM
  7. [SOLVED] Application-defined or object-defined error (worksheet, range, sel
    By darxoul in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-02-2006, 09:05 AM

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