Trying Post Document

gzell

New Member
Joined
Apr 20, 2019
Messages
37
I am researching my Great Great Grandfathers time during the Civil War. What started to be a simple Excel file has grown to a large file, 25000+ rows. Not what I planned for but the further I got in the research the more I expanded to included all members of his units. I am new to VBA and have am trying to learn the basics to complete this document. If this request is not allowed please let me know.

Flow
Search Column D for Cell "Rank:"
When found, insert row below this Cell
Also, insert text "Age:" in column D in new row
Insert row below Cell with "Age"
insert "Residence:" in column D of this row
Loop through Range of Spreadsheet
Repeat
Until end of document
End

This is a code I found online, to enter one line but it does not work as expected. Any help would be greatly appreciated.

Sub Insert_Row_Below_Blank()
Dim i As Range
Dim cell As Range

Set i = Range("D1:D25391")
For Each cell In i.Cells
If cell.Value = "Rank:" Then
cell.Offset(1).EntireRow.Insert

End If

Next
End Sub







Excel 2016 (Windows) 32 bit
ABCDE
1Document IDPerson_IDNameDetailsNotes
2xxxxxx
3xxxxxxService:
4xxxxxxLocation:
5xxxxxxRegiment:
6xxxxxxFunction:
7xxxxxxCompany:
8xxxxxxRank:
9xxxxxxEnrolled:
10xxxxxxDate:
11xxxxxxEnlisted:
12xxxxxxDate:
13xxxxxxDetail:
14xxxxxxCaptured:
15xxxxxxPrisoner:
16xxxxxxDied:
17xxxxxxCause:
18xxxxxxBuried:
19mmmm
20mmmmService:
21mmmmLocation:
22mmmmRegiment:
23mmmmFunction:
24mmmmCompany:
25mmmmRank:
26mmmmEnrolled:
27mmmmDate:
28mmmmEnlisted:
29mmmmDate:
30mmmmDetail:
31mmmmCaptured:
32mmmmPrisoner:
33mmmmExchanged:
34mmmmLocation:
35mmmmBattle:
36mmmmDate:
37mmmmDetail:
38mmmmLocation:
39mmmmDate:
40mmmmCaptured:
41mmmmForwarded:
42mmmmDate:
43mmmmDate:
44mmmmLocation
45mmmmDied:
46mmmmCause:
47mmmmBuried:

<tbody>
</tbody>
Sheet1

 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this...UNTESTED

Code:
Sub MM1()
Dim r As Long, lr As Long
lr = Cells(Rows.Count, "D").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("D" & r).Value = "Rank:" Then
        Rows(r + 1).Resize(2).Insert
        Range("D" & r + 1).Value = "Age:"
        Range("D" & r + 2).Value = "Residence:"
    End If
Next r
End Sub
 
Upvote 0
When looping through a range and either inserting or deleting rows, it is always better to work from the bottom up (like Michael shows). Otherwise, you are changing the size of the range that you haven't checked yet, and some rows could get missed.
Thanks for the response but it isn't working.
Please explain exactly how it is not working. What is happening?
 
Upvote 0
It doesn't do anything. I have created a new test sheet and nothing happens. I am searching to make sure it isn't the worksheet. Thanks for the response.
 
Upvote 0
Two things to check:

1. Are these values in column D?
2. Is the value you are looking for EXACTLY equal to "Rank:"?
If there is even an extra space anywhere in there, it won't match, and the code won't do anything.

An easy way to check is to identify some cell that you think matches. Let's say it is D10.
Then enter the following in any blank cell:
=D10="Rank:"
If that returns FALSE, then they do not really match, there is something extra in there.
 
Upvote 0
How about
Code:
Sub gzell()
   Dim Ar As Areas
   Dim Rng As Range
   With Range("D2", Range("D" & Rows.count).End(xlUp))
      .Replace "Rank:", "=xxxRank", xlPart, , False, , False, False
      Set Ar = .SpecialCells(xlFormulas, xlErrors).Areas
      .Replace "=xxxRank", "Rank:", xlPart, , False, , False, False
   End With
   For Each Rng In Ar
      Rng.Offset(1).EntireRow.Resize(2).Insert
      Rng.Offset(1).Resize(2).Value = Application.Transpose(Array("Age:", "Residence:"))
   Next Rng
End Sub
 
Upvote 0
Thanks. I did a quick run on a practice sheet and it looks good. I will do a thorough run tonight when i get back home.

thanks to both of you for the response and help. I greatly appreciate your help.

Glen
 
Upvote 0
You're welcome, let us know how it goes.
 
Upvote 0
It works perfect until it reaches row 96 it then goes to End Sub. I stepped through the process in debug mode and Row 97 and placing Residence in the cell it went to End. Any thoughts?
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top