VBA selecting value from drop down box

PGD15

Board Regular
Joined
Aug 5, 2016
Messages
137
Hi,


I'm trying to use VBA to log me into a secure webpage, then navigate to a webpage where I need to select a value from a drop down box before searching the database.


I cannot get the last part where it selects the value in the drop down box to work, I have used the below code.


The drop down box Name is = District, text value is "South" and combo value for South is A in HTML code. Can someone please help (read a couple of other posts but didn't understand them).


Code:
        Sub database()


    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    
    'add worksheet
    Sheets.Add After:=ActiveSheet


    
    'destination
     Set destsheet = ActiveSheet
    'use internet explorer
     Set IE = CreateObject("InternetExplorer.application")
    ' with internet open, make this visable and go to webpage x, enter username 
    and passwork
    With IE
        .Visible = True
        .Navigate ("URL")
        While .Busy Or .ReadyState <> 4: DoEvents: Wend
    '.Document.getElementsbyname("User name").Focus
    .Document.getElementsByName("username")(0).Value = "username"
    .Document.getElementsByName("password")(0).Value = "Pword"
    While .Busy Or .ReadyState <> 4: DoEvents: Wend
    Set objCollection = IE.Document.getElementsByTagName("input")
    'log in (submit)
    i = 0
    While i < objCollection.Length
               If objCollection(i).Type = "submit" And _
               objCollection(i).Name = "" Then
                 ' "Search" button is found
                Set objElement = objCollection(i)
                
        End If
        i = i + 1
    Wend
    'upon logging in naviage to webpage...
    objElement.Click
    .Navigate ("URL 2")
   
        While .Busy Or .ReadyState <> 4: DoEvents: Wend
        Debug.Print .LocationURL
    End With


    With IE
    IE.doc.getElementsByName("district").Item(A).Selected = True
 
    End With
    End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
got it to work! don't need a reply, encase anyone has a further query similar to this its:

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">
Code:
[/FONT][/COLOR]</code>ie.[COLOR=#303336][FONT=inherit]document[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]querySelector[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Select[name="Name"] option[value="ObjectName"]"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Selected [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]True[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,825
Members
449,470
Latest member
Subhash Chand

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