Hello forum, I have a huge problem with Excel and I have no idea how to solve it. Hmm, how do I explain this? Someone is giving me data pulled from a server in an Excel format. I have a column that has a series of entries. For example:
Blank
Entry 1
Entry 2
Entry 3
Entry 4
Entry 5
However, sometimes there's an entry or two missing. So it'll look like this:
Blank
Entry 1
Entry 2
Entry 4
Entry 5
Is there anyway to tell Excel to say "Hey there's an entry missing here," and have it insert a row with the missing entry value? I do not work in Excel ever but people around here do, in case someone has some advice. I already asked the guy if he could just server me the data formatted correctly, and -- of course -- he can't/won't. Thanks in advanced!!
Last edited by fishnfrogs; 11-05-2009 at 06:04 PM. Reason: asked
Is the data as simple as that -- a single word followed by a single number?
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hi, it's more complicated than that -- I believe. I have every state in one column, then it has the group column that I'm having problems with. Then is one more column with a letter in it. After that there are a few more columns that have the actually data in them. So it really looks like this:
Alaska BLANK A 75 more data columns etc
Alaska GROUP01 A 74 more data columns etc
Alaska GROUP02 A 74 more data columns etc
I'm attaching a worksheet that might help explain my situation a little better. I pretty much need it to look at the 'Group' column and determine that there is a value missing and insert a row. If it could even fill in the values of the other columns that would be amazing. Thanks for your help!!
Again -- is the data in column B as simple as that? Or exactly like that: GROUP followed by a 2-digit number?
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
It's just like GROUPXX.![]()
Try this:
Adding a Macro to a Code ModuleCode:Sub GroupXX() Dim iRow As Long Dim n1 As Long Dim n2 As Long iRow = Cells(Rows.Count, "B").End(xlUp).Row Do With Cells(iRow, "B") If UCase(.Text) Like "GROUP##" Then n1 = Right(.Text, 2) If UCase(.Offset(-1).Text) Like "GROUP##" Then n2 = Right(.Offset(-1).Text, 2) If n2 < n1 - 1 Then Rows(iRow - 1).Copy Rows(iRow).Insert Cells(iRow, "B") = "GROUP" & Format(n1 - 1, "00") n1 = n1 - 1 iRow = iRow + 1 End If End If End If End With iRow = iRow - 1 Loop While iRow > 2 End Sub
1. Copy the code from the post
2. Press Alt+F11 to open the Visual Basic Editor (VBE)
3. From the menu bar in the VBE window, do Insert > Module
4. Paste the code in the window that opens
5. Close the VBE to return to Excel
Running a Macro
1. Do Alt+F8 to open the macro dialog
2. Select the macro name (here, GroupXX) from the dropdown list and press Run
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hi, thanks for that. It's mostly working now. However, can you think of a way to insert the rows after a cell in the column is blank and the next cell value is GROUP03? If not, I can live with this. You're a life saver. Thanks!!
Maybe if you post before and after examples, showing (in the after example) the inserted rows highlighted.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hmm...I haven't been able to figure anything out. Well, actually, your code worked perfectly. However, maybe to make things simpler, would you know of a way to insert a new row after a blank row and if the next row isn't GROUP00. That way I could run that script, then the script you gave me, and it should fill in all the missing values for me. I've tried using this:
But it inserts the row above the blank one. I wanted it to go below. Any suggestions are appreciated. Thanks again!!Code:Sub Test() Dim iRow As Long iRow = Cells(Rows.Count, "C").End(xlUp).Row Do With Cells(iRow, "C") If IsEmpty(Cells(iRow, "C")) Then If IsEmpty(Cells(iRow, "B")) = False Then If Cells(iRow + 1, "C").Value <> "GROUP00" Then Rows(iRow + 1).Copy Rows(iRow).Insert Cells(iRow, "E") = "0.00" Cells(iRow, "F") = "0.00" Cells(iRow, "G") = "0.00" Cells(iRow, "H") = "0.00" Cells(iRow, "I") = "0.00" Cells(iRow, "J") = "0.00" Cells(iRow, "K") = "0.00" Cells(iRow, "L") = "0.00" Cells(iRow, "C") = "GROUP00" iRow = iRow + 1 End If End If End If End With iRow = iRow - 1 Loop While iRow > 2 End Sub
Please provide before and after examples of what you want.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hi, here are the examples. Thanks!!
Hello fishnfrogs,
Here is another macro to add in the missing rows with the group and sequence numbers. I have run this on the data you posted and it works. Let me know if I missed anything. This is little more complex than Shg's code, so I haven't added comments to explain what is happening. The complexity makes it a little more difficult to understand, but also provides a lot of flexibility in string manipulation.
I see from your profile you're a Tar Heel. I grew up in in the western side of the state in Asheville. Where are you?
Macro Code
Code:Sub AddMissingRows() Dim Cell As Range Dim Cnt As Long Dim Group As String Dim I As Long Dim RegExp As Object Dim Rng As Range Dim RngEnd As Range Dim SeqX As Variant Dim State As String Dim Wks As Worksheet 'Worksheet that holds the data Set Wks = Worksheets("Sheet2") 'Start with "B2" to find the last data entry in column "B" Set Rng = Wks.Range("B2") Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp) Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd)) 'Create Regular Expression object for pattern matching, text search and text replace Set RegExp = CreateObject("VBScript.RegExp") RegExp.IgnoreCase = True RegExp.Pattern = "([a-z]+)(\d+)" 'Step through the data cells one at a time For Each Cell In Rng If Cell = "" Then Cnt = 0 State = Cell.Offset(0, -1) End If 'Check if the cell contains a alpha characters followed by numbers If RegExp.Test(Cell) Then 'Extract the group name Group = RegExp.Replace(Cell.Text, "$1") 'Extract the sequece number SeqX = Val(RegExp.Replace(Cell.Text, "$2")) 'Test if the sequence number If SeqX <> Cnt Then 'Add the appropriate number of rows if the sequence number is off For I = Cnt To SeqX - 1 Cell.EntireRow.Insert Shift:=xlDown Cell.Offset(-1, -1) = State Cell.Offset(-1, 0) = Group & Format(I, "00") Next I 'Extend the row by the number of new rows added Set Rng = Rng.Resize(RowSize:=Rng.Rows.Count + (SeqX - Cnt - 1)) End If Cnt = Cnt + 1 End If Next Cell 'Release the object reference and the memory it used Set RegExp = Nothing End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks