+ Reply to Thread
Results 1 to 3 of 3

Store variable in worksheet and use that variable in VBA code

  1. #1
    Registered User
    Join Date
    10-15-2011
    Location
    Manchester
    MS-Off Ver
    Excel 2007
    Posts
    49

    Talking Store variable in worksheet and use that variable in VBA code

    Hello,

    Hoping somebody can help.

    I have a bit of VBA code in my workbook which opens up an ODBC connection to a database.

    With ActiveSheet.ListObjects.Add(SourceType:=0, source:= _
    "ODBC;DSN=MYDATABASE;UserID=HAR;Password=HAR;", Destination:=Range("$A$1")).QueryTable......


    I have it all working but I want the "MYDATABASE", "USERID" and "Password" to be variables which use data on another sheet tab.

    for example.. on a connections sheet tab store them in B1, B2, B3


    I thought storing the variables would be something like this

    Dim UserID As String
    UserID = "sheets.("connections").range.("B1")"

    I'm novice in terms of understanding some of the code... I'm learning how code works as I piece bits together.

    So I'd like help to provide how I store the variable and then how to include it in the VBA code where I need it.

    Help would be much appreciated

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Store variable in worksheet and use that variable in VBA code

    Almost:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-15-2011
    Location
    Manchester
    MS-Off Ver
    Excel 2007
    Posts
    49

    Re: Store variable in worksheet and use that variable in VBA code

    Perfect.. although I had got the full stops wrong after sheets and after range

    UserID = sheets.("connections").range.("B1").value
    Password = sheets.("connections").range.("B2").value

    UserID = sheets("connections").range("B1").value
    Password = sheets("connections").range("B2").value

    Thanks for your help. Very useful

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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