VBA Code writing

bubububub

New Member
Joined
Jul 2, 2010
Messages
18
Hello,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Could you please help me design a code to complete these tasks:<o:p></o:p>
<o:p></o:p>
Select a number in a column (from Excel 2007)<o:p></o:p>
Switch to internet explorer<o:p></o:p>
paste the number (The box on the website should already be selected. In case it matters, the link is http://www.upcdatabase.com/itemform.asp) <o:p></o:p>
hit enter<o:p></o:p>
copy the url<o:p></o:p>
hit back on the browser (This should also reselect the box for entering the next number, or at least it does when I use the backspace key after clicking on the page.)<o:p></o:p>
paste the url in the column next to the number<o:p></o:p>
and repeat the process again with the next number in the column<o:p></o:p>
<o:p></o:p>
Is this even possible?<o:p></o:p>
<o:p></o:p>
Thank you immensely for your help,<o:p></o:p>
Bub<o:p></o:p>
<o:p></o:p>
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Am I suffering from deja vu?

I could have sworn I saw, and answered, a very similar post yesterday.

Brian

I'm not 100% sure but I think the OP wants to check if the UPCs are valid as well as look them up.

I also suggested using the HYPERLINK function when I saw the resultant URL from the search.
 
Upvote 0
Here is some code to automate the job. It is not tested under all circumstances. The web page has several variances in data layout.
Rich (BB code):
'========================================================================================
'- LOOK UP BARCODES ON THE WEB AND GET DESCRIPTION
'- VB Editor Tools/Reference to 'Microsoft Internet Controls' (SHDocVw.dll)
'- InternetExplorer Object : http://msdn.microsoft.com/en-us/library/aa752084(VS.85).aspx
'----------------------------------------------------------------------------------------
'- EXTRACTS THE PAGE HTML TEXT AS A STRING (can avoid "Access Denied" with other methods)
'- Method : Format column A as text and list codes A2 down. Run the macro.
'- Additional data in a field means that an extra data type is required in array Arr1()
'- #### This is the only action required.  #######
'- Brian Baulsom July 2010.      Excel 2003.
'========================================================================================
'- Worksheet
Dim ws As Worksheet
Dim BarCode As String
Dim MyRow As Long
Dim MyCol As Integer
Dim LastRow As Long
Dim Counter As Integer
'----------------------------------------------------------------
'- Get Web Page
Const MyBaseURL As String = "http://www.upcdatabase.com/item/"
Dim IE As SHDocVw.InternetExplorer
Dim MyURL As String
Dim LoadTimeOut As Integer   ' web page timeout 10 seconds
Dim LoadTimer As Integer     ' seconds counter
Dim rsp As Variant           ' message box
Dim Success As Integer       ' count successes
'-----------------------------------------------------------------
'- Page Content
Dim PageString As String
Dim MyRegExp As Object
Dim MyMatches As Object
Dim Arr1()                  ' array of data items
Dim Arr2()                  ' data item positions in the string
Dim N1, N2, C1, C2          ' data string extract pointers
Dim DataValue As String
'----------------------------------------------------------------------------------------
'========================================================================================
'- MAIN ROUTINE TO GET LOOKUP VALUE FROM WORKSHEET
'- Barcodes are in column A
'========================================================================================
Sub BARCODE_LOOKUP()
    Application.Calculation = xlCalculationManual
    '####################################################################################
    '- SHEET HEADERS & DATA ITEM LIST
    '- ADD NEW ITEMS HERE. EXACT TEXT. IN CORRECT ORDER.
    '-------------------------------------------------------------------------------------
    Arr1() = Array("Bar Code", "Description", "Size/Weight", "Owner GLN", "Owner Name", _
            "Owner Address", "Issuing Country", "Last Modified", "Last modified by", _
            "Pending Updates")
    '####################################################################################
    ReDim Arr2(UBound(Arr1) + 1)
    LoadTimeOut = 10        ' 10 seconds
    Set MyRegExp = CreateObject("VbScript.RegExp")
    '-----------------------------------------------------------------------------------
    Set ws = ActiveSheet
    ws.Range(Cells(1, "A"), Cells(1, UBound(Arr1))) = Arr1()
    LastRow = ws.Range("A65536").End(xlUp).Row
    Counter = LastRow - 1
    Success = 0
    '=================================================================================
    '- LOOP WORKSHEET ROWS
    For MyRow = 2 To LastRow
        PageString = ""
        BarCode = CStr(ws.Cells(MyRow, "A").Value)
        MyURL = MyBaseURL & BarCode
        '==============================================
        GET_WEB_PAGE                    ' subroutine
        '==============================================
        '- CHECK FOR ERRORS IN THE PAGE
        If InStr(1, PageString, "Item not found", vbTextCompare) > 0 Then
            ws.Cells(MyRow, 2).Value = "Item not found"
        ElseIf InStr(1, PageString, "incorrect or invalid", vbTextCompare) > 0 Then
            ws.Cells(MyRow, 2).Value = "Invalid number"
        Else
            If rsp = vbCancel Then Exit For
            If rsp = "web page error" Then GoTo NextLine
            '-------------------------------------------
            GET_PAGE_CONTENT    ' subroutine
            '-------------------------------------------
        End If
        '===============================================
        Application.Wait Now + TimeValue("00:00:02")
NextLine:
    Next
    '-----------------------------------------------------------------------------------
    MsgBox ("Completed " & Success & " of " & Counter & " records." _
                & IIf(rsp = vbCancel, vbCr & "Cancelled by user", ""))
    Application.StatusBar = False
    Application.Calculation = xlCalculationAutomatic
End Sub
'========== END OF MAIN =================================================================
'========================================================================================
'- SUBROUTINE : GET WEB PAGE
'- Visible property set False so it runs in the background.
'- StatusBar shows progress.
'- Timeout set to 10 seconds above
'========================================================================================
Private Sub GET_WEB_PAGE()
    LoadTimer = 0
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = False
        '--------------------------------------------------------------------------------
        On Error Resume Next
        .navigate MyURL
        Do Until (.readyState = READYSTATE_COMPLETE) Or (LoadTimer >= LoadTimeOut)
            Application.StatusBar = MyRow - 1 & " of " & Counter & "  :  " _
                & BarCode & "   " & LoadTimer & " seconds."
            Application.Wait Now + TimeValue("0:00:01") ' WAIT 1 SECOND
            LoadTimer = LoadTimer + 1
            DoEvents
        Loop
        On Error GoTo GetOut
        '--------------------------------------------------------------------------------
        '- PAGE TO STRING
        PageString = CStr(.Document.Body.innerhtml)
        '--------------------------------------------------------------------------------
        '- CLOSE IE
        .Quit
        '--------------------------------------------------------------------------------
        '- TIMEOUT MESSAGE
        If LoadTimer >= LoadTimeOut Then
            rsp = MsgBox("The web page indicated in the statusbar has timed out" & vbCr _
                & "OK to try the next one", vbOKCancel)
            If rsp = vbCancel Then Exit Sub
        Else
            Success = Success + 1
        End If
        '--------------------------------------------------------------------------------
    End With
    Set IE = Nothing
    Exit Sub
    '-----------------------------------------------------------------------------------
GetOut:
    MsgBox ("Error loading web page")
    rsp = "web page error"
End Sub
'========== EOP =========================================================================
'========================================================================================
'- SUBROUTINE : GET PAGE CONTENT
'- REGULAR EXPRESSIONS TO EXTRACT DATA FROM THE PAGE STRING
'========================================================================================
Private Sub GET_PAGE_CONTENT()
    Dim C1, C2      ' string start/finish character positions
    '------------------------------------------------------------------------------------
    '- STAGE 1 : EXTRACT JUST THE DATA TABLE
    With MyRegExp
        .Global = True
        .ignorecase = True
        .Pattern = "Description[\s\S]*/table"
        Set MyMatches = .Execute(PageString)        ' zero based array
    End With
    '------------------------------------------------------------------------------------
    PageString = MyMatches(0)
    '====================================================================================
    '- STAGE 2 : EXTRACT DATA ONLY (remove tags etc.) = new PageString
    With MyRegExp
        .Global = True
        .ignorecase = True
 
        .Pattern = "<TR>|<TD>|</TD>|<TR>|</TR>|&#.*;|</TBODY>|</TABLE"< p>         PageString = .Replace(PageString, "")
    End With
    '====================================================================================
    '- STAGE 3 : DATA TO WORKSHEET
    '- Data Positions to array
    For n = 1 To (UBound(Arr1))
        Arr2(n) = InStr(1, PageString, Arr1(n), vbTextCompare)
    Next
    Arr2(UBound(Arr2)) = Len(PageString) + 1
    '------------------------------------------------------------------------------------
    MyCol = 2
    N1 = 1
    N2 = 2
    '------------------------------------------------------------------------------------
    While N1 <= UBound(Arr1)
        C1 = Arr2(N1)   ' first character
        If C1 <> 0 Then
            Do
                C2 = Arr2(N2)       ' next item start
                N2 = N2 + 1
            Loop While C2 = 0
            DataValue = Mid(PageString, C1, C2 - C1)
            DataValue = "'" _
                & Trim(Application.WorksheetFunction.Clean(Mid(PageString, C1 _
                + Len(Arr1(N1)), C2 - C1 - Len(Arr1(N1)))))
        Else
            DataValue = "n/a"
        End If
        '-------------------------------------------------------------------------------
        '- TO WORKSHEET
        ActiveSheet.Cells(MyRow, MyCol).Value = DataValue
        MyCol = MyCol + 1
        N1 = N1 + 1
        '-------------------------------------------------------------------------------
    Wend
End Sub
'==========  eop ========================================================================
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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