Insert blank row if cell value sequence is incorrect. e.g., if A, C, then insert row between them [for B]

gaelen

New Member
Joined
Jan 28, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,
I'm importing data from a MS-DOS era database txt file export. There are 17000 records with 15 possible data fields each. However, if a field wasn't populated in the DOS database, the export doesn't include a space for it in the export, which means the order of the fields vary, and therefore can't be transposed. One record might have A, B, C, D fields, and the next one has A, C, D (I.e., B is missing)

Does anyone know how I could accomplish something like "If a cell with the value A is not followed by a cell with the value B, insert blank row"?

Here is an example of the first four lines from two records:

TN A20 0015
CN A20 0015
TI World Congress fall celebration, 1991
G [videorecording]

and

TN A21 0001.2
CN A21 0001; AC2 177
ST Assembly
TI New Year's Continental Assembly

In this example, the first record is missing a field called "ST", which should be after CN. I'd like run a command that says "If CN is not followed by ST, insert blank row"
THANK YOU!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Here is a simpler way of representing the example:

TN
CN
TI
G

and

TN
CN
ST
TI
 
Upvote 0
Welcome to the MrExcel board!

Is TN A20 0015 all in one cell, two cells or three cells? (Suggest you investigate XL2BB for providing sample data)

Is there any blank line(s) between records?
 
Upvote 0
Welcome to the MrExcel board!

Is TN A20 0015 all in one cell, two cells or three cells? (Suggest you investigate XL2BB for providing sample data)

Is there any blank line(s) between records?
Thanks for replying Peter.
The current delimiter between records is $.
Here is some sample data. Please let me know if anything seems unclear.

Sample Data Fields Sequence.xlsx
ABCDEF
1Current Irregular sequence ($ is the record Delimiter)Desired Sequence What I'd like Column A to look like after formula or VBA
2TNTNTN
3CNCNCN
4TISTTI
5GSS
6SPTI
7ESOT
8DRGG
9PRSPSP
10PTESES
11PDDRDR
12CHPRPR
13CDPTPT
14RCPDPD
15DECHCH
16KCDCD
17MPRCRC
18$DEDE
19TNKK
20CNMPMP
21ST$
22SS
23TI
24OT
25G
26SP
27ES
28DR
29PR
30PT
31PD
32CH
33CD
34RC
35DE
36K
37MP
38$
39TN
40CN
41ST
42SS
43TI
44G
45SP
46ES
47DR
48PR
49PT
50PD
51CH
52CD
53RC
54DE
55K
56MP
57$
58TN
59CN
60ST
61SS
62TI
63OT
64G
65SP
66ES
67DR
68PR
69PT
70PD
71CH
72CD
73RC
74DE
75K
76MP
77$
78TN
79CN
80ST
81SS
82TI
83G
84SP
85ES
86DR
87PR
88PT
89PD
90CH
91CD
92RC
93DE
94K
95MP
96$
Sheet1
 
Upvote 0
Thanks for the XL2BB sample data.
Please let me know if anything seems unclear.
Yes, "What I'd like" in column F is unclear. Why isn't it like column G below instead? Or was "TI" in F4 instead of F6 a mistake?

Also, how many columns of data are there in each row with your real data?
Is there actually a heading row with the original data?
Rather than physically inserting rows where required, would it be acceptable to simply produce the data in another worksheet with the appropriate layout?

gaelen.xlsm
ABCDEFG
1CurrentDesiredWhat I'd like
2TNTNTNTN
3CNCNCNCN
4TISTTI
5GSS
6SPTITI
7ESOT
8DRGGG
9PRSPSPSP
10PTESESES
11PDDRDRDR
12CHPRPRPR
13CDPTPTPT
14RCPDPDPD
15DECHCHCH
16KCDCDCD
17MPRCRCRC
18$DEDEDE
19TNKKK
20CNMPMPMP
21ST$$
Sheet1
 
Last edited:
Upvote 0
Thanks for the XL2BB sample data.

Yes, "What I'd like" in column F is unclear. Why isn't it like column G below instead? Or was "TI" in F4 instead of F6 a mistake?

Also, how many columns of data are there in each row with your real data?
Is there actually a heading row with the original data?
Rather than physically inserting rows where required, would it be acceptable to simply produce the data in another worksheet with the appropriate layout?

gaelen.xlsm
ABCDEFG
1CurrentDesiredWhat I'd like
2TNTNTNTN
3CNCNCNCN
4TISTTI
5GSS
6SPTITI
7ESOT
8DRGGG
9PRSPSPSP
10PTESESES
11PDDRDRDR
12CHPRPRPR
13CDPTPTPT
14RCPDPDPD
15DECHCHCH
16KCDCDCD
17MPRCRCRC
18$DEDEDE
19TNKKK
20CNMPMPMP
21ST$$
Sheet1
Good catch - my mistake on F4 (I'm getting quite groggy from the hours of excel) : )
In my real data at the moment I have 1 column with 295K rows (which is a BIG improvement over how the data started). The final table there will be 19 columns with 17000 rows.

Regarding headers, the column I shared will be transposed and the two letter abbreviation data will become the header... ie, I'll split "TN A20 0015" with fixed delimiters for the first two characters, and then transpose the data.... my problem at the moment is that order of the data is inconsistent, so I can't just transpose the 295K rows... unless there is something I'm missing.

All that aside, thank you very much for your replies. I'm 99% done with the document - this is just the last hurdle and I'm fried : ) I'm all ears to any suggestions : )
 
