Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2019, 10:18 AM
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 How to I create a macro that dynamically creates a table, to enter questions, in the below format?

I’ve been attempting to format the following questions into a table (image shown), using a macro, in Microsoft Word. But the number of questions that the macro needs to be used on is always different. So it needs to be dynamic in that it reads the number of questions, then formats a table for them accordingly. I plead to the VBA guru's I know read this forum, I’ve been trying this a while now but can’t seem to find much on this subject area online, and so I don't know where to start.


Thank you in advance for any responce.




Reply With Quote
  #2  
Old 01-02-2019, 01:18 PM
macropod's Avatar
macropod macropod is offline How to I create a macro that dynamically creates a table, to enter questions, in the below format? Windows 7 64bit How to I create a macro that dynamically creates a table, to enter questions, in the below format? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

You don't need a macro for that. Instead, create a custom Quick Part for the rows you need for a sample question. Then, whenever you need to insert another question, simply insert the Quick Part at that point.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-02-2019, 01:42 PM
macropod's Avatar
macropod macropod is offline How to I create a macro that dynamically creates a table, to enter questions, in the below format? Windows 7 64bit How to I create a macro that dynamically creates a table, to enter questions, in the below format? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Cross-posted at: https://answers.microsoft.com/en-us/...6-7936e0d4781b
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-03-2019, 03:31 AM
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 macropod View Post
You don't need a macro for that. Instead, create a custom Quick Part for the rows you need for a sample question. Then, whenever you need to insert another question, simply insert the Quick Part at that point.

I'm sorry for the late reply. The problem is that the user(not necessarily me) receives the list of questions from a third party, rather than writing it ourselfs.I intend to add several features such a multiple choice answers in some of the blank cells in the table (I don't know if this helps, but gives perspective). Thank you for taking the time to answer.
Reply With Quote
  #5  
Old 01-03-2019, 03:33 AM
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 macropod View Post
I apologise for that, it's simply that the other forums didn't seem to be the right community to help me tackle this problem, so I migrated over here. I didn't mean to insult your efforts in any way.
Reply With Quote
  #6  
Old 01-03-2019, 08:53 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,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If each question consists of a single paragraph and there are no other paragraphs in the document (including empty paragraphs) then something like this mash of unorganized mess might do. I don't have time to refine:

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
    oCol.Add Left(oRng.Paragraphs(lngIndex).Range.Text, Len(oRng.Paragraphs(lngIndex).Range.Text) - 1)
  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
  oRngDup.Delete
  ActiveDocument.Paragraphs(1).Range.Delete
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 01-03-2019, 02:41 PM
macropod's Avatar
macropod macropod is offline How to I create a macro that dynamically creates a table, to enter questions, in the below format? Windows 7 64bit How to I create a macro that dynamically creates a table, to enter questions, in the below format? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by MathiasFC View Post
The problem is that the user(not necessarily me) receives the list of questions from a third party, rather than writing it ourselfs.I intend to add several features such a multiple choice answers in some of the blank cells in the table (I don't know if this helps, but gives perspective). Thank you for taking the time to answer.
The approach I suggested in post 2 works for what you described in post 1 - simply create a table for your 'sample question 1' with some standard text (including standard multiple-choice questions), then add it to a custom Quick Part in the template the document is based on. Provided your other users have access to that template, all they need do is insert that custom Quick Part into the document however many times is needed. If you prefer, you could even create an entire table as a custom Quick Part, so that your users can insert it once, then delete any rows they don't need.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 01-04-2019, 06:22 AM
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 macropod View Post
The approach I suggested in post 2 works for what you described in post 1 - simply create a table for your 'sample question 1' with some standard text (including standard multiple-choice questions), then add it to a custom Quick Part in the template the document is based on. Provided your other users have access to that template, all they need do is insert that custom Quick Part into the document however many times is needed. If you prefer, you could even create an entire table as a custom Quick Part, so that your users can insert it once, then delete any rows they don't need.

I'll try this out, thank you for tkaing the time
Reply With Quote
  #9  
Old 01-04-2019, 06:25 AM
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 each question consists of a single paragraph and there are no other paragraphs in the document (including empty paragraphs) then something like this mash of unorganized mess might do.
This is crazy, and almost exactly what I needed thank you so much. I'll try and refine it myself. If there were other paragraphs in the document, could there be a way to exclude these? eg, only carry out the macro on highlighted text? Again, thank you for taking the time.
Reply With Quote
  #10  
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,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
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
  #11  
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
  #12  
Old 01-14-2019, 04:50 PM
Guessed's Avatar
Guessed Guessed 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: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Mathias, I'm not sure of what your issue is exactly. Are you trying to run this macro multiple times in a single document or are you running it once? It looks like it was intended for a single run in a document.

It could be adjusted to be run on subsequent passes but would be done as a separate macro for ease of implementation.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
vba in microsoft word

Thread Tools
Display Modes


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 08:21 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