G
Guest
Hello,
I some VBA code which I will post below. What I am trying to do is cut all
rows that meet the criteria of having the word "NONE" in the row and paste
that row onto a new worksheet that is created by the code. I am able to
create the new worksheet; go back to the existing sheet and sort it in
ascending order. The code will then run without errors and will cut all of
the rows containing "NONE" like I want it to; but when it pastes the just cut
row onto the new worksheet it keeps pasting it into the first row;
overwriting the data that was there. When the code is finished running all of
the rows that I want to have cut are gone from the existing sheet but the
only row I have pasted on the new sheet is the last row that was cut and
pasted. How can I get the code to move down one row each time it needs to
paste the most recent cut row? I have tried some things with the
ActiveCell.Next.Select method but when I add that in to the code I receive
errors. Any help with this will be greatly appreciated. Here is the code
below:
Private Sub cmdNoneTab_Click()
'Code below from the first Dim statement until the first End With statement
'will create a new Worksheet named Disbursement = NONE. If the command
'button is pushed unnecessarily a MsgBox will be displayed stating that a
'sheet with the same name has already been created.
Dim ws As Worksheet
Dim newSheetname As String
newSheetname = "Disbursement = NONE"
For Each ws In Worksheets
If ws.Name = newSheetname Or newSheetname = "" Or
IsNumeric(newSheetname) Then
MsgBox "Sheet already exists", vbInformation
Exit Sub
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
.Move After:=Worksheets(Worksheets.Count)
.Name = newSheetname
End With
Worksheets("InsuranceDataForLoanPortfolio").Activate 'Re-Activate the
InsuranceDataForLoanPortfolio
'Tab after creating the
new Worksheet.
Cells.Select 'Select all the cells on the active Worksheet
'Code in section below will sort column "S" in Ascending order.
Selection.Sort Key1:=Range("S2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
For Each Cell In Range("DisbMonthData") 'from here down is the trouble
If Cell.Value = "NONE" Then
Cell.EntireRow.Cut
Sheets("Disbursement = NONE").Select
ActiveSheet.Paste
End If
Next Cell
End Sub
Thank you for your assistance.
Dave Y
I some VBA code which I will post below. What I am trying to do is cut all
rows that meet the criteria of having the word "NONE" in the row and paste
that row onto a new worksheet that is created by the code. I am able to
create the new worksheet; go back to the existing sheet and sort it in
ascending order. The code will then run without errors and will cut all of
the rows containing "NONE" like I want it to; but when it pastes the just cut
row onto the new worksheet it keeps pasting it into the first row;
overwriting the data that was there. When the code is finished running all of
the rows that I want to have cut are gone from the existing sheet but the
only row I have pasted on the new sheet is the last row that was cut and
pasted. How can I get the code to move down one row each time it needs to
paste the most recent cut row? I have tried some things with the
ActiveCell.Next.Select method but when I add that in to the code I receive
errors. Any help with this will be greatly appreciated. Here is the code
below:
Private Sub cmdNoneTab_Click()
'Code below from the first Dim statement until the first End With statement
'will create a new Worksheet named Disbursement = NONE. If the command
'button is pushed unnecessarily a MsgBox will be displayed stating that a
'sheet with the same name has already been created.
Dim ws As Worksheet
Dim newSheetname As String
newSheetname = "Disbursement = NONE"
For Each ws In Worksheets
If ws.Name = newSheetname Or newSheetname = "" Or
IsNumeric(newSheetname) Then
MsgBox "Sheet already exists", vbInformation
Exit Sub
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
.Move After:=Worksheets(Worksheets.Count)
.Name = newSheetname
End With
Worksheets("InsuranceDataForLoanPortfolio").Activate 'Re-Activate the
InsuranceDataForLoanPortfolio
'Tab after creating the
new Worksheet.
Cells.Select 'Select all the cells on the active Worksheet
'Code in section below will sort column "S" in Ascending order.
Selection.Sort Key1:=Range("S2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
For Each Cell In Range("DisbMonthData") 'from here down is the trouble
If Cell.Value = "NONE" Then
Cell.EntireRow.Cut
Sheets("Disbursement = NONE").Select
ActiveSheet.Paste
End If
Next Cell
End Sub
Thank you for your assistance.
Dave Y