简体   繁体   中英

Macro to Search, find, copy, paste a word and merge cells

i am trying to make a macro that can:

  1. search for the word "Yes" in all rows in column E, copy and paste it to the corresponded row in column B (the same row)
  2. Merge the cell in column B with the cell in column C

For example: The macro is searchig for the word "Yes" in column E, the word "Yes" is found in E5, copy and paste "Yes" to B5, merge B5 and C5 and confirm the merging (excell always displays a pop un window asking to manually click on the button to confirm the merging), continue with next search.

Please, try the next code. It will not ask anything when merging cells and the area will take the left cell value (B = Yes). If you need the value in C, or a concatenation between the two cells, you must tell us that:

Sub testMergeYesWord()
 Dim sh As Worksheet, lastR As Long, i As Long
 
 Set sh = ActiveSheet 'use here the sheet you need
 lastR = sh.Range("E" & rows.count).End(xlUp).row
    
 For i = 2 To lastR
    If UCase(sh.Range("E" & i).Value) = "YES" Then
        sh.Range("B" & i).Value = sh.Range("E" & i).Value
        Application.DisplayAlerts = False 'to avoid the message asking what value to be allocate to the merged area
         sh.Range("B" & i & ":C" & i).merge
        Application.DisplayAlerts = True
    End If
 Next i
 MsgBox "Ready..."
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM