+ Reply to Thread
Results 1 to 10 of 10

Find value and copy the Range to another sheet with merged cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2018
    Location
    Turkey
    MS-Off Ver
    2016
    Posts
    6

    Arrow Find value and copy the Range to another sheet with merged cells

    Hi All,

    I made a lot serach on the forums and google and I found a couple of possible solutions about my problem but still the codes do not work as I like. My problem is;

    I would like to find an exact value from a column and then to copy whole rows and colums which are the value that I found. My problem is, I can find the value and copy the rows and colums to another sheet but the code just copy the rows. Merged cells look empty. I would like to copy the rows and colums like I did manually select then do copy and paste.

    I uploaded the template file that I use. In that file as you can see with blue mark, I would like to copy the merged rows exaclty as it is seen which is 17082018/17 to another sheet with VBA.

    Thank you in advance...
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Find value and copy the Range to another sheet with merged cells

    Might be easiest to just unmerge the cells.

    Sub UnMergeCells()
        Dim rng As Range
        Dim a As String
        Dim i As String
        Dim sh As Worksheet
    
        Set sh = ActiveSheet
        For Each rng In sh.UsedRange
            If rng.MergeCells Then
                rng.Orientation = xlHorizontal
                a = rng.MergeArea.Address
                i = rng
                rng.MergeCells = False
                Range(a) = i
            End If
    
        Next
    
    End Sub

  3. #3
    Registered User
    Join Date
    10-01-2018
    Location
    Turkey
    MS-Off Ver
    2016
    Posts
    6

    Re: Find value and copy the Range to another sheet with merged cells

    thank you quick response. But I don't want to unmerged the cells. I would like to copy all from B19 to R28 to another sheet while they are merged. I think the code should find the first row and last row of which are "18" at A column and then should copy with this range (from B19 to R28) - to another sheet. but I think we should do this with varianbles.

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Find value and copy the Range to another sheet with merged cells

    Here is the code for macro.
    Value to be entered in B2 of Sheet "Copy".
    Sub CopyData()
    
    Dim LR As Long, X As Long, FstRo As Long, LastRo As Long
    Dim Fstr As String
    Dim Frng As Range
    
    On Error GoTo Line1
    Sheets("Packing").Activate
    
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    With Sheets("Copy")
    Fstr = .Range("B2")
    .Cells.Delete
    .Cells.Interior.Pattern = xlNone
    .Cells.Borders.LineStyle = xlNone
    .Cells.UnMerge
    End With
    
    Set Frng = Range("B1:B" & LR).Find(Fstr, After:=Range("B1"), LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
            
    If Not Frng Is Nothing Then
    FstRo = Frng.Row
    LastRo = Frng.End(xlDown).Row - 1
        If LastRo = Rows.Count - 1 Then
        LastRo = Range("C" & Rows.Count).End(xlUp).Row
        End If
    End If
    
    Range("B" & FstRo & ":R" & LastRo).Copy Sheets("Copy").Range("B2")
    Line1:
    End Sub
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 10-01-2018 at 11:01 PM.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  5. #5
    Registered User
    Join Date
    10-01-2018
    Location
    Turkey
    MS-Off Ver
    2016
    Posts
    6

    Re: Find value and copy the Range to another sheet with merged cells

    Thank you so much kvsrinivasamurthy! The code works perferct!

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Find value and copy the Range to another sheet with merged cells

    Thanks for the compliment.

  7. #7
    Registered User
    Join Date
    10-01-2018
    Location
    Turkey
    MS-Off Ver
    2016
    Posts
    6

    Re: Find value and copy the Range to another sheet with merged cells

    Quote Originally Posted by kvsrinivasamurthy View Post
    Thanks for the compliment.
    I had an issue about the code. If I write 17072018/01 to "B2" the code copy all the rows from 17072018/01 to 17072018/17. I need to copy just related row. I tried to modify the codes but I couldn't make it work properly. Could you please help me to revise that? Thank you.

  8. #8
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Find value and copy the Range to another sheet with merged cells

    Try this revised code

    Sub CopyData()
    
    Dim X As Long, FstRo As Long, LastRo As Long
    Dim Fstr As String
    Dim Frng As Range
    
    On Error GoTo Line1
    Sheets("Packing").Activate
    
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    With Sheets("Copy")
    Fstr = .Range("B2")
    .Cells.Delete
    .Cells.Interior.Pattern = xlNone
    .Cells.Borders.LineStyle = xlNone
    .Cells.UnMerge
    End With
    
    Set Frng = Range("B1:B" & LR).Find(Fstr, After:=Range("B1"), LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
            
    If Not Frng Is Nothing Then
    FstRo = Frng.Row
    
    If Frng.Offset(1, 0) = "" Then
    LastRo = Frng.End(xlDown).Row - 1
        If LastRo = Rows.Count - 1 Then
        LastRo = Range("C" & Rows.Count).End(xlUp).Row
        End If
    Else
    LastRo = FstRo
    
    End If
    
    
    End If
    
    Range("B" & FstRo & ":R" & LastRo).Copy Sheets("Copy").Range("B2")
    Line1:
    End Sub

  9. #9
    Registered User
    Join Date
    10-01-2018
    Location
    Turkey
    MS-Off Ver
    2016
    Posts
    6

    Re: Find value and copy the Range to another sheet with merged cells

    It didn't work. Now it doesn't copy the merged cells. I revised the code like this;

    If Range("A" & Frng.Row) = 1 Then
    FstRo = Frng.Row
    LastRo = Frng.End(xlDown).Row - 1
        Else
     FstRo = Frng.Row
    LastRo = Frng.Row
         
    End If
    I put "1" to A colum which has merged cells. With If condition now the code can work as I like. Than your for your guidence.

  10. #10
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Find value and copy the Range to another sheet with merged cells

    This code is tested for all cases in example data.
    Sub CopyData()
    
    Dim X As Long, FstRo As Long, LastRo As Long
    Dim Fstr As String
    Dim Frng As Range
    
    On Error GoTo Line1
    Sheets("Packing").Activate
    
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    With Sheets("Copy")
    Fstr = .Range("B2")
    .Cells.Delete
    .Cells.Interior.Pattern = xlNone
    .Cells.Borders.LineStyle = xlNone
    .Cells.UnMerge
    End With
    
    Set Frng = Range("B1:B" & LR).Find(Fstr, After:=Range("B1"), LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
            
    If Not Frng Is Nothing Then
    FstRo = Frng.Row
    k = Range("B" & FstRo + 1)
    
        If Frng.End(xlDown).Row = Rows.Count Then
        LastRo = Range("C" & Rows.Count).End(xlUp).Row
       
        Else
    
        T = 0
        Do While Range("B" & FstRo + T + 1) = ""
        T = T + 1
        Loop
        LastRo = FstRo + T
        
        End If
    
    End If
    
    Range("B" & FstRo & ":R" & LastRo).Copy Sheets("Copy").Range("B2")
    Line1:
    End Sub

+ 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.Find with merged cells
    By BuZZarD73 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2015, 12:32 PM
  2. Copy from merged cells in one sheet and paste to merged cells in another sheet
    By loroverde in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2015, 01:30 PM
  3. Replies: 1
    Last Post: 04-22-2013, 10:34 AM
  4. Using Range.find() doesn't work when the text you look for is in merged cells
    By nfuids in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-17-2012, 03:24 PM
  5. if find merged cell in a range change the reference sheet value to 0
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-06-2011, 07:41 AM
  6. Copy data from a sheet and Paste in another sheet into Merged cells
    By acsishere in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2011, 02:01 PM
  7. Loop Through A Range, Find Cells > 1, Copy Offset To Another Sheet
    By bugmenot in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-25-2008, 06:30 PM

Tags for this Thread

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