p = p + 1 is not working?

Darkcloud617

New Member
Joined
Sep 7, 2017
Messages
38
Hopefully a quick questions. The code below works like I need it to, however, it does not seem to copy the row to the other sheet. In fact... excel seems to not do anything about the p = p + 1. I am sure its an easy mistake made but I cannot seem to find the problem. Any ideas on why that may not be happening? Very much appreciated.


Code:
Sub Pending(): Dim wp As Worksheet, wa As Worksheet, a As Long, p As String
Set wp = Sheets("Pending"): Set wa = Sheets("Projects"):
p = Application.Max(3, wp.Range("A" & Rows.Count).End(xlUp).Row + 1)
For a = 5 To wa.UsedRange.Rows.Count


If wa.Range("Q" & a) = "Move" Then
wa.Range("A" & a).Resize(1, 19).Value = wa.Range("A" & p).Resize(1, 19).Value
wp.Range("A" & p) = "": p = p + 1:


End If
Next a
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You declared p as a string, not a number. So you cannot do mathematical computations on it:
Code:
p As String
 
Upvote 0
Should this line be
Code:
wa.Range("A" & a).Resize(1, 19).Value = [COLOR=#ff0000]wp[/COLOR].Range("A" & p).Resize(1, 19).Value
 
Upvote 0
Thank you for the quick feedback.

I defined p as integer and changed that line per your request Fluff but it still seems to have the same issue. For some reason it refuses to copy the row to the 'Pending' sheet. It still almost seems like excel cannot see the p = p + 1.

Code:
Sub Pending(): Dim wp As Worksheet, wa As Worksheet, a As Long, p As Integer
Set wp = Sheets("Pending"): Set wa = Sheets("Projects"):
p = Application.Max(3, wp.Range("A" & Rows.Count).End(xlUp).Row + 1)
For a = 5 To wa.UsedRange.Rows.Count


If wa.Range("Q" & a) = "Move" Then
wa.Range("A" & a).Resize(1, 19).Value = wp.Range("A" & p).Resize(1, 19).Value
wp.Range("A" & a) = "": p = p + 1:


End If
Next a
End Sub

it is supposed to:

1. Copy all cells on activated row
2. erase the cells
3. *paste copied cells to first blank row in 'Pending'

Thank you again, I have played more than I care to say trying to get this working.
 
Upvote 0
If you add this line
Code:
p = Application.Max(3, wp.Range("A" & Rows.Count).End(xlUp).Row + 1)
[COLOR=#ff0000]MsgBox p[/COLOR]
For a = 5 To wa.UsedRange.Rows.Count
what does the msgbox return?
 
Upvote 0
It seems to return "3" every time.

Odd, I could have swore the first time I ran that it returned "8" :confused:. Maybe my eyesight is going bad.
 
Upvote 0
Ok, first you are trying to copy to the wrong sheet. Try
Code:
Sub Pending()
   Dim wp As Worksheet, wa As Worksheet, a As Long, p As Integer
   Set wp = Sheets("New"): Set wa = Sheets("pcode"):
   p = Application.Max(3, wp.Range("A" & Rows.Count).End(xlUp).Row + 1)
   For a = 5 To wa.UsedRange.Rows.Count
      If wa.Range("Q" & a) = "Move" Then
         wp.Range("A" & p).Resize(1, 19).Value = wa.Range("A" & a).Resize(1, 19).Value
         [COLOR=#ff0000]wp.Range("A" & a) = ""[/COLOR]: p = p + 1:
      End If
   Next a
End Sub
Second, what are you trying to do with the part in red?
 
Upvote 0
That line is supposed to be searching 'wa' not 'wp'. I was playing before posting and forgot to change those values back, apologies. It is supposed to blank out the cells instead of deleting or cutting the row.

Code:
wp.Range("A" & a) = "": p = p + 1:

1. erase data in row in 'wa'
2. paste that row into 'wp' in first blank row
 
Last edited:
Upvote 0
Change that line to
Code:
wa.Rows(a) = ""
& try the code in post#7
 
Upvote 0
You are an absolute genius. It works perfect now.

Just a quick question... Why did the Code wp.Range("A" & a) = "" break the area for p = p + 1 ? If I am thinking about it correctly it should have just searched the range and moved on to p = p + 1.

Sorry if a silly question, I am starting to piece together how to code in VBA and issues like these throw me way off.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

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