Excel VBA programming, after inserting a few lines in a table, inserts the row after the row the whole move down, does not destroy the original data content,

Source: Internet
Author: User

Range (Rows (3), rows (5)). Insert Shift:=xldown
1) Insert a row at the current cell  ; You can add a loop statement to insert multiple rows
Range ("A10"). Select
Selection.EntireRow.Insert, Copyorigin:=xlformatfromleftorabove

2) Insert the same number of rows at the current selection row as the number of rows to select, and change the line number to insert in different places.
Rows ("10:11"). Select
Selection.insert Shift:=xldown, Copyorigin:=xlformatfromleftorabove
3) Change range to Sheet1.cells (x, y) or range (cells (x1,y1), Cells (x2,y2)) to use the program to control where it is inserted

how does VBA or function in Excel automatically insert new data?

I will use the function, VBA know a little, ask: How in the Diagram when column C appears greater than 4.8 and less than 0.2, the next ZD9 automatically inserted. It's the k294+920 behind the picture.-0.363

Explains how the data is computed: The first ZD8 data is entered manually in line 9 and row 10. k294+880 and E Column 3174.967G column 3174.97 in column A are arranged in another worksheet and can be called directly. The C-column value behind him is called the front-view, c11= $D $10-e11,c12= $D $10-e12,c13= $D $10-e13

ZD9 back C14 is based on the k294+920 behind the 0.363 (less than 0.2), a random number of 0.2 to 0.5. If 7.652 (greater than 4.8) is present at the back of the k294+920, a number of 4.2 to 4.8 is random. E14=d10-c14 B15 is also a random number, which is based on the number of 0.363 (less than 0.2) that appears in the back of the k294+920 after a random 4.2 to 4.8. If 7.652 (greater than 4.8) is present at the back of the k294+920, a number of 0.2 to 0.5 is random. D15=e14+b15

The ZD9 is finished. Below the c16= $D $15-e16,c17= $D $15-e17,c18= $D $15-e18 is this, if the lower C column appears more than 4.8 or less than 0.2, and then insert the next point ZD10, so loop.

If no white, please contact QQ313616791 or phone 18048795658

That 7.652 is hypothetical. So I'll tell you exactly what to do: I want a few processes like this,
(1.) When column C appears less than 0.2 or greater than 4.8, insert two lines, which is called the turn point.
(2) The data in these two lines meet this requirement,
ZD9 that 9 = The 8+1 behind the previous ZD8, is the meaning of the next turn point. Obviously in the next should be zd10,zd11,zd12, ZD13 .... So, the ZD will start from ZD1.
Write a piece of code according to your figure data and requirements to see if it meets the requirements:
1, please back up the original data table before running, remember!
2, in order to facilitate inspection, will automatically write the formula in the relevant cells.
3. The code is placed directly in the code page of the "Leveling record" table of figure one, and if written in a module, modify the code yourself. After the code runs, it generates the appearance of Figure II, which can only be run once, and it must be guaranteed that the data format is the Tuyi (original table) format when running again. Because several data are random numbers, multiple test results will be different.
4, the default figure one e column is known data.

Sub Insert Data ()
Dim I as Long
Dim K as Long
Dim N as Long
n = 8
K = 10
With ActiveSheet
For i = one to 65536
If. Cells (k, 4). Value-. Cells (i, 5). Value >= 0.2 and. Cells (k, 4). Value-. Cells (i, 5). Value <= 4.8 Then
. Range ("C" & i) = "= $D $" & K & "-E" & I
Else
If. Cells (k, 4). Value-. Cells (i, 5). Value >= 4.8 Then
. Rows (i). Insert Shift:=xldown
. Range ("A" & i) = "ZD" & N + 1
. Range ("C" & i) = "=round (rand () * (4.8-4.2) + 4.2, 3)"
. Range ("E" & i) = "= $D $" & K & "-C" & I
. Rows (i + 1). Insert Shift:=xldown
. Range ("B" & i + 1) = "=round (rand () * (0.5-0.2) + 0.2, 3)"
. Range ("D" & i + 1) = "=e" & I & "+b" & i + 1
ElseIf. Cells (k, 4). Value-. Cells (i, 5). Value <= 0.2 Then
. Rows (i). Insert Shift:=xldown
. Range ("A" & i) = "ZD" & N + 1
. Range ("C" & i) = "=round (rand () * (0.5-0.2) + 0.2, 3)"
. Range ("E" & i) = "= $D $" & K & "-C" & I
. Rows (i + 1). Insert Shift:=xldown
. Range ("B" & i + 1) = "=round (rand () * (4.8-4.2) + 4.2, 3)"
. Range ("D" & i + 1) = "=e" & I & "+b" & i + 1
End If
K = i + 1
n = n + 1
i = i + 1
End If
If. Range ("E" & i + 1) = "Then Exit for
Next I
End with
End Sub
This is a good understanding, I repeat, you confirm. When a value less than 0.2 or greater than 4.8 appears in column C, two blank lines are inserted above the line, and the zd+ number is written in column A. All others are left blank.

Sub InsertRow ()
Dim LastRow as long, I as Long, J as Long
Application.cutcopymode = False
LastRow = Cells (Cells.Rows.Count, 3). End (3). Row
i = 1
Do
If IsNumeric (Cells (I, 3)) and Len (cells (I, 3)) > 0 Then
If cells (I, 3) < 0.2 Or Cells (i, 3) > 4.8 Then
Rows (I & ":" & i + 1). Insert Shift:=xldown
j = j + 1
Cells (i, 1) = "ZD" & J
i = i + 2
LastRow = Cells (Cells.Rows.Count, 3). End (3). Row
End If
End If
i = i + 1
Loop Until i > LastRow
End Sub



Excel VBA programming, after inserting a few lines in a table, inserts the row after the row the whole move down, does not destroy the original data content,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.