Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2024, 05:01 AM
Dr. Rick Dr. Rick is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2010
Advanced Beginner
VBA/Macro to reverse order table lines
 
Join Date: Apr 2023
Location: Coastal N.C.
Posts: 32
Dr. Rick is on a distinguished road
Question VBA/Macro to reverse order table lines

I have a long (15 pages or so) table 6 or 7 columns wide that I simply need to reverse the order of the lines in the table. This seems like a simple task for which someone might have already written a macro (or could easily write) The process, I'm sure, would involve adding a column to the left hand side and using it to number the rows and then sorting on this column and deleting it. I don't necessarily want a column that simply numbers the lines when all is said and done.




Searching for this didn't seem to yield anything I could use. Let me know if I'm wrong.
Thanks.
R Joslin
Reply With Quote
  #2  
Old 11-21-2024, 12:42 PM
gmaxey gmaxey is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

You don't need a macro to do this.


1. Add your temporary column
2. Define the order you want the rows indexed
3. Sort the table on the temporary column
4. Remove the temporary table
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-22-2024, 06:43 AM
Dr. Rick Dr. Rick is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2010
Advanced Beginner
VBA/Macro to reverse order table lines
 
Join Date: Apr 2023
Location: Coastal N.C.
Posts: 32
Dr. Rick is on a distinguished road
Thumbs up

Quote:
Originally Posted by gmaxey View Post
You don't need a macro to do this.


1. Add your temporary column
2. Define the order you want the rows indexed
3. Sort the table on the temporary column
4. Remove the temporary table

It's true, Greg, that I might not NEED a macro for this, but isn't the whole purpose of macro to automate tasks? I'm NOT trying to sound whiny or snarky. I tried the above process and no doubt did something wrong and it didn't produce the results I wanted, so I'm taking the lazy way out and wanting someone else to give me a macro that will accomplish it.


Thanks, however, for confirming that I had the right idea.


I'm sure there must be a way I can do those 4 steps from the keyboard--I'm still trying to get over the fact that one can't include mouse clicks in macros. I was sure I had done so in the distant past when recording a macro, but I was told authoritatively that this probably was not so.


Thanks, anyway. I'll try the hammer and tongs method again and see what happens.
Good day.
RJ
Reply With Quote
  #4  
Old 11-22-2024, 11:30 AM
gmaxey gmaxey is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oTbl As Table
Dim lngIndex As Long
  Set oTbl = Selection.Tables(1)
  oTbl.Columns.Add
  For lngIndex = 1 To oTbl.Rows.count
    oTbl.Cell(lngIndex, oTbl.Columns.count).Range.Text = lngIndex
  Next lngIndex
  oTbl.Sort False, oTbl.Columns.count, , wdSortOrderDescending
  oTbl.Columns(oTbl.Columns.count).Delete
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-23-2024, 05:45 AM
Dr. Rick Dr. Rick is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2010
Advanced Beginner
VBA/Macro to reverse order table lines
 
Join Date: Apr 2023
Location: Coastal N.C.
Posts: 32
Dr. Rick is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oTbl As Table
Dim lngIndex As Long
  Set oTbl = Selection.Tables(1)
  oTbl.Columns.Add
  For lngIndex = 1 To oTbl.Rows.count
    oTbl.Cell(lngIndex, oTbl.Columns.count).Range.Text = lngIndex
  Next lngIndex
  oTbl.Sort False, oTbl.Columns.count, , wdSortOrderDescending
  oTbl.Columns(oTbl.Columns.count).Delete
End Sub
Greg
I'm astounded that so small a macro could accomplish the task. I'll certainly try it. Am I right to assume I start with the table "selected"?


But...

Before I saw this reply I was planning to ask you the question below; I'm still trying to learn how to do it manually as you suggested.



As it turns out I already have a blank column (the first one) on the left of the table I’m try to reverse. Can I just put the numbers in that column and then sort on it, and later just “clear contents” of that column to get it back to blank again? Or even delete the column once I’m done sorting and then add a new blank column?

I was able to select the column and get numbers in each cell, but was stuck on how to sort the table based on the numbers in column 1.


Sorry to seem (or BE) so dense.
RJ
Reply With Quote
  #6  
Old 11-24-2024, 06:49 AM
gmaxey gmaxey is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Yes
Yes
Select the table. Table Layout>Sort Sort by Column 1 Ascending
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 11-25-2024, 01:40 PM
macropod's Avatar
macropod macropod is online now VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,379
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Here's an even more efficient version of Greg's macro - with error checking:
Code:
Sub TblSort()
Application.ScreenUpdating = False
Dim r As Long, c As Long
With Selection
  If .Information(wdWithInTable) = False Then Exit Sub
  With .Tables(1)
    .Columns.Add
    c = .Columns.Count
    For r = 1 To .Rows.Count
      .Cell(r, c).Range.Text = r
    Next
    .Sort False, c, , wdSortOrderDescending
    .Columns(c).Delete
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 11-27-2024, 08:18 AM
Dr. Rick Dr. Rick is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2010
Advanced Beginner
VBA/Macro to reverse order table lines
 
Join Date: Apr 2023
Location: Coastal N.C.
Posts: 32
Dr. Rick is on a distinguished road
Default

Thanks to Paul E.
I'll try your macro as well
Can you recommend a "book" (or similar digital item) that would be a good introduction to VBA for someone who (like me) has extensive history of BASIC programming? I think I'd enjoy doing some programming again.
Reply With Quote
  #9  
Old 11-27-2024, 01:53 PM
macropod's Avatar
macropod macropod is online now VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,379
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I don't have any VBA book recommendations - I've never read any books on VBA.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 12-06-2024, 07:32 AM
Dr. Rick Dr. Rick is offline VBA/Macro to reverse order table lines Windows 10 VBA/Macro to reverse order table lines Office 2010
Advanced Beginner
VBA/Macro to reverse order table lines
 
Join Date: Apr 2023
Location: Coastal N.C.
Posts: 32
Dr. Rick is on a distinguished road
Default

I constructed a small table to test your macro and it worked perfectly. Thanks.


I found that I could do it manually as well using your brief instructions, thanks again.
Reply With Quote
Reply

Tags
order of tables, reversing order

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA/Macro to reverse order table lines Print two pages per page but in reverse order hammad Word 10 06-20-2019 09:01 AM
reverse order of paragraphs, word 2010 moorea21 Word VBA 1 08-02-2018 03:47 AM
VBA/Macro to reverse order table lines Is There an Animation that Will Reverse the Order of a Bulleted List equity PowerPoint 3 04-04-2014 12:33 AM
VBA/Macro to reverse order table lines Reverse Order for Flashcards? bknollman3 PowerPoint 2 03-23-2013 06:34 AM
Reverse order of headings throughout Word my_vine_figtree Word VBA 1 08-17-2010 01:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:26 AM.


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