+ Reply to Thread
Results 1 to 6 of 6

VB Code to enter cell content within VB code

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    VB Code to enter cell content within VB code

    I am using below code to go to certain website.
    In the below code i need to make Address part to be variable.
    So assume i have http://www.google.com in cell C4, I need the content of Cell C4 inserted in Address:=
    so that i dont have to create seperate Sub for each Website, just need to enter the website address in excel.
    Sub WebSite1()
    ThisWorkbook.FollowHyperlink Address:="http://www.google.com", NewWindow:=True
    End Sub
    
    Sub WebSite2()
    ThisWorkbook.FollowHyperlink Address:="http://www.Yahoo.com", NewWindow:=True
    End Sub
    
    Sub WebSite3()
    ThisWorkbook.FollowHyperlink Address:="http://www.CNN.com", NewWindow:=True
    End Sub
    Please let me know if you have any questions.
    Thanks.

    Riz
    Attached Files Attached Files
    Last edited by rizmomin; 01-21-2017 at 05:42 PM. Reason: Wrap

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: VB Code to enter cell content within VB code

    .
    Using this code :

    Option Explicit
    
    Sub WebSite1()
    Dim UReL As String
    UReL = Range("C4").Value
    ThisWorkbook.FollowHyperlink Address:=UReL, NewWindow:=True
    End Sub
    
    Sub WebSite2()
    Dim UReL As String
    UReL = Range("C5").Value
    ThisWorkbook.FollowHyperlink Address:=UReL, NewWindow:=True
    End Sub
    
    Sub WebSite3()
    Dim UReL As String
    UReL = Range("C6").Value
    ThisWorkbook.FollowHyperlink Address:=UReL, NewWindow:=True
    End Sub
    Attached Files Attached Files

  3. #3
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to enter cell content within VB code

    Hello Logit:

    There is small misunderstanding.
    Rather then having lots of individual code for each web site, i need to have one unique code where code needs to pick up the web address from cell C4,C5,C6 etc....

    Sub WebSite1()
    ThisWorkbook.FollowHyperlink Address:="http://www.google.com", NewWindow:=True
    End Sub
    In above address will be pick up from C4 when button at B4 is pressed
    If i press Botton at B5 then adreess needs to picked up from cell C5 and so on.
    I hope this is clear and let me know if any questions.

    Riz

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: VB Code to enter cell content within VB code

    .
    Not certain I understand what you seeking.

    In the below code i need to make Address part to be variable.
    URel is a variable.

    You will always have to 'tell' Excel what the URL is. Either as you have it now with a hyperlink in the cell or a line of code in VBA.

    Guess you could create a "IF THEN" scenario and reference the command buttons on the sheet.

    Something like (NOTE THIS IS NOT THE ACTUAL SYNTAX) :

    If CommandButton1 Then
    
         Address:="http://www.google.com", NewWindow:=True
    
    ElseIf CommandButton2 Then
    
         Address:="http://www.Yahoo.com", NewWindow:=True
    
    ElseIf CommandButton3 Then
    
         Address:="http://www.CNN.com", NewWindow:=True
    
    End If

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: VB Code to enter cell content within VB code

    .
    Place this code in the Sheet1 VBE :

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim UReL1, UReL2, UReL3 As String
    UReL1 = Range("C4").Value
    UReL2 = Range("C5").Value
    UReL3 = Range("C6").Value
    
        If Button1_Click = True Then
        
            ThisWorkbook.FollowHyperlink Address:=UReL1, NewWindow:=True
            
        ElseIf Button2_Click = True Then
            
            ThisWorkbook.FollowHyperlink Address:=UReL2, NewWindow:=True
            
        ElseIf Button3_Click = True Then
        
            ThisWorkbook.FollowHyperlink Address:=UReL2, NewWindow:=True
    
        End If
        
    End Sub

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VB Code to enter cell content within VB code

    Sub WebSite1()
    ThisWorkbook.FollowHyperlink Address:=Cells(4, 3), NewWindow:=True
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Split cell's content by using VBA code
    By Tapyr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2016, 09:37 AM
  2. VBA Code to delete Row on Content of one Cell
    By Jes1397 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-08-2015, 06:29 PM
  3. [SOLVED] VB Code to check the content in cell
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-23-2014, 11:06 AM
  4. Cell content + Macro code
    By frank35 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-12-2013, 12:07 PM
  5. VBA -- pouse code to enter data in a worksheet, and then resume code
    By jcm4ccc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-26-2013, 12:59 PM
  6. [SOLVED] what code can delete cell content
    By colwyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2008, 08:19 AM
  7. Code in cell but need to do Alt-Enter and put more code
    By comptechbranden in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-07-2007, 12:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1