VBA code to look in range and copy if value is below something

CV899000

Board Regular
Joined
Feb 11, 2016
Messages
98
I am trying to get a VBA code to work.

I have a sheet with a coloumn (A) with a header called: "Documents - close to deadline"

I then have

Coloumn C is "weeks until deadline"

Coloumn D is "Document name".

I can have any number of documents (not predetermined) depending on the project or the state of the project.

What I want is for excel to automatically copy the "Document name" into "Documents - close to deadline" if the "weeks until deadline" is below 2.

I can write a code that does that:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("C2").Value < 2 Then
  Range("D2").Copy Range("A1").Offset(1, 0)
  End If
End Sub

My problem is that it does not update as I want it to.
Let me try to explain:

Situation 1:C2=2
C3=2
C4=4
C5=4

D2=Document1
D3=Document2
D4=Document4
D5=Document5

If I then change C2 to 1, D2 is copied to A2, and if I then change C3 to 1, D3 is copied to A3. That works fine.

Now, if I then delete the number in C2 to indicate that the document has been finished, I want the excel to notice that and make the content of A3 jump up to A2.

As it is now, if I delete the "1" in C2, then the "Document1" still remains in A2.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Do you mean if you clear a cell in column C, you want to delete across the row from A to D and shift the cells below up?

E.g. You clear C2, the code deletes A2:D2 and shifts the data below up
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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