Find and copy based on cell values VBA

kamalm

New Member
Joined
Jul 30, 2018
Messages
33
Hi, I have multiple values along the Column E. Some of them are "ALARM". Each of the "ALARM" values have their own date and time at the Column B and C respectively but at the row before the "ALARM" values. I inserted sample at the table below:

ABCDEFGHI
1
2
324/05/201812:17:43
4ALARM
5
6
724/05/201812:18:55
8ALARM
9
10
11
1224/05/201812:20:22
13ALARM
14
15

<tbody>
</tbody>

i need macro that can copy the date to Column G while time to Column H, same row with "ALARM" values. The outcome should be like this:


ABCDEFGHI
1
2
324/05/201812:17:43
4ALARM24/05/201812:17:43
5
6
724/05/201812:18:55
8ALARM24/05/201812:18:55
9
10
11
1224/05/201812:20:22
13ALARM24/05/201812:20:22
14

<tbody>
</tbody>
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Are there any formulas in columns A:E?

No, there is no any formula. I just tried this code and it works. But this code only copy for date. Am I need to repeat the same code for time ?

Code:
Sub findalarm()


Dim rcnt As Long
rcnt = Worksheets(2).Range("E" & Rows.Count).End(xlUp).Row
For i = 1 To rcnt
If Range("E" & i).Value = "(ALARM" Then
    Range("E" & i).Offset(-1, -3).Copy
    Range("E" & i).Offset(0, 2).PasteSpecial xlPasteAll
    
    End If
    Next i
    
Application.CutCopyMode = False




End Sub
 
Upvote 0
Assumes the layout shown in your initial post.
Code:
Sub ALARM()
Dim V As Variant, c As Range
Application.ScreenUpdating = False
With Range("A3:E" & Cells(Rows.Count, "E").End(xlUp).Row)
    V = .Value
    .Columns(5).Replace "ALARM", "#N/A"
    On Error Resume Next
    For Each c In .Columns(5).SpecialCells(xlCellTypeConstants, xlErrors)
        c.Offset(0, 2).Resize(, 2).Value = c.Offset(-1, -3).Resize(, 2).Value
    Next c
    On Error GoTo 0
    .Value = V
End With
With Range("G:H")
    .Columns(1).NumberFormat = "dd/mm/yyyy"
    .Columns(2).NumberFormat = "hh:mm:ss"
    .EntireColumn.AutoFit
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top