Author Topic: Excel Macro Request  (Read 1342 times)

Offline PlzBreakMyCampaign

  • DnD Handbook Writer
  • ****
  • Posts: 1962
  • Immune to Critical Hits as a Fairness Elemental
    • View Profile
Excel Macro Request
« on: April 19, 2014, 05:36:20 PM »
Is anyone on the boards mildly comfortable with Excel macros? I'm not picky about the version since I have access to them all. Or even Open or Libre Office...

Context: I'm converting Zook's spell list into xls. I've gotten over the major formatting hurdle but there is still the issue of about 5000 rows that will need large, formulaic changes to the columns.

Example 1: Let's say I have spells with the information in the same format "Duration: x" in many rows usually in one column but often in another. We want all the cells in that row including "Duration: x" moved to the right so that the duration cell matches the other rows but no information is overwritten. It looks like so:

Cell 1     Cell 2     Duration: 1     Cell 4     Cell 5
Cell 1     Cell 2     Duration: 2     Cell 4     Cell 5
Cell 1     Duration: 3     Cell 3     Cell 4     Cell 5
Cell 1     Cell 2     Duration: 4     Cell 4     Cell 5

and obviously we want:

Cell 1     Cell 2     Duration: 1     Cell 4     Cell 5     Cell 6
Cell 1     Cell 2     Duration: 2     Cell 4     Cell 5     Cell 6
Cell 1     Empty    Duration: 3     old C3    old C4    Cell 5
Cell 1     Cell 2     Duration: 4     Cell 4     Cell 5     Cell 6

Offline Demelain

  • Hero Member
  • ***
  • Posts: 564
    • View Profile
Re: Excel Macro Request
« Reply #1 on: April 20, 2014, 12:23:57 PM »
It's been a while and I'd forgotten what a pain in the ass tracking down the documentation is, but I think this will work. I move to LibreOffice a while ago though, so you might want to test it on a small selection before running it over 5000 rows of spreadsheet. Included comments to explain what I think I'm doing.

Dim var As Range 'variable var is a dimension defined as the range; rename "var" to whatever you want if you care to
Set var = Range("B:B").Find("Duration: ") 'range is set to column B (assuming B is the duration column from your example, change to the appropriate column if this is wrong)
Do While Not var Is Nothing 'loop should execute until B is empty (the end of the data)
   var.Insert xlShiftToRight 'data is shifted to the right
   Set var = Range("B:B").FindNext 'move to the next cell in the range
Loop 'close loop

Should work in 2010
« Last Edit: April 20, 2014, 12:26:03 PM by Demelain »