VBA code to check (1) if a name is already listed and if not AND if vale of adjacent cell >0 then add

Mel Smith

Well-known Member
Joined
Dec 13, 2005
Messages
1,041
Office Version
  1. 365
Platform
  1. Windows
I have two spreadsheets in different workbooks. On my Personnel sheet I have a long (1,000+) list of names in column B. In a different workbook, ='[Averages.xlsm]Seconds, I have a list of names in cells B4:B63.
What I want to do is loop through ='[Averages.xlsm]Seconds cells B4:B63 and if there is a name that is not listed on my Personnel sheet in column B, then loop through ='[Averages.xlsm]Seconds cells C4:C63 and if >0 then add the cell contents of the adjacent cell in column B to the next empty cell in column B on the Personnel sheet.

I.E. if, say B24 of my '[Averages.xlsm]Seconds is listed in Personnel column B then do nothing. If B24 of my '[Averages.xlsm]Seconds is not listed and C24 '[Averages.xlsm]Seconds >0 then add '[Averages.xlsm]Seconds B24 to the next empty cell in column B on the Personnel sheet.

As always, your help will be much appreciated.

Mel
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have managed to get my code working :)

VBA Code:
Sub CopyNames()
    Dim wb1 As Workbook, wb2 As Workbook 'Declare the workbooks
    Dim ws1 As Worksheet, ws2 As Worksheet 'Declare the worksheets
    Dim rng1 As Range, rng2 As Range
    Dim cell As Range
    Dim lastrow1 As Long, lastrow2 As Long
    Dim found As Boolean
   
    Set wb1 = Workbooks("Personnel Records.xlsm")
    Set wb2 = Workbooks("Averages.xlsm")
    Set ws1 = wb1.Sheets("Personnel")
    Set ws2 = wb2.Sheets("Players")
   
    lastrow1 = ws1.Cells(Rows.Count, "B").End(xlUp).Row
    'lastrow2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row
    lastrow2 = 63
   
    Set rng1 = ws1.Range("B1:B" & lastrow1)
    Set rng2 = ws2.Range("B4:B" & lastrow2)
   
    For Each cell In rng2
        found = False
        For Each Name In rng1
            If cell.Value = Name.Value Then
                found = True
                Exit For
            End If
        Next Name
        If Not found And cell.Offset(0, 1).Value > 0 Then
            lastrow1 = lastrow1 + 1 'Increment the last row by 1
            ws1.Cells(lastrow1, "B").Value = cell.Value
        End If
    Next cell
End Sub

Phew! Mel
 
Upvote 0
Solution
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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