Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-06-2015, 12:29 PM
LearningMacro LearningMacro is offline Shift Excel Cell after executing a macro for second time Windows 7 64bit Shift Excel Cell after executing a macro for second time Office 2010 64bit
Novice
Shift Excel Cell after executing a macro for second time
 
Join Date: Jan 2015
Posts: 1
LearningMacro is on a distinguished road
Default Shift Excel Cell after executing a macro for second time

Hello All,

I've written a (below)macro that pulls data from the sql server 2008 r2. My issue is when the user runs the macro for first time by entering Jobnumber (say J0001) excel puts data on the spreadsheet starting from cell "A1" which is fine. The issue here is, when the user runs the macro for the second time by entering the jobnumber (say J0002), excel puts the data for Jobnumber (J0002) on cell "A1" and shifts the cells for J0001(first job) to cell "F" instead of moving down. How can I shift the previous entry down in the spreadsheet with the latest entry on top?

Here is my macro and attachment:



Code:
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
Attached Images
File Type: jpg Test2.jpg (29.9 KB, 14 views)
Reply With Quote
  #2  
Old 01-08-2015, 02:54 PM
charlesdh charlesdh is offline Shift Excel Cell after executing a macro for second time Windows 7 32bit Shift Excel Cell after executing a macro for second time Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

HI,

Have the file Destination go to another part of the sheet and then you copy the data to "A1" and have it insert the copied data and at the same time move the file down.
This is a recorded macro. But it may help.
Code:
Sub Macro3()
    Range("H1:H3").Copy
    Range("A1").Select
    Selection.Insert Shift:=xlDown
    Application.CutCopyMode = False
End Sub
Reply With Quote
  #3  
Old 01-08-2015, 03:05 PM
charlesdh charlesdh is offline Shift Excel Cell after executing a macro for second time Windows 7 32bit Shift Excel Cell after executing a macro for second time Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

Ok. Just waited my time. User found solution at:

http://www.vbforums.com/showthread.p...107&viewfull=1
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shift Excel Cell after executing a macro for second time Delete empty cell and shift up Linh Word Tables 1 09-21-2014 01:52 PM
Shift Excel Cell after executing a macro for second time Excel 2013 – need help extracting date and time from text cell plus more. Art Mann Excel 1 07-18-2014 09:55 PM
Can anyone here tweek this macro for renaming Excel files based on a cell's contents? chrisd2000 Excel Programming 6 07-01-2014 01:53 PM
Shift Excel Cell after executing a macro for second time Shift cell right gsrikanth Excel Programming 2 01-17-2012 11:29 PM
Shift Excel Cell after executing a macro for second time Cell formatting for time - Excel 2010 PomDave Excel 5 09-05-2011 10:49 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:13 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft