Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:02:14+00:00 2026-06-02T21:02:14+00:00

I have 2 sheets (with phone numbers) in my document. if the number exists

  • 0

I have 2 sheets (with phone numbers) in my document. if the number exists in sheet1 i want to remove the row from sheet 2.

I’m almost there (This is the first time I am using VBA). But could anyone help me with the last part.

Sub CleanList()

    Dim stopList As Range, cell1 As Range

    Set stopList = Sheet1.Range("A1:A10000")

    For Each cell1 In stopList
        Dim fullList As Range, cell2 As Range
        Set fullList = Sheet2.Range("A2:A10000")

        For Each cell2 In fullList
            If NumberFix(cell1.Value) = NumberFix(cell2.Value) Then
                cell2.EntireRow.Delete
            End If
        Next cell2
    Next cell1

End Sub

Private Function NumberFix(ByVal nr As String) As String

    If Not nr.StartsWith("46") Then
        nr = "46" + nr
    End If

    NumberFix = nr

End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:02:15+00:00Added an answer on June 2, 2026 at 9:02 pm

    The first thing is the way your using nr.StartsWith is more VB.NET-esque. THe function your looking for in VBA (probably not VB-script btw) is

    Dim firstTwoChar As String
    firstTwoChar = Mid(nr, 1, 2)
    
    If Not firstTwoChar = "46" Then
        nr = "46" + nr
    End If
    
    NumberFix = nr
    

    But even with that I’d say you should not be using a for...each iterator if you are deleting rows. The problem is when you delete row 5 then row 6 becomes row 5 and the next row you go to is row “6” but is actually row 7 in the original list, effectively skipping the original row 6.

    You need to move backwards. Something like

    Sub CleanList()
    
        Dim stopList As Range, cell1 As Range
    
        Set stopList = Sheet1.Range("A1:A10000")
    
        For Each cell1 In stopList
            Dim fullList As Range, cell2 As Range
    
            Dim firstRowSheet2 As Integer, lastRowSheet2 As Integer, r As Integer
            Dim sheet1sNumber As String
            sheet1sNumber = NumberFix(cell1.Value)   'you really only need to do this once 
                                                     so you may as well bring it out of
                                                     the for loop and store the value and 
                                                     not recalculate each time
            Dim cell2 As Range
            For r = firstRowSheet2 To lastRowSheet2 Step -1 
                             '"Step -1" allows you to move backwards through the loop
                With Sheet2
                    Set cell2 = .Cells(r, 1)
                    If sheet1sNumber = NumberFix(cell2.Value) Then
                            cell2.EntireRow.Delete
                        End If
                End With
    
            Next r
    
        Next cell1
    
    End Sub
    

    But of course @ExternalUse is right. There are a lot of built in options for removing duplicates from a list. Unless you are trying to learn VBA, then this is a good exercise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sheet A that looks something like this: Name Phone Number Title
I have four sheets in a spreadsheet. On each sheet there is a textbox.
i have two sheets having data like this sheet1 : **A** **B** **C** 752
I have 10 Excel files with 50 sheets in each, every sheet has a
I have used Microsoft.Office.Interop.Excel namespace to create excel sheets using C# . I want
Using Excel 2007 VBA Have a sheet Dashboard, have other sheets but some special
I have two sheets, the first pulls the data from the second to print
I have an Excel document with 10000 rows of data in two sheets, the
Possible Duplicate: Copy column from one Excel sheet to another Excel sheet I have
I want to export data from mysql tables to excel sheet. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.