+ Reply to Thread
Results 1 to 6 of 6

Copy range to other sheet depending on entry in cell

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2016
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    3

    Copy range to other sheet depending on entry in cell

    Hello,

    I recently discovered VBA.
    I want to write values froms cells C4:C10 to another sheet, depending on the number given in C2.
    On the next worksheet (ws2), I have number 1 to 10. If the number in the first row is the same as given in C2, then the values (C4:10) need to be copied in the cells below that number.

    I already got a code to find the the same number:
    Private Sub CommandButton1_Click()
    Dim FindString As String
    Dim Rng As Range
    FindString = Sheets("ws1").Range("c2").Value
    If Trim(FindString) <> "" Then
    With Sheets("ws2").Range("B1:K1")
    Set Rng = .Find(What:=FindString, _
    After:=.Cells(.Cells.Count), _
    LookIn:=xlValues, _
    LookAt:=xlWhole, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, _
    MatchCase:=False)
    If Not Rng Is Nothing Then
    Application.Goto Rng, True
    Else
    MsgBox "Nothing found"
    End If
    End With
    End If
    End Sub
    Many thanks! *How can I add an attachment to this post?
    screenshot excel test.png screenshot excel test2.png Test.xlsm
    Last edited by RutgerdV; 03-17-2016 at 05:49 AM.

  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,863

    Re: Copy range to other sheet depending on entry in cell

    Try:
    Private Sub CommandButton1_Click()
    Dim FindString As String
    Dim Rng As Range
    FindString = Sheets("ws1").Range("c2").Value
    If Trim(FindString) <> "" Then
    With Sheets("ws2").Range("B1:K1")
    Set Rng = .Find(What:=FindString, _
    After:=.Cells(.Cells.Count), _
    LookIn:=xlValues, _
    LookAt:=xlWhole, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, _
    MatchCase:=False)
    If Not Rng Is Nothing Then
      Sheets("ws1").Range("C4:C10").copy Rng.offset(1,0)
    Else
    MsgBox "Nothing found"
    End If
    End With
    End If
    End Sub
    and please edit your post to comply with http://www.excelforum.com/forum-rule...rum-rules.html by using code tags
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    03-17-2016
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    3

    Re: Copy range to other sheet depending on entry in cell

    Thanks, got it working with some adjustments:
    Private Sub CommandButton1_Click()
        Dim FindString As String
        Dim Rng As Range
        FindString = Sheets("ws1").Range("c2").Value
        If Trim(FindString) <> "" Then
            With Sheets("ws2").Range("B1:K1")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing Then
                                Sheets("ws1").Range("C4:C10").Copy Destination:=Rng.Offset(1, 0)
                Else
                    MsgBox "Nothing found"
                End If
                End With
        End If
    End Sub

  4. #4
    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,863

    Re: Copy range to other sheet depending on entry in cell


    As Destination is first (and only) parameter in Range.Copy method name of the parameter can be skipped, so
    Sheets("ws1").Range("C4:C10").Copy Destination:=Rng.Offset(1, 0)
    is exactly the same as:
    Sheets("ws1").Range("C4:C10").Copy Rng.Offset(1, 0)
    see how you use MsgBox - for instance you wrote:
    MsgBox "Nothing found"
    extended version would be:
    MsgBox Prompt:="Nothing found"
    ---------------

    Glad it worked, and also from code tags usage in post #3. What I asked before (and ask again) is to push "Edit Post" button below your first post and use code tags also there.

    PS. As you have action for both "found" and "not found" have a look on soch "inversion" in the code:
                If Rng Is Nothing Then
                    MsgBox "Nothing found", vbExclamation
                Else
                     Sheets("ws1").Range("C4:C10").Copy Rng.Offset(1, 0)
                End If
    I added also second (optional) parameter to MsgBox to emphasize that it is a warning message

  5. #5
    Registered User
    Join Date
    03-17-2016
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    3

    Re: Copy range to other sheet depending on entry in cell

    Thanks you really helped me!

  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,863

    Re: Copy range to other sheet depending on entry in cell

    Glad to hear that.
    So if that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. Check cell value in range, depending on condition copy to another Sheet
    By KemalO in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-28-2015, 11:01 AM
  2. Replies: 0
    Last Post: 03-24-2014, 07:31 AM
  3. [SOLVED] Copy & paste entire row to different sheet based on cell entry
    By XxCMoneyxX in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-14-2013, 07:52 PM
  4. [SOLVED] Copy cell from 1 sheet to another sheet depending on what info is displayed in a cell
    By chilli76 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-24-2013, 12:33 PM
  5. Macro to copy data from one sheet to another depending on name in first cell
    By subtilty in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2013, 07:44 AM
  6. [SOLVED] IF forumla - need to populate a cell on one sheet depending on range of cells in another
    By Emma2902 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 12-14-2012, 07:43 AM
  7. Copy a range to different sheets depending on first cell contents
    By WaveOfMutilation in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 03-01-2010, 12:58 PM

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