Cut and Paste Data from One sheet to another

jfoBoston

New Member
Joined
Feb 8, 2018
Messages
4
I'm timing out when running the following (pressing escape debugs on the RED below):

'moving data from "Solicitation File" if the value in Column V is null - it is copying to the new worksheet, but never completes - stalls at "If CStr(xRg(K).Value) = "" Then" - data is getting to the new sheet, but the module hangs.

Sub MoveNoPhone()


With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "No Phone"

End With




Dim xRg As Range
Dim xCell As Range
Dim i As Long
Dim J As Long
Dim K As Long
i = Worksheets("Solicitation File").UsedRange.Rows.Count
J = Worksheets("No Phone").UsedRange.Rows.Count
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("No Phone").UsedRange) = 0 Then J = 0
End If
Set xRg = Worksheets("Solicitation File").Range("V2:V" & i)
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = "" Then
xRg(K).EntireRow.Copy Destination:=Worksheets("No Phone").Range("A" & J + 1)
xRg(K).EntireRow.Delete
If CStr(xRg(K).Value) = "" Then
K = K - 1
End If
J = J + 1
End If
Next

Cells.Select
Columns.EntireColumn.AutoFit
Cells.EntireRow.AutoFit

Application.ScreenUpdating = True


End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Maybe...
Code:
For K = 1 To xRg.Row.Count
If CStr(xRg.Rows(K)) = "" Then
 xRg.Row(K).EntireRow.Copy Destination:=Worksheets("No Phone").Range("A" & J + 1)
 xRg.Row(K).EntireRow.Delete
 If CStr(xRg.Rows(K)) = "" Then
HTH. Dave
ps. Welcome to the Board! Please use code tags
 
Last edited:
Upvote 0
Thanks Dave! I appreciate the quick response and the welcome.

I swapped out the code you edited, and it stalls on the line above the last one. I'll keep tinkering.
 
Upvote 0
The code is not hanging, it's just that you have written a permanent loop.
When deleting or inserting rows, you should always work from bottom up. Try
Code:
Sub MoveNoPhone()


With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Sheet5"

End With




Dim xRg As Range
Dim xCell As Range
Dim i As Long
Dim J As Long
Dim K As Long
i = Worksheets("Active").UsedRange.Rows.Count
J = Worksheets("sheet2").UsedRange.Rows.Count
If J = 1 Then
   If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0
End If
Set xRg = Worksheets("Active").Range("V2:V" & i)
Application.ScreenUpdating = False
For K = xRg.Count To 1 Step -1
   If CStr(xRg(K).Value) = "" Then
      xRg(K).EntireRow.Copy Destination:=Worksheets("sheet2").Range("A" & J + 1)
      xRg(K).EntireRow.Delete
      J = J + 1
   End If
Next

Cells.Select
Columns.EntireColumn.AutoFit
Cells.EntireRow.AutoFit

Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,346
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top