+ Reply to Thread
Results 1 to 3 of 3

Copy particular cells of a row to another worksheet if cell value = x

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2013
    Location
    Phils
    MS-Off Ver
    Excel 2007
    Posts
    2

    Copy particular cells of a row to another worksheet if cell value = x

    Hi Guys,

    Kindly help me with this one. I have a macro in the System Network worksheet that copy particular cells of a row if the value of a cell = Network Attack to another worksheet named Attack Report.

    It's working as of this moment, but my problem is everytime the macro is run, it copies also the previously copied cells resulting into duplicates. What I want to do is when the macro runs, it skips the previously copied cells and only copy those that are not copied yet. Also, is there a way to put a button in the worksheet that will run the macro?

    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 = "Network Attack" 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
    Please see attached file for your reference.


    Thanks in advance
    Attached Files Attached Files
    Last edited by arlu1201; 02-17-2014 at 07:48 AM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Copy particular cells of a row to another worksheet if cell value = x

    The code needs a way to know it has already been copied. Your title mentions an X but you don't have any cells that actually have an X in them. This is a good idea but where would you want it? If I would guess, I would say you'd want it and the last cell in that row, on the right, right? If so, try:
    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 = "Network Attack" And UCase(c.End(xlToRight).Value) <> "X" 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
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    12-19-2013
    Location
    Phils
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Copy particular cells of a row to another worksheet if cell value = x

    Quote Originally Posted by Mordred View Post
    The code needs a way to know it has already been copied. Your title mentions an X but you don't have any cells that actually have an X in them. This is a good idea but where would you want it? If I would guess, I would say you'd want it and the last cell in that row, on the right, right? If so, try:
    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 = "Network Attack" And UCase(c.End(xlToRight).Value) <> "X" 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
    Hi Mordred, thanks for replying. I tried the code you provided but still does the same thing. Everytime the macro is run, it copies the needed data all over again. Is there a way to skip the data copied by the previously ran macro?

+ 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. [SOLVED] copy data from cells in a column on one worksheet to a single cell in another worksheet
    By jpsnickers in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-03-2013, 07:02 PM
  2. If x in cell, copy other cells to new worksheet
    By Jarae in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-14-2012, 09:45 AM
  3. [SOLVED] What is the best way to examine cell and copy adjacent cells to another worksheet?
    By LMersch in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 07-19-2012, 06:08 PM
  4. [SOLVED] Macro to copy certain cells to new worksheet based on cell value
    By mmctague in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-28-2012, 10:54 AM
  5. Replies: 8
    Last Post: 10-19-2011, 06:17 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