<font face=Courier New><br><br><br><br><SPAN style="color:#00007F">Sub</SPAN> Insert_Rows()<br> <SPAN style="color:#00007F">Dim</SPAN> LR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br> Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br> LR = Range("A" & Rows.Count).End(xlUp).Row<br> <SPAN style="color:#00007F">For</SPAN> r = LR <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1<br> <SPAN style="color:#00007F">If</SPAN> Len(Range("A" & r).Value) > 0 <SPAN style="color:#00007F">Then</SPAN><br> Rows(r).Insert<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#00007F">Next</SPAN> r<br> Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
Call Insert_Rows
End If
End Sub
Sub Insert_Rows()
Dim LR As Long, r As Long
Application.ScreenUpdating = False
LR = Range("A" & Rows.count).End(xlUp).Row
For r = LR To 1 Step -1
If Len(Range("A" & r).Value) > 0 Then
Rows(r).Insert
End If
Next r
Application.ScreenUpdating = True
End Sub
20220310_data_format.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Input type | Material Flow - refer to the sheet Figure | Output/Select | Did you buy this material or produce it on your own? | |||
2 | Activity | Category | Text | Flow | Origin | ||
3 | jui | Energy | |||||
4 | |||||||
5 | Transport | ||||||
6 | |||||||
7 | |||||||
8 | gyhu | Energy | |||||
9 | |||||||
10 | Transport | ||||||
11 | |||||||
12 | |||||||
13 | |||||||
Test_2 (2) |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
D8:D11 | List | =F_hide!A36# |
E8:E11 | List | =F_hide!$A$4:$A$5 |
D3:D6 | List | =F_hide!A29# |
E3:E6 | List | =F_hide!$A$4:$A$5 |
Are you only interested in column C?What I want to achieve is that every time a cell in column C is filled
Is the code supposed to check/enforce nothing in that row or are you assuming the user(s) will not enter there anyway?this applies only to those rows colored since e.g. row 7 has to remain blank.
Are you only interested in column C?
not really neededWhat is supposed to happen if C3 gets data and 'energy' becomes 3 rows and then C3 is cleared leaving 'energy' with 3 blank rows?
Since the person has to fill all the cols after entering the C one my assumption is that it is enough to look only into it, but maybe since the use case in which he might fill and delete the C col a few times exists as you pointed out and every time adding or adding/deleting the row might just mess things up safer to look at more cells in the same row i.e. add the row when he fills x cells in the same row (which should also avoid the issue of second point)Are you only interested in column C?
What is supposed to happen if C3 gets data and 'energy' becomes 3 rows and then C3 is cleared leaving 'energy' with 3 blank rows?
This is not an issue.What happens if C4 is filled in before C3?
For now I am assuming user will not enter anything, franky I would like to enforce the user not to put anything but in this minisheet there is only one blank row but there can be several since user can insert as many activities as he whishes of different size (i.e rows)Is the code supposed to check/enforce nothing in that row or are you assuming the user(s) will not enter there anyway?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Set c = Intersect(Target, Columns("C"))
If Not c Is Nothing Then
If c.CountLarge = 1 Then
If Len(c.Value) > 0 Then Rows(c.Row + 1).Insert
End If
End If
End Sub