+ Reply to Thread
Results 1 to 4 of 4

Using a cell value as criteria in a data import script

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2008
    Posts
    3

    Using a cell value as criteria in a data import script

    Hi, I'm hoping someone will be able to help me with what is probabaly a straight forward task in VBA. I have little experience of macros other then to record 'Get external data' imports.

    I'm trying to amend this code so that the user can enter a Start Date into a cell C3 and then the data import gets info from a table where the TxnDate is >= to that value. I've tried a few things but I just keep getting error messages!

    This is what I’ve got to work with:

    
    Sub update()
    
    Dim StartDate As Date
    
    StartDate = Range("C3").Value
    
    Sheets("Sheet2").Select
    
    With ActiveSheet.QueryTables.Add(Connection:="ODBC;;;;" , _
    Destination:=Sheets("Sheet2").Range("A1"))
    .CommandText = Array( _
    "SELECT SMPRODTXNS.PRODUCTCODE, SMPRODTXNS.TXNNUMBER, SMPRODTXNS.LocationCode, SMPRODTXNS.BatchSerialNr, SMPRODTXNS.TypeOfPost, SMPRODTXNS.Account, SMPRODTXNS.TxnDate, SMPRODTXNS.Reference1, SMPRODTXNS" _
    , _
    ".Reference2, SMPRODTXNS.EachUnitFlag, SMPRODTXNS.SalesValue, SMPRODTXNS.CostValue, SMPRODTXNS.PeriodNr, SMPRODTXNS.QtyEach, SMPRODTXNS.QtyUnit, SMPRODTXNS.Year" & Chr(13) & "" & Chr(10) & "FROM SMPRODTXNS SMPRODTXNS" & Chr(13) & "" & Chr(10) & "")
    .Name = "Query"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub
    The date field is TxnDate. What do I need to do so that it will only get results after the date in cell C3?

    As previously mentioned, my VBA knowledge is limited at best so please go easy

    Thanks in advance

    Ric
    Last edited by Rick_104; 05-12-2008 at 05:02 AM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Rick_104

    Welcome to Exceltip forum

    Please take a couple of minutes and read the Forum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    05-12-2008
    Posts
    3
    Can anyone help?!

  4. #4
    Registered User
    Join Date
    05-12-2008
    Posts
    3
    anyone at all?

+ 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