Hi all,
I`m trying to create a macro which will help me to save time at my work place.
The codes that I wrote so far are:
1. Insert rows based on specific value
Sub test()
Dim i, r As Range
Set r = Sheets("Main").Columns(28).SpecialCells(-4123, 1)
For i = r.Count To 1 Step -1
r(i).Offset(1).Resize(r(i).Value-1).EntireRow.Insert
With r(i).Offset(1, -27).Resize(r(i).Value)
.Clear
.Cells(0, 1).AutoFill .Cells(0, 1).Resize(r(i).Value + 1)
.Cells(0, 2).AutoFill .Cells(0, 2).Resize(r(i).Value + 1)
.Cells(0, 3).AutoFill .Cells(0, 3).Resize(r(i).Value + 1)
.Cells(0, 4).AutoFill .Cells(0, 4).Resize(r(i).Value + 1)
.Cells(0, 5).AutoFill .Cells(0, 5).Resize(r(i).Value + 1)
.Cells(0, 6).AutoFill .Cells(0, 6).Resize(r(i).Value + 1)
.Cells(0, 7).AutoFill .Cells(0, 7).Resize(r(i).Value + 1)
.Cells(0, 8).AutoFill .Cells(0, 8).Resize(r(i).Value + 1)
.Cells(0, 9).AutoFill .Cells(0, 9).Resize(r(i).Value + 1)
.Cells(0, 10).AutoFill .Cells(0, 10).Resize(r(i).Value + 1)
.Cells(0, 11).AutoFill .Cells(0, 11).Resize(r(i).Value + 1)
.Cells(0, 12).AutoFill .Cells(0, 12).Resize(r(i).Value + 1)
.Cells(0, 13).AutoFill .Cells(0, 13).Resize(r(i).Value + 1)
.Cells(0, 14).AutoFill .Cells(0, 14).Resize(r(i).Value + 1)
.Cells(0, 16).AutoFill .Cells(0, 16).Resize(r(i).Value + 1)
End With
Next
End Sub
The problems here are:
a) When I have value in range(r) = 0 the macro stops.
What i want is, when I have value in this range 0 to skip inserting row below it.
b) the information that is copying in the rows below the main row sholud be the same like the information in the main row.
2.Transfer data
Sub datatrans()
Dim k As Long
Dim i, j, n As Integer
k = Worksheets("Main").Cells(Rows.Count, 20).End(xlUp).Row
MsgBox (k)
For i = 1 To k
For j = 20 To 24
n = n + 1
Worksheets("Sub1").Cells(n, 1).Value = Worksheets("Main").Cells(i, j).Value
Next j
Next i
End Sub
3.Delete rows and transfer data
Sub deleterows()
Dim rng As Long
Dim rng1 As Range
rng = Worksheets("Sub1").Cells(Rows.Count, 1).End(xlUp).Row
For i = rng To 1 Step -1
If Cells(i, 1).Value = " " Or Cells(i, 1).Value = 0 Then
Rows(i).EntireRow.Delete
End If
Next i
Set rng1 = Worksheets("Sub1").Range("A1:A" & Worksheets("Sub1").Cells(Rows.Count, 1).End(xlUp).Row)
Worksheets("Main").Range("O:O").ClearContents
Worksheets("Main").Range("O1:O" & rng1.Rows.Count).Offset(1, 0).Value = rng1.Value
End Sub
So there is another question. Is it possible to combine all of these 3 macros in 1.
I tried to do it with macro datatrans and delete rows but:
For i = rng To 1 Step -1
If Cells(i, 1).Value = " " Or Cells(i, 1).Value = 0 Then
Rows(i).EntireRow.Delete
End If
this part of the macro did not work.
If someone help me to do what I want.
Thank you in advance
Here is my example:
Bookmarks