Standardising the gap between blocks of text

chadski778

Active Member
Joined
Mar 14, 2010
Messages
297
I have two blocks of data in column A and they vary in size in each day (between A1 and A100) separated by empty rows. Could I please have code that will make the gap between the blocks always be 3 empty rows? I would like this automated so that I can fit it into another macro
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
chadski778,

What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:
 
Upvote 0
Sorry I should have said the first block of rows starts as A1 and can vary from A1:A5 up to A1:A32. The second block of rows of data starts at A37 and can extend anywhere up to A37:A60
 
Upvote 0
OK I will post them this evening because I cannot install the add-on using my work computer. I am using Excel 2007
 
Upvote 0
Excel Workbook
ABCDEF
1BEFORE
2
3Generic Name
4Highly refined mineral oil (IP346
5Highly refined mineral oil (IP346
6Highly refined mineral oil (IP346
7Highly refined mineral oil (IP346
8Highly refined mineral oil (IP346
9Polyolefin polyamine succinimide polyol
10Calcium alkaryl sulphonate
11Zinc dialkyldithiophosphate
12Olefin co-polymer
13Highly refined mineral oil (IP346
14Polyalkylmethacrylate
15Calcium alkaryl sulphonate
16Borated polyolefin polyamine succinimide
17Polyolefin polyamine succinimide molybdenum cmplx
18Polyalkarylamine
19Branched alkanes
20Alkyl methacrylate (as impurity)
21
22
23
24
25
26
27
28
29
30
31
32
33
34Risk phrase key
35N/C = Not classified as hazardous under EC Directives
36R41 = Risk of serious damage to eyes
37R53 = May cause long-term adverse effects in the aquatic environment
38R51/53 = Toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
39R43 = May cause sensitisation by skin contact
40R38 = Irritating to skin
41R65 = Harmful: may cause lung damage if swallowed
42R36/37/38 = Irritating to eyes, respiratory system and skin
43R50/53 = Very toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
44
45
46
47
48
CasDoc


Excel Workbook
ABCDEF
1AFTER
2
3Generic Name
4Highly refined mineral oil (IP346
5Highly refined mineral oil (IP346
6Highly refined mineral oil (IP346
7Highly refined mineral oil (IP346
8Highly refined mineral oil (IP346
9Polyolefin polyamine succinimide polyol
10Calcium alkaryl sulphonate
11Zinc dialkyldithiophosphate
12Olefin co-polymer
13Highly refined mineral oil (IP346
14Polyalkylmethacrylate
15Calcium alkaryl sulphonate
16Borated polyolefin polyamine succinimide
17Polyolefin polyamine succinimide molybdenum cmplx
18Polyalkarylamine
19Branched alkanes
20Alkyl methacrylate (as impurity)
21
22
23Risk phrase key
24N/C = Not classified as hazardous under EC Directives
25R41 = Risk of serious damage to eyes
26R53 = May cause long-term adverse effects in the aquatic environment
27R51/53 = Toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
28R43 = May cause sensitisation by skin contact
29R38 = Irritating to skin
30R65 = Harmful: may cause lung damage if swallowed
31R36/37/38 = Irritating to eyes, respiratory system and skin
32R50/53 = Very toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
33
CasDoc


Note - 'Generic name' is in A1 not A3 like in the diagram and the first block of data can vary in size from 4 rows to anything up to 30
 
Upvote 0
chadski778,


For the macro to work correctly, there must be at least one blank row between the two groups in column A in worksheet CasDoc.


Sample raw data in worksheet CasDoc:


Excel Workbook
A
1Generic Name
2Highly refined mineral oil (IP346
3Highly refined mineral oil (IP346
4Highly refined mineral oil (IP346
5Highly refined mineral oil (IP346
6Highly refined mineral oil (IP346
7Polyolefin polyamine succinimide polyol
8Calcium alkaryl sulphonate
9Zinc dialkyldithiophosphate
10Olefin co-polymer
11Highly refined mineral oil (IP346
12Polyalkylmethacrylate
13Calcium alkaryl sulphonate
14Borated polyolefin polyamine succinimide
15Polyolefin polyamine succinimide molybdenum cmplx
16Polyalkarylamine
17Branched alkanes
18Alkyl methacrylate (as impurity)
19
20
21
22
23
24
25
26
27
28
29
30
31
32Risk phrase key
33N/C = Not classified as hazardous under EC Directives
34R41 = Risk of serious damage to eyes
35R53 = May cause long-term adverse effects in the aquatic environment
36R51/53 = Toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
37R43 = May cause sensitisation by skin contact
38R38 = Irritating to skin
39R65 = Harmful: may cause lung damage if swallowed
40R36/37/38 = Irritating to eyes, respiratory system and skin
41R50/53 = Very toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
42
CasDoc





After the macro:


Excel Workbook
A
1Generic Name
2Highly refined mineral oil (IP346
3Highly refined mineral oil (IP346
4Highly refined mineral oil (IP346
5Highly refined mineral oil (IP346
6Highly refined mineral oil (IP346
7Polyolefin polyamine succinimide polyol
8Calcium alkaryl sulphonate
9Zinc dialkyldithiophosphate
10Olefin co-polymer
11Highly refined mineral oil (IP346
12Polyalkylmethacrylate
13Calcium alkaryl sulphonate
14Borated polyolefin polyamine succinimide
15Polyolefin polyamine succinimide molybdenum cmplx
16Polyalkarylamine
17Branched alkanes
18Alkyl methacrylate (as impurity)
19
20
21
22Risk phrase key
23N/C = Not classified as hazardous under EC Directives
24R41 = Risk of serious damage to eyes
25R53 = May cause long-term adverse effects in the aquatic environment
26R51/53 = Toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
27R43 = May cause sensitisation by skin contact
28R38 = Irritating to skin
29R65 = Harmful: may cause lung damage if swallowed
30R36/37/38 = Irritating to eyes, respiratory system and skin
31R50/53 = Very toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
32
CasDoc





Sample raw data in worksheet CasDoc (with at least one blank row between the groups) before the macro:


Excel Workbook
A
1Generic Name
2Highly refined mineral oil (IP346
3Highly refined mineral oil (IP346
4Highly refined mineral oil (IP346
5Highly refined mineral oil (IP346
6Highly refined mineral oil (IP346
7Polyolefin polyamine succinimide polyol
8Calcium alkaryl sulphonate
9Zinc dialkyldithiophosphate
10Olefin co-polymer
11Highly refined mineral oil (IP346
12Polyalkylmethacrylate
13Calcium alkaryl sulphonate
14Borated polyolefin polyamine succinimide
15Polyolefin polyamine succinimide molybdenum cmplx
16Polyalkarylamine
17Branched alkanes
18Alkyl methacrylate (as impurity)
19
20Risk phrase key
21N/C = Not classified as hazardous under EC Directives
22R41 = Risk of serious damage to eyes
23R53 = May cause long-term adverse effects in the aquatic environment
24R51/53 = Toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
25R43 = May cause sensitisation by skin contact
26R38 = Irritating to skin
27R65 = Harmful: may cause lung damage if swallowed
28R36/37/38 = Irritating to eyes, respiratory system and skin
29R50/53 = Very toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
30
CasDoc





After the macro:


Excel Workbook
A
1Generic Name
2Highly refined mineral oil (IP346
3Highly refined mineral oil (IP346
4Highly refined mineral oil (IP346
5Highly refined mineral oil (IP346
6Highly refined mineral oil (IP346
7Polyolefin polyamine succinimide polyol
8Calcium alkaryl sulphonate
9Zinc dialkyldithiophosphate
10Olefin co-polymer
11Highly refined mineral oil (IP346
12Polyalkylmethacrylate
13Calcium alkaryl sulphonate
14Borated polyolefin polyamine succinimide
15Polyolefin polyamine succinimide molybdenum cmplx
16Polyalkarylamine
17Branched alkanes
18Alkyl methacrylate (as impurity)
19
20
21
22Risk phrase key
23N/C = Not classified as hazardous under EC Directives
24R41 = Risk of serious damage to eyes
25R53 = May cause long-term adverse effects in the aquatic environment
26R51/53 = Toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
27R43 = May cause sensitisation by skin contact
28R38 = Irritating to skin
29R65 = Harmful: may cause lung damage if swallowed
30R36/37/38 = Irritating to eyes, respiratory system and skin
31R50/53 = Very toxic to aquatic organisms, may cause long-term adverse effects in the aquatic environment
32
CasDoc





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ThreeRowGap()
' hiker95, 08/11/2011
' http://www.mrexcel.com/forum/showthread.php?t=570998
Dim ER As Long, NSR As Long
Worksheets("CasDoc").Activate
ER = Range("A1").End(xlDown).Row
NSR = Range("A" & ER).End(xlDown).Row
Select Case NSR - ER
  Case 2
    Rows(NSR).Resize(2).Insert
  Case 3
    Rows(NSR).Resize(1).Insert
  Case 4
    'do nothing
  Case Is > 4
    Rows(ER + 4 & ":" & NSR - 1).Delete
End Select
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the ThreeRowGap macro.
 
Upvote 0
The code below would promptly solve the problem:

Rich (BB code):
Option Explicit
Sub ThreeRowGap()
' akinrotimi, 08/11/2011
' http://www.mrexcel.com/forum/showthread.php?p=2824989#post2824989
 
    Application.ScreenUpdating = False
    Range("a3").End(xlDown).Offset(1, 0).Select
    ActiveCell.FormulaR1C1 = "qwerty123"
    Range("a3").End(xlDown).Offset(1, 0).Select
    ActiveCell.FormulaR1C1 = "qwerty123"
    Range("a3").End(xlDown).Offset(1, 0).Select
    ActiveCell.FormulaR1C1 = "qwerty123"
    Columns("a:a").Select
    Selection.TextToColumns Destination:=Range("a1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
    Columns("a:a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Columns("a:a").Select
    Selection.Replace What:="qwerty123", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("A1").Select
    Application.ScreenUpdating = False
End Sub
Let me know if you still have any issues.

Regards

Rotimi
 
Upvote 0
akinrotimi,

Interesting approach. One for the archives.

I have removed all the Select statements (the code will execute faster).


Code:
Sub ThreeRowGap_akinrotimi()
' akinrotimi, 08/11/2011
' http://www.mrexcel.com/forum/showthr...89#post2824989
Application.ScreenUpdating = False
Range("a3").End(xlDown).Offset(1, 0).Resize(3).FormulaR1C1 = "qwerty123"
With Columns(1)
  .TextToColumns Destination:=Range("a1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
  .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  .Replace What:="qwerty123", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
End With
Application.ScreenUpdating = False
End Sub
 
Upvote 0
Chadski,

I am happy the solution proffered works as stated.That was and remains Bill's vision and that is the reason I am here.

Some of us need to learn not to be superfluously aggressive,forward or froward as decorum is a minimal requirement in a forum like this.This is a simple case of six(6) and half-dozen.As they say,less is more!

Once again,thanks for appreciating the solution.

Regards

Rotimi
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

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