Copy/Refence specific cells if particular cell is equal to value

jhemz

New Member
Joined
Dec 19, 2013
Messages
15
Hi Guys,

I would like to ask if my idea is possible and doable. Currently, I have two worksheets (System Network and AttackReport). I want to copy or reference specific cells if the value of cells in Column B in System Network sheet is equal to NetworkAttack.

If ColumnB=NetworkAttack, I want to reference or copy of that row:
- ColumnA in System Network worksheet to ColumnA in AttackReport worksheet
- ColumnC in System Network worksheet to ColumnB in AttackReport worksheet
- ColumnE in System Network worksheet to ColumnF in AttackReport worksheet
- ColumnF in System Network worksheet to ColumnG in AttackReport worksheet
- ColumnG in System Network worksheet to ColumnI in AttackReport worksheet
- ColumnH in System Network worksheet to ColumnJ in AttackReport worksheet
- ColumnI in System Network worksheet to ColumnL in AttackReport worksheet
- ColumnJ in System Network worksheet to ColumnM in AttackReport worksheet

72zr0l.jpg


29o0opt.jpg



Could this be possible?


Thanks in advance!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this. This assumes you want the data entered on the next available row of Attack Report.
Code:
Sub copyData()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range, c As Range, rw As Long
Set sh1 = Sheets("System Network")
Set sh2 = Sheets("Attack Report")
lr = sh1.Cells(Rows.Count, 2).End(xlUp).Row
Set rng = sh1.Range("B7:B" & lr)
    For Each c In rng
        If c.Value = "NetworkAttack" Then
            rw = sh2.Cells(Rows.Count, 1).End(xlUp)(2).Row
            With sh2
                .Range("A" & rw) = c.Value
                .Range("B" & rw) = c.Offset(0, 2).Value
                .Range("F" & rw) = c.Offset(0, 4).Value
                .Range("G" & rw) = c.Offset(0, 5).Value
                .Range("I" & rw) = c.Offset(0, 6).Value
                .Range("J" & rw) = c.Offset(0, 7).Value
                .Range("L" & rw) = c.Offset(0, 8).Value
                .Range("M" & rw) = c.Offset(0, 9).Value
            End With
        End If
    Next
End Sub
 
Upvote 0
Try this. This assumes you want the data entered on the next available row of Attack Report.
Code:
Sub copyData()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range, c As Range, rw As Long
Set sh1 = Sheets("System Network")
Set sh2 = Sheets("Attack Report")
lr = sh1.Cells(Rows.Count, 2).End(xlUp).Row
Set rng = sh1.Range("B7:B" & lr)
    For Each c In rng
        If c.Value = "NetworkAttack" Then
            rw = sh2.Cells(Rows.Count, 1).End(xlUp)(2).Row
            With sh2
                .Range("A" & rw) = c.Value
                .Range("B" & rw) = c.Offset(0, 2).Value
                .Range("F" & rw) = c.Offset(0, 4).Value
                .Range("G" & rw) = c.Offset(0, 5).Value
                .Range("I" & rw) = c.Offset(0, 6).Value
                .Range("J" & rw) = c.Offset(0, 7).Value
                .Range("L" & rw) = c.Offset(0, 8).Value
                .Range("M" & rw) = c.Offset(0, 9).Value
            End With
        End If
    Next
End Sub

Hi JLGWhiz,

I just corrected some functions but it does the job very well. Many thanks for the help! :)
 
Upvote 0
Try this. This assumes you want the data entered on the next available row of Attack Report.
Code:
Sub copyData()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range, c As Range, rw As Long
Set sh1 = Sheets("System Network")
Set sh2 = Sheets("Attack Report")
lr = sh1.Cells(Rows.Count, 2).End(xlUp).Row
Set rng = sh1.Range("B7:B" & lr)
    For Each c In rng
        If c.Value = "NetworkAttack" Then
            rw = sh2.Cells(Rows.Count, 1).End(xlUp)(2).Row
            With sh2
                .Range("A" & rw) = c.Value
                .Range("B" & rw) = c.Offset(0, 2).Value
                .Range("F" & rw) = c.Offset(0, 4).Value
                .Range("G" & rw) = c.Offset(0, 5).Value
                .Range("I" & rw) = c.Offset(0, 6).Value
                .Range("J" & rw) = c.Offset(0, 7).Value
                .Range("L" & rw) = c.Offset(0, 8).Value
                .Range("M" & rw) = c.Offset(0, 9).Value
            End With
        End If
    Next
End Sub

Hi JLGWhiz,

Everytime the macro runs, it still copy the previously copied cell so it ended with duplicate rows. Is there a way to skip the rows which was already previously copied/transferred?
 
Upvote 0
Please post the code with the changes you made.
 
Upvote 0
Hi JLGWhiz,

I didn't make changes on the on the code, but on the header arrangement (row 6) for the AttackReport sheet.
 
Upvote 0
Hi JLGWhiz,

Here is the code I modified quite a bit:

Sub copyData()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range, c As Range, rw As Long
Set sh1 = Sheets("System Network")
Set sh2 = Sheets("Attack Report")
lr = sh1.Cells(Rows.Count, 2).End(xlUp).Row
Set rng = sh1.Range("B7:B" & lr)
For Each c In rng
If c.Value = "NetworkAttack" Then
rw = sh2.Cells(Rows.Count, 1).End(xlUp)(2).Row
With sh2
.Range("A" & rw) = c.Offset(0, -1).Value
.Range("B" & rw) = c.Offset(0, 1).Value
.Range("F" & rw) = c.Offset(0, 3).Value
.Range("G" & rw) = c.Offset(0, 4).Value
.Range("I" & rw) = c.Offset(0, 5).Value
.Range("J" & rw) = c.Offset(0, 6).Value
.Range("L" & rw) = c.Offset(0, 7).Value
.Range("M" & rw) = c.Offset(0, 8).Value
End With
End If
Next
End Sub


How can the macro run every 1 minute or so? Also, is there a way that whenever the macro runs, it skips the previously referenced/copied cell?


Thanks!
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

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