Hi,
I have created a macro script in MS excel for a button which will fetch some data from a different excel sheet over some conditions if matches. But the problem is that the order of the lines which is being extracted is in reverse. For example:
1.This should be line 5
2.This should be line 4
3.This should be line 3
4.This should be line 2
5.This should be line 1
and so on...
Here's the code that i have developed. Frankly speaking i'm not a programmer, i have just created it by the help of google help. Can anyone help me to make it in correct order. Thanks a ton in advance.
Sub CreateExtDep()
Sheets.Add.Name = "ExtDep"
Dim i As Long
Sheets("T&C Report").Select
For i = TargetRow(ActiveSheet, 1) - 1 To 2 Step -1
If UCase(ActiveSheet.Cells(i, 19).Value) = "BEC" Then
ActiveSheet.Rows(i).Copy Sheets("ExtDep").Cells(TargetRow(Sheets("ExtDep"), 1), 1)
' ActiveSheet.Rows(i).Delete
End If
Next i
Sheets("T&C Report").Select
Rows("1:4").Select
Selection.Copy
Sheets("ExtDep").Select
ActiveWindow.SmallScroll Down:=-6
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.PasteSpecial xlPasteColumnWidths
Columns("A:X").EntireColumn.AutoFit
Columns("F").EntireColumn.Delete
Rows("5:5").Select
ActiveWindow.FreezePanes = True
ActiveSheet.Move _
After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
Sheets("Summary").Select
End Sub
Function TargetRow(ByRef ws As Worksheet, ByVal col As Long) As Long
'returns index of first empty row from bottom of sheet
'requires worksheet object and column index
TargetRow = ws.Cells(Rows.Count, col).End(xlUp).Row
If IsEmpty(ws.Cells(TargetRow, col)) Then
'in this case the column is empty
'change targetrow to 2 to move the rows starting on the second row
TargetRow = 1
Else
TargetRow = TargetRow + 1
End If
End Function
Please help me out...
I have created a macro script in MS excel for a button which will fetch some data from a different excel sheet over some conditions if matches. But the problem is that the order of the lines which is being extracted is in reverse. For example:
1.This should be line 5
2.This should be line 4
3.This should be line 3
4.This should be line 2
5.This should be line 1
and so on...
Here's the code that i have developed. Frankly speaking i'm not a programmer, i have just created it by the help of google help. Can anyone help me to make it in correct order. Thanks a ton in advance.
Sub CreateExtDep()
Sheets.Add.Name = "ExtDep"
Dim i As Long
Sheets("T&C Report").Select
For i = TargetRow(ActiveSheet, 1) - 1 To 2 Step -1
If UCase(ActiveSheet.Cells(i, 19).Value) = "BEC" Then
ActiveSheet.Rows(i).Copy Sheets("ExtDep").Cells(TargetRow(Sheets("ExtDep"), 1), 1)
' ActiveSheet.Rows(i).Delete
End If
Next i
Sheets("T&C Report").Select
Rows("1:4").Select
Selection.Copy
Sheets("ExtDep").Select
ActiveWindow.SmallScroll Down:=-6
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.PasteSpecial xlPasteColumnWidths
Columns("A:X").EntireColumn.AutoFit
Columns("F").EntireColumn.Delete
Rows("5:5").Select
ActiveWindow.FreezePanes = True
ActiveSheet.Move _
After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
Sheets("Summary").Select
End Sub
Function TargetRow(ByRef ws As Worksheet, ByVal col As Long) As Long
'returns index of first empty row from bottom of sheet
'requires worksheet object and column index
TargetRow = ws.Cells(Rows.Count, col).End(xlUp).Row
If IsEmpty(ws.Cells(TargetRow, col)) Then
'in this case the column is empty
'change targetrow to 2 to move the rows starting on the second row
TargetRow = 1
Else
TargetRow = TargetRow + 1
End If
End Function
Please help me out...