Upvote 0
Good catch - my mistake on F4 (I'm getting quite groggy from the hours of excel) : )
In my real data at the moment I have 1 column with 295K rows (which is a BIG improvement over how the data started). The final table there will be 19 columns with 17000 rows.

Regarding headers, the column I shared will be transposed and the two letter abbreviation data will become the header... ie, I'll split "TN A20 0015" with fixed delimiters for the first two characters, and then transpose the data.... my problem at the moment is that order of the data is inconsistent, so I can't just transpose the 295K rows... unless there is something I'm missing.

All that aside, thank you very much for your replies. I'm 99% done with the document - this is just the last hurdle and I'm fried : ) I'm all ears to any suggestions : )
(when I wrote "the data is inconsistent" I mean the order of the data is inconsistent.... it's not as though they all say title>author>publisher>date (if they did I'd know what to do)... what I have is more like:

title> author>date
author>publisher>date

and I'm unsure of how to create blanks en masse where the empty missing fields should be.
 
Upvote 0
In my real data at the moment I have 1 column with 295K rows (which is a BIG improvement over how the data started). The final table there will be 19 columns with 17000 rows.
Now I am confused whether I am dealing with 1 column or 19?

Regarding headers, the column I shared will be transposed and the two letter abbreviation data will become the header... ie, I'll split "TN A20 0015"
I also do not understand that. Your sample data and expected results in post 4 have nothing to do with "TN A20 0015"

it's not as though they all say title>author>publisher>date
Again, that seems to have nothing to do with the earlier data & what you show us is all we know about your data.

So, lets start somewhere & do a little test.
I am assuming that there is a header row and that the "fields" start in row 2.
I am also assuming for now 2 columns of data.
So my original data looks like columns A:B below.

The following code has produced what you see in columns D:E
Is that headed in the right direction?

VBA Code:
Sub FixLayout()
  Dim a As Variant, b As Variant, Fields As Variant
  Dim i As Long, j As Long, f As Long, k As Long, uba2 As Long, ubf As Long
  
  Fields = Split("TN|CN|ST|SS|TI|OT|G|SP|ES|DR|PR|PT|PD|CH|CD|RC|DE|K|MP|$", "|")
  ubf = UBound(Fields)
  a = Range("A1").CurrentRegion
  uba2 = UBound(a, 2)
  ReDim b(1 To Rows.Count, 1 To uba2)
  f = -1
  For i = 2 To UBound(a)
    Do
      f = f + 1
      k = k + 1
    Loop Until a(i, 1) = Fields(f) Or f = ubf
    For j = 1 To uba2
      b(k, j) = a(i, j)
    Next j
    If f = ubf Then f = -1
  Next i
  Range("D2:E2").Resize(k).Value = b
End Sub

gaelen.xlsm
ABCDE
1Hdr1Hdr2
2TNaTNa
3CNbCNb
4TIc
5Gd
6SPeTIc
7ESf
8DRgGd
9PRhSPe
10PTiESf
11PDjDRg
12CHkPRh
13CDlPTi
14RCmPDj
15DEnCHk
16KoCDl
17MPpRCm
18$DEn
19TNrKo
20CNsMPp
21STt$
22SSuTNr
23TIvCNs
24OTwSTt
25GxSSu
26SPyTIv
27ESzOTw
28DRaaGx
29PRabSPy
30PTacESz
31PDadDRaa
32CHaePRab
33CDafPTac
34RCagPDad
35DEahCHae
36KaiCDaf
37MPajRCag
38$DEah
39TNalKai
40CNamMPaj
41STan$
42SSaoTNal
43TIapCNam
44GaqSTan
45SParSSao
46ESasTIap
47DRat
48PRauGaq
49PTavSPar
50PDawESas
51CHaxDRat
52CDayPRau
53RCazPTav
54DEaPDaw
55KbCHax
56MPcCDay
57$RCaz
58TNeDEa
59CNfKb
60STgMPc
61SSh$
62TIiTNe
63OTjCNf
64GkSTg
65SPlSSh
66ESmTIi
67DRnOTj
68PRoGk
69PTpSPl
70PDqESm
71CHrDRn
72CDsPRo
73RCtPTp
74DEuPDq
75KvCHr
76MPwCDs
77$RCt
78TNyDEu
79CNzKv
80STaaMPw
81SSab$
82TIacTNy
83GadCNz
84SPaeSTaa
85ESafSSab
86DRagTIac
87PRah
88PTaiGad
89PDajSPae
90CHakESaf
91CDalDRag
92RCamPRah
93DEanPTai
94KaoPDaj
95MPapCHak
96$CDal
97RCam
98DEan
99Kao
100MPap
101$
Sheet1
 
Upvote 0
Thanks again for your time, Peter - my apologies for the ambiguity. I figured out a manual workaround and was able sort the full set in about three hours. I can explain the workaround here, if you wish, but I doubt it will be useful for future readers.
All the best!
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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