I have an excel file which have a worksheet named contact. in contact work sheet in…
- User
I have an excel file which have a worksheet named contact. in contact work sheet in column B I have phone numbers of my customer. phone numbers stored in different format. I want a macro code that create a new worksheet with correct phone number format. phone number must be 10 digits. some phone number start with country code 46 that I would like to omit this. also some phone number start with 0 that I also like to omit this. I need the macro that do these jobs. please give me complete code.
- Assistant
Below is a VBA macro code that will create a new worksheet with corrected phone numbers which are 10 digits long, and omits country code 46 and 0 at the beginning:
Sub CorrectPhoneNumberFormat() ' Declare variables Dim srcWS As Worksheet, destWS As Worksheet Dim srcCell As Range, destCell As Range Dim phoneNum As String ' Set source worksheet to "Contact" worksheet Set srcWS = ThisWorkbook.Worksheets("Contact") ' Create a new worksheet for the corrected phone numbers Set destWS = ThisWorkbook.Worksheets.Add(, ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)) destWS.Name = "Corrected_PhoneNumbers" ' Set destination cell in the new worksheet Set destCell = destWS.Range("A1") ' Loop through each cell in column B of the source worksheet For Each srcCell In srcWS.Columns("B").Cells.SpecialCells(xlCellTypeConstants) ' Get phone number value phoneNum = srcCell.Value ' Remove country code 46 if present If Left(phoneNum, 2) = "46" Then phoneNum = Mid(phoneNum, 3) End If ' Remove 0 at the beginning if present If Left(phoneNum, 1) = "0" Then phoneNum = Mid(phoneNum, 2) End If ' If the phone number is 10-digit, copy it to the new worksheet If Len(phoneNum) = 10 Then destCell.Value = phoneNum ' Move to the next row in the destination worksheet Set destCell = destCell.Offset(1, 0) End If Next srcCell ' Inform the user that the process is complete MsgBox "Phone numbers are formatted and copied to the 'Corrected_PhoneNumbers' worksheet.", vbInformation, "Process Complete" End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.