Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-04-2019, 06:37 AM
gmaxey gmaxey is offline How to I create a macro that dynamically creates a table, to enter questions, in the below format? Windows 10 How to I create a macro that dynamically creates a table, to enter questions, in the below format? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,622
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default


If you created a style "Questions" and applied it to each paragraph that was a question then something like this bigger mess might do:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 1/3/2019
Dim oCol As New Collection
Dim lngIndex As Long
Dim oTbl As Table
Dim oRng As Range, oRngDup As Range
  Set oRng = ActiveDocument.Range
  For lngIndex = 1 To oRng.Paragraphs.Count
    If oRng.Paragraphs(lngIndex).Style = "Questions" Then
      oCol.Add Left(oRng.Paragraphs(lngIndex).Range.Text, Len(oRng.Paragraphs(lngIndex).Range.Text) - 1)
    End If
  Next
  Set oRngDup = oRng.Duplicate
  oRng.Collapse wdCollapseEnd
  oRng.InsertBefore vbCr
  oRng.Collapse wdCollapseEnd
  oRngDup.End = oRng.Start
  Set oTbl = oRng.Tables.Add(oRng, 4, 2)
  With oTbl
    .Style = "Table Grid"
        For lngIndex = 1 To 4
      .Cell(lngIndex, 1).Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    Next lngIndex
    .Columns(1).Width = 18
    .AutoFitBehavior wdAutoFitWindow
    .Cell(1, 2).Range.Text = oCol.Item(1)
    For lngIndex = 2 To oCol.Count
      .Rows.Add
      .Rows.Last.Cells(2).Range.Text = oCol.Item(lngIndex)
      .Rows.Add
      .Rows.Add
      .Rows.Add
    Next
    For lngIndex = 4 To .Rows.Count Step 4
      .Cell(lngIndex, 1).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
    Next lngIndex
  End With
  For lngIndex = oRngDup.Paragraphs.Count To 1 Step -1
    If oRngDup.Paragraphs(lngIndex).Style = "Questions" Then
      oRngDup.Paragraphs(lngIndex).Range.Delete
    End If
  Next lngIndex
lbl_Exit:
  Exit Sub
 End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 01-13-2019, 04:35 PM
MathiasFC MathiasFC is offline How to I create a macro that dynamically creates a table, to enter questions, in the below format? Windows 10 How to I create a macro that dynamically creates a table, to enter questions, in the below format? Office 2016
Novice
How to I create a macro that dynamically creates a table, to enter questions, in the below format?
 
Join Date: Jan 2019
Posts: 12
MathiasFC is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
If you created a style "Questions" and applied it to each paragraph that was a question then something like this bigger mess might do:
I'm so sorry this is so late after your reply, I posted this right after, but not as a reply but rather a post, so you didn't see it (first time on a forum mistake) . This works perfectly, except that there are spaces between each line/question, which is resulting in the program creating an empty 4, every 4 rows. (This also happened in the original program but somehow only just caught it). Again, sorry for the delay, and thank you for taking the time.
With kind regards,
Mathias[/font]
Reply With Quote
Reply

Tags
vba in microsoft word



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to I create a macro that dynamically creates a table, to enter questions, in the below format? Legal Caption needs a way to dynamically create right-paren separator gilly1 Mail Merge 2 03-08-2017 08:39 AM
Create a ComboBox in a Form that creates a new document and autofills a textBox BlackGirlMagic Word VBA 5 02-13-2017 02:39 PM
How to I create a macro that dynamically creates a table, to enter questions, in the below format? Automatically Organize data in a sheet differently (in order to create a dynamically updated graph) carlos_cs Excel 3 05-04-2016 08:44 AM
Looking for code to create a macro that prompts user to enter text, then does a find/replace. sfvegas PowerPoint 0 01-08-2016 02:22 AM
How to I create a macro that dynamically creates a table, to enter questions, in the below format? Dynamically create documents MrRikkie Word VBA 1 10-12-2012 09:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:20 PM.


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