|
-
Jan 6th, 2015, 09:08 AM
#1
Thread Starter
Junior Member
-
Jan 6th, 2015, 09:56 AM
#2
Re: Shift Excel Cell after executing a macro second time
There's probably a better approach than this, but would it be acceptable to move the existing table "out of the way" first, drop the new one into A1 (etc.), then move the previous one to the next available row below it?
-
Jan 6th, 2015, 11:01 AM
#3
Thread Starter
Junior Member
Re: Shift Excel Cell after executing a macro second time
Hi Vbfbryce,
Your approach will also work for me. Please help with this. Thanks.
-
Jan 6th, 2015, 11:35 AM
#4
Re: Shift Excel Cell after executing a macro second time
Ok, maybe something like this:
Code:
Sub moveData()
Dim ws As Worksheet
Dim nRows As Integer
Dim rngCopy As Range
Dim pasteRow As Integer
Set ws = ActiveSheet
nRows = ws.Range("a" & Rows.Count).End(xlUp).Row - 1 'how many rows are in existing table
Set rngCopy = ws.Range("a2:e" & nRows + 1) 'range of existing data to copy
rngCopy.Copy
ws.Range("a1000").PasteSpecial 'paste to A1000
Application.CutCopyMode = False
rngCopy.Clear 'clear original table
'bring in new query table, destination cell A1 **********
'
'********************************************************
pasteRow = ws.Range("a999").End(xlUp).Row + 1 'find first blank row after new table is brought in
ws.Range("a1000:e" & 1000 + nRows - 1).Copy 'copy moved dats
ws.Range("a" & pasteRow).PasteSpecial
Application.CutCopyMode = False
ws.Range("a1000:e" & 1000 + nRows - 1).Clear 'clear moved data
Set ws = Nothing
End Sub
-
Jan 6th, 2015, 02:40 PM
#5
Thread Starter
Junior Member
Re: Shift Excel Cell after executing a macro second time
Thank you for your response. Can you please help me integrate the code you suggested in my original macro? I'm new to macros. Thanks again.
-
Jan 6th, 2015, 03:09 PM
#6
Re: Shift Excel Cell after executing a macro second time
put this:
where I say "bring in new query table."
The first time, you'd run your sub. The second time, you'd run my sub, which would call yours "in the middle."
-
Jan 6th, 2015, 03:49 PM
#7
Thread Starter
Junior Member
Re: Shift Excel Cell after executing a macro second time
Hi vbfbryce,
I appreciate your response. I tried calling the sub as you suggested but it still moved existing data to the right. Attached is the screenshot. Please have a look at it.
Thanks
-
Jan 6th, 2015, 05:44 PM
#8
Re: Shift Excel Cell after executing a macro second time
Edit: Do not use this as I neglected to test it sufficiently. If the returned query results exceed the open space, it will push to the right.
I'll leave it here in case some finds something in it useful.
-------------------
My turn. 
Different approach, push the junk out of the way.
Code:
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim qt As QueryTable
Set qt = GetQueryTable(ws.Range("a1"))
If Not (qt Is Nothing) Then
Dim rows As Long
rows = qt.ResultRange.rows.CountLarge + 1
ws.rows(qt.ResultRange.row).Resize(rows).Insert Excel.xlShiftDown, Excel.xlFormatFromLeftOrAbove
End If
Call Task
End Sub
Public Function GetQueryTable(rng As Excel.Range) As QueryTable
Dim qt As QueryTable
Dim checkRow As Excel.Range
Set checkRow = rng.Worksheet.rows(rng.row)
For Each qt In rng.Worksheet.QueryTables
If Not (Application.Intersect(qt.ResultRange, checkRow) Is Nothing) Then
'range contains QueryTable
Set GetQueryTable = qt
Exit For
End If
Next
End Function
Last edited by TnTinMN; Jan 6th, 2015 at 06:12 PM.
-
Jan 6th, 2015, 09:19 PM
#9
Re: Shift Excel Cell after executing a macro second time
OK, I think I found a solution. It is similar to the post above, but the QueryTable is created created on a temporary WorkSheet.
Code:
Sub Test2()
Dim currentWS As Excel.Worksheet
Set currentWS = ActiveSheet
Application.ScreenUpdating = False
' add a temporary worksheet
Dim scratch As Excel.Worksheet
Set scratch = ActiveWorkbook.Worksheets.Add(Type:=Excel.XlSheetType.xlWorksheet, _
After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
Dim rng As Excel.Range
Set rng = scratch.Range("A1")
Call Task(rng)
Dim qt As QueryTable
Set qt = GetQueryTable(rng)
If Not (qt Is Nothing) Then
Dim rows As Long
rows = qt.ResultRange.rows.CountLarge + 1
currentWS.rows(1).Resize(rows).Insert Excel.xlShiftDown, Excel.xlFormatFromLeftOrAbove
' Application.CopyObjectsWithCells = True
qt.ResultRange.Copy ' Cut fails on Paste
currentWS.Range("a1").PasteSpecial xlPasteColumnWidths
currentWS.Range("a1").PasteSpecial xlPasteAll
qt.Delete
End If
Application.DisplayAlerts = False
scratch.Delete ' delete temp WS
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Public Function GetQueryTable(rng As Excel.Range) As QueryTable
Dim qt As QueryTable
Dim checkRow As Excel.Range
Set checkRow = rng.Worksheet.rows(rng.row)
For Each qt In rng.Worksheet.QueryTables
If Not (Application.Intersect(qt.ResultRange, checkRow) Is Nothing) Then
'range contains QueryTable
Set GetQueryTable = qt
Exit For
End If
Next
End Function
This code would require you to modify your Task method as follows:
Code:
Sub Task(rng As Excel.Range)
...
Set thisQT = rng.WorkSheet.QueryTables.Add(Connection:=connstring, Destination:=rng)
...
I think that if the query result set increases in size that you will have issues with overlap when the queries are updated (Refresh).
Last edited by TnTinMN; Jan 6th, 2015 at 09:22 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|