Seperate us cell phone number from landline number? - excel

Is there a way i can check if a US number is a cell phone or landline?
I have found several sites online, but they are paid per number. Can i use code to do the check?
Thanks

This is less of an Excel question and more of a general question about phone numbers. If there's some way (I don't know of one) that you can freely check a phone number against some registry, then we could use some code to do this in Excel - sure. However, I'm not sure how to check whether a line is mobile or land line.
I did find this SO thread, but it looks like the free options are limited. I did find this site just Googling around, but I have no idea how to check if that's accurate or not. There's this site which also charges, but could help you.
Again, if you (or someone) does find a way to check phone numbers, you could probably automate it with VB, but getting to that point is the tricky part.
(Note: the webpages I linked above I just found Googling around).

I have worked on an admittedly crude way to do what you've requested, using the free service phonevalidator.com. I cannot attest to the accuracy of this service, but I tried for several cell numbers I know and several landlines as well and it was accurate for them.
This option uses VBA to go to the website, automatically enter a number, and scrape the phone type off of the results page. I have made the scraping a function (probably could be optimized better, but it works).
In a standard code module, start with this function:
Public Function GetPhoneNumType(phoneNum As String) As String
Dim ie As Object
Dim objResult As Object
'Initialize IE
Set ie = CreateObject("InternetExplorer.Application")
'Go to the phone validator
ie.Navigate "http://www.phonevalidator.com/index.aspx"
'Wait for the page to load
Do While ie.ReadyState <> 4
DoEvents
Loop
'Enter the phone number
ie.document.getElementByID("ContentPlaceHolder1_txtPhone").Value = phoneNum
'Click the search button
ie.document.getElementByID("ContentPlaceHolder1_SearchButton").Click
'Wait until javascript executes and displays the PhoneTypeLabel
On Error Resume Next
Do Until Not objResult Is Nothing
Set objResult = ie.document.getElementByID("ContentPlaceHolder1_PhoneTypeLabel")
Loop
On Error GoTo 0
'Return the phone type result, either LANDLINE or CELL PHONE
GetPhoneNumType = objResult.innerText
End Function
This takes in a phoneNum as a string and returns the result either as LANDLINE or CELL PHONE (I'm not sure if there are any other return types).
For purposes of this example, let's assume that I entered two phone numbers (these can be with or without dashes, but should have no other symbols and no leading 1, etc.) in cells A1 and A2, I can call this function like so:
Public Sub TestPhoneNum()
Dim rngNumbers As Range, c As Range
Set rngNumbers = Sheets("Sheet1").Range("A1:A2")
For Each c In rngNumbers
c.Offset(0, 1).Value = GetPhoneNumType(c.Value)
Next c
MsgBox "Complete!"
End Sub
This will put the phone number type in the column adjacent (to the right) of each cell that contains a phone number. You can adjust A1:A2 to be whatever range has the phone numbers. Whichever range you enter, this subroutine will always place the phone number type in the right adjacent cell.
I added the message box because this code is slow, and that notified me when the code was done executing.
Hopefully this gets you at least started.

Related

Using user input in VBA code and extracting data into workbook

I am exploring webscraping to try and improve efficiency when inputting data. Unfortunately the website I wish to extract data from is now in tabular format and so I wish to use VBA to manipulate the website to the desired result.
I'm not very familiar with coding/VBA but so far I have got VBA to open a website and search for a provided value. In this case the CAS number 67-64-1 refs Acetone on the website.
The code for this is:
Sub BrowseToSite()
Dim IE As New SHDocVw.InternetExplorer
IE.Visible = True
IE.Navigate "https://apps.who.int/food-additives-contaminants-jecfa-database/Search.aspx#"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
IE.Document.forms("form1").elements("ctl00$ContentPlaceHolder1$txtSearch").Value = "67-64-1"
IE.Document.forms("form1").elements("ctl00$ContentPlaceHolder1$btnSearch").Click
End Sub
Ultimately I wish to create a list in an excel sheet of CAS numbers that this code can loop through and return either the found phrase (in this case No safety concern at current levels of intake when used as a flavouring agent) or simply return a "Not Found". Sometimes the search returns multiple results, for the time being I just wish to take the first result.
This raises 2 problems I'm not sure how to solve:
How can I modify my code to loop through values within a column of a worksheet instead of having to explicitly give each one.
2.I'm unsure how to pull the data into the adjacent column.
Below is an image of the desired output. Column A is inputted by the user and hopefully column B is created by VBA code.
Any help would be appreciated.
what you need is a step by step process for web scraping.
i highly recommend you get familiar with seleniumbasic for vba https://florentbr.github.io/SeleniumBasic/
you need to loop on your excel rows using Range() or Cells(i,1) to read the row.
you check the number of search results using collections
save as many results as you wish in the excel in front of the row using cells(i, k) k being number of returned search results.
unfortunately the website did not load for me to help you further

Excel Userform/VBA Combine CheckBox & TextBox into a single cell string

A little background about what I'm trying to do:
My userform is going to be used to grade employee training with the results being added to an excel spreadsheet. The checkboxes in my form are representative of the different kinds of mistakes they can make and each mistake ideally has a quantity box that will specify how many times each mistake was made if its box is checked. I need both in one cell, as each row is 1 test for each employee. Here is an example of my userform:
Mistakes Screen Shot
All checkboxes and textboxes share the same number and I've already programmed them to automatically insert a 1 quantity if the box is checked/empty if unchecked. (Of course the quantity must be editable in case a mistake type is duplicated.)
So far I was able to use a loop with a string to get the checkboxes to put their captions into the single cell using this code:
Dim CheckBox As Control
Dim Mistakes As String, delimiter As String
For Each CheckBox In Me.Frame_Mistakes.Controls
If TypeOf CheckBox Is MSForms.CheckBox Then
If (CheckBox.Value) Then
Mistakes = Mistakes & delimiter & CheckBox.Caption
delimiter = " | "
End If
End If
Next
With Sheet1
.Cells(emptyRow, 4).Value = Mistakes
End With
However, so far I have been unable to figure out how to get the quantity to be added at the end of each mistake preceding the delimiter. I would prefer it if I could get the string to be in this format: Mistakes Format in Excel
If my intentions are unclear, I apologize. I am incredibly new and honestly surprised I was able to make it this far. Please and Thanks!
So, if I understand you correctly, You want the output of the Mistakes cell to read
[Caption of checked box](x[Number in Text Box])|[Caption of checked box](x[Number in Text Box])...
If that is so, you simply need to add a snippet of code to the end of your 'Mistakes' variable so that it reads:
Mistakes = Mistakes & delimiter & Checkbox.Caption & "(x" & TextBox.text & ")"
Where things get difficult is that you will need to differentiate between text boxes so that only the one that applies to the relevant checkbox is being used. You could do this in a number of ways, such as passing the Textbox as an argument, or with a switch case to name a few.
Another problem is making sure that the textbox only uses numbers. The way that I accomplish this is with a combination of the IsNumeric() and Val() functions. You first check if the value is numeric, then store it in an int using the Val() function. Since you only need it for a string, though, using the IsNumeric() function alone should be fine.
If you need more specific clarification, I would need to know exactly what you are looking for.

I cannot identify the 'VBA.Date' code component

I have found many blocks of code that containVBA.Date, for example;
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Then Exit Sub
Target.Offset(0, 1).Value = Format(VBA.Date, “MM/DD/YYYY”)
End Sub
Or
Sub YearSheets()
Dim i As Integer
i = 0
Do
Sheets.Add(After:=Sheets(Sheets.Count)).Name = Format(VBA.Date + i, “MM-DD-YYYY”)
i = i + 1
Loop Until i = 365
End Sub
I have tried these steps:
I have pushed F1 for VBA's help but it shows "Keyword Not Found".
Searched my VBA books but no one shows something because they take such a piece of information as unimportant or too easy (I guess).
I have tried to Google it --> You can imagine what happens with a combination of such common words.
I have pushed F2 in the VBE Window and opened the Object Browser
i. I searched for VBA and I got the library ... not very bright
ii. I searched for the Date Property and I found it to be preceded by the DateTime Module and going more in seniority from the VBA Library.
Question-born-from-question: Is it possible to be the VBA library here VBA.Date?
Frustrating-thing-that-happens: Once I type VBE IntelliSense shows Date Property, so it's hard-coded somewhere in my machine.
What really is VBA.Date - an object, a library that can be entered as as object?
Date() and Time() are Properties of the DateTime Class. Found in Excel VBA. They are of DataType Variant that contain the current system date when Date is used and current system time when Time is used.
They do not need to be preceeded by the VBA or the DateTime in order to be used they can simply be used with the terms Date, and Time.
Date, and if the calendar is Gregorian, Date$ behavior is unchanged by the Calendar property setting. If the calendar is Hijri, Date$ returns a 10-character string of the form mm-dd-yyyy, where mm (01-12), dd (01-30) and yyyy (1400-1523) are the Hijri month, day and year. The equivalent Gregorian range is Jan 1, 1980 through Dec 31, 2099.
In the future the best way to get information on specific function, properties, methods, classes and other members of the VBA Language you can use the Object Browser.
Marked one is the object browser and when clicked on will open this window (it can also be accessed with pressing F2) In this window enter what you are searching for
One you get results scroll through and look for a more specific item:
Now there are 2 points in the above image:
1) At the bottom of the screen it gives basic detail about this item, in this case
it states that it belongs to the `DateTime` Class, and is a property of that
class, and that it is a Variant.
2) Also in the picture above I have right clicked on the item and selected Help from
the context menu, this will bring up even more details about this item.
As you can see here Microsoft has built in support for this item and gives you details on what it is, what data type it would use and what it returns. Also, how to use and some common notes for when you use it!
VBA.Date and VBA.Time are functions that return the current date and the current time, respectively.
In your editor Tools / References you'll see 'Visual Basic For Applications' is selected. I have absoutely no proof but my idle conjecture is that this is where your VBA.Date (and VBA, everything) lies. Furthermore your F2 investigations show you the truth - It's in the VBA module there. Note you can use VBA.Date or just Date and they are the same thing.
In short it's hard coded in that DLL file referred to in Tools/References.

Simple vba program in Excel

Sub TEST()
If cells(i, "R").Value <> "UK" Then
cells(i, "R").Interior.ColorIndex = 3
End If
End Sub
If I run this program it throws application defined error \
I am new to Excel (beginner)
How to correct this error!!!
Thanks In advance
I think the issue is "R" that I know of the cells method takes 2 parameters one is rows the other is columns (in that order) but this is done by number not letter so if you change it to cell(1,18) then the code above works fine.
This link may also be useful to learn more, among other things it describes how you would normally select a range first as I believe your code above will assume the currently selected page, however you might want to run in on a button click from another page or as soon as the spreadsheet opens.
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
The problem is that the variable i has not been assigned a value. VBA assumes that it is zero. Since i is used to determine the row of the cell, Excel throws an exception because there is no row 0!
First you have to define i variable
for example: Dim i as variant

Creating a custom hyperlink function in excel

I have searched far and wide, but can't find an answer to this simple question. I want to make a custom function in excel which will create a hyperlink.
Excel has a built in hyperlink function that works like this:
=Hyperlink(link_location, display_text)
I want to create a function called CustomHyperlink which takes one parameter, and returns a hyperlink to a google query with that parameter. Just for the sake of the question, lets assume that the passed parameter is a alphanumeric string, with no spaces.
Essentially, calling
=CustomHyperlink("excel")
should be the same as calling
=Hyperlink("http://www.google.com/search?q=excel", "excel")
This seems like such a simple task, but I absolutely cannot find a way to make this function.
Can anyone offer some quick help?
I can offer a partial solution, one that will update an existing hyperlink. This only makes sence if you are using it like, say
CustomHyperlink(A1)
were A1 contains the required serch term
To use,
enter your UDF formula in a cell, eg =CustomHyperlink(A1)
create a hyperlink on the cell (right click, Hyperlink...) . This can be any hyperlink, valid or invalid
put the required search term in the referenced cell, eg in A1 put excel
When the UDF runs it will update the hyperlink to Google the entered search term
Function CustomHyperlink(Term As String) As String
Dim rng As Range
Set rng = Application.Caller
CustomHyperlink = Term
If rng.Hyperlinks.Count > 0 Then
rng.Hyperlinks(1).Address = "http://www.google.com/search?q=" & Term
End If
End Function
In VBA editor you can use
ThisWorkbook.FollowHyperlink Address:=(strWebsite), NewWindow:=True
Which will take you to that specific website, and just build a function around that to navigate you to the site you need.
Nice idea although this isn't possible.
You seem to want to have the formula of the cell as one thing (your custom function call) and yet have the value as another (the hyperlink / URL) which simply isn't possible.
The correct way through VBA to add a hyperlink is to use the Hyperlinks property but it is not possible to call this property, through a Worksheet UDF (because of the reason above).
What is wrong with just using the the built-in =Hyperlink() worksheet function? You could effectively parameterise your URL as follows (where cell A1 = Excel):
=HYPERLINK("http://www.google.com/search?q="&A1)
You can't do this directly for the reasons creamyegg suggests, but there is a way to achieve the functionality albeit with a bit of a performance consideration.
You could use the Worksheet_Change event to track for the presence of your UDF then process the hyperlink addition there.
You would need to set up an empty function to allow this to happen, otherwise Excel will throw an error whenever you entered =CustomHyperlink... in a cell.
The below should work, not really had time to test.
Private Sub worksheet_change(ByVal target As Range)
Dim SearchValue As String
If LCase(Left(target.Formula, 16)) = "=customhyperlink" Then
SearchValue = Mid(target.Formula, 19, Len(target.Formula) - 20)
target.Value = SearchValue
target.Hyperlinks.Add target, "http://www.google.com/search?q=" & SearchValue, , "Search Google for " & SearchValue, SearchValue
End If
End Sub
The performance consideration is of course the volatile Worksheet_Change event as this can really kill large, complex workbooks.

Resources