Consulting

Results 1 to 7 of 7

Thread: Shift Excel Cell after executing a macro

  1. #1

    Shift Excel Cell after executing a macro

    Hello All,

    I've written a (below)macro that pulls data from the sql server 2008 r2. My issue is when the user enters the Jobnumber for the first time excel puts data on the spreadsheet starting from "A1" cell. The issue here is, when the user runs the macro for the second time all the data is again put on spreadsheet starting from "A1" cell. How can I shift the previous entry down in the spreadsheet with the latest entry on top?

    Here is my macro:

    Sub Task()
    
    Dim sqlstring As String
    
    
    Dim connstring As String
    
    
    Dim Strcode As String
    
    
    Strcode = Trim(InputBox("Please enter a Job #", "Task history"))
    
    
    sqlstring = "select distinct m.JobNumber , cast(m.ExpectedDate as DATE) 'Ship Date' ,m.CustLongName 'Customer' & _
                 " from ArchiveJobHeader m  left join AuxiliaryInfoFile af (nolock) on af.jobnumber=m.jobnumber & _
                 " where m.JobNumber = '" & Trim(Strcode) & "'" & _
                 " order by 'Resulttime'"
    connstring = "ODBC;DSN=SQLDSN;UID=test;PWD=test123"
    
    
     Dim thisQT As QueryTable
    
    
     Set thisQT = ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("a1", "a1000"))
    
    
     thisQT.BackgroundQuery = False
    
    
     thisQT.Sql = sqlstring
    
    
    
    
    thisQT.Refresh
    
    
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    Sub Task()
    Dim thisQT As QueryTable
    Dim sqlstring As String
    Dim connstring As String
    Dim Strcode As String
    
        Strcode = Trim(InputBox("Please enter a Job #", "Task history"))
        
        sqlstring = "select distinct m.JobNumber , cast(m.ExpectedDate as DATE) 'Ship Date' ,m.CustLongName 'Customer'" & _
                     " from ArchiveJobHeader m  left join AuxiliaryInfoFile af (nolock) on af.jobnumber=m.jobnumber" & _
                     " where m.JobNumber = '" & Trim(Strcode) & "'" & _
                     " order by 'Resulttime'"
        connstring = "ODBC;DSN=SQLDSN;UID=test;PWD=test123"
    
        Rows(1).Insert
        Set thisQT = ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("a1", "a1000"))
    
        thisQT.BackgroundQuery = False
        thisQT.Sql = sqlstring
        thisQT.Refresh
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Hi xld,

    Thank you for your response. I tried the above code you suggested. When I run the macro second time (after cell "A1" has data for the first Job # say J0001) for Job # J0002, the entries for first Job # (J0001) are shifted to right side of the spreadsheet starting from column "J". I want the first job entry to move down instead of moving on right.

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    It's working fine for me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Hi xld,

    I've attached the screenshot of the spreadsheet. Please take a look at it.


    Attachment 12683

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    a) I can't see looking at a picture will help much

    b) I get an invalid link with that file.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Hi xld,

    Still stuck with this issue. Can you please help?

    Thanks

Posting Permissions

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