1
What I want to do is to have a macro that will:
- Detect, on page ("Original") the value of a cell ($E8, a date)
- Go to another page ("Transfer"), (page name varies, but the appropriate page name comes up in "Original" $Z$1.)
- Look down the A column of "Transfer", which lists every Monday (dates range starts at A20, text above).
- Find the Monday before that $E8 date (so for $E8 = Sat 17th, it would find Mon 12th)
- Insert a row BENEATH that Monday row (so before the row that says Mon 19th)
- Erase that row (so the row goes Mon-12, blank, Mon-19
- Cut/Copy from ("Original $E8") the range A8:H8
- Go to the "Transfer" page
- Insert that A8:H8 selection into the row created at 5.
- Loop back and do the same thing for $E9 until all the info has been put into "Transfer".
The cells I've given are the right cells, the dates I've just made up (they vary for each account anyway).
Eric has very kindly provided me with a code that I have modified, which is as follows:
Public Sub do_stuff()
Dim date_to_look_for As String
Dim row As Integer
date_to_look_for = Range("'Original'!K8").Value
'^L: This is the cell that you are reading from. Ensure it is the MONDAY formula
row = 20
'^L: This is where the Transfer date values start
Do Until row = Range("'Transfer'!A1").End(xlDown).row + 1 'create our loop.
'Notice that the .end function will find the end of the data in a column
If Range("'Transfer'!A" & row).Value = date_to_look_for Then
'^L: Look for Original (X) Value specified above (make sure it's Monday).
Range("'Transfer'!" & row + 1 & ":" & row + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'^L: Once
Range("'Transfer'!A" & row + 1 & ":H" & row + 1).Value = Range("'Original'!A8:H8").Value
'^L:This is WHERE it will paste '^L: This is what will copy
Exit Sub 'no sense in running loop more if already found
End If
row = row + 1
Loop
'If code gets here then the date was never found! so tack to end of list
Dim endrow As Integer
endrow = Range("'Transfer'!A1").End(xlDown).row
Range("'Transfer'!A" & endrow & ":H" & endrow).Value =
Range("'Original'!A8:H8").Value
'^L: What is this?
End Sub
(The L: messages are my notes as I worked out what each section did - please feel free to correct me if I've misunderstood. The other green 'notes are Eric's and I'm not sure I understand those bits. I don't really need to though, as long as it works, but if you feel like educating me on coding please feel free :D)
My problem is now how to make it loop so it works its way down the original values (in this case the K column, so it goes to K9, K10, etc, and does the same thing? Also, can it CUT instead of COPY, and remove from Original sheet once transferred?
Thanks to everyone who assisted, you guys are great!
I don't understand why I can't get help in the first place, to be quite frank. Might as well try and cast a wider net to get some assistance. I can't code and I've spent days on this - I just cannot make it work. – Lauren – 2017-06-16T18:15:57.120
And I posted two existing codes. You said neither would work without elaborating why. Even though one occasionally does - I had hoped I could modify that. And when I showed the error that was causing the crash, you didn't assist. If you can't or wont help that's fine. But maybe someone else will. – Lauren – 2017-06-16T18:26:20.897
You responded here but not there. I could only assume you either didn't want to or couldn't help. When I try to run my co-workers macro, it crashes when it gets to: " If Not found Is Nothing Then". Excel freezes and if I don't ESC out of it, it crashes." @ScottCraner – Lauren – 2017-06-16T18:32:29.747
Lauren could you put up a screenshot of both the "Original" and "Transfer" worksheets? You can void the data, I would just like to see the sheet structure. I can help you out. – Nate – 2017-06-16T18:38:29.790
I just went through your old code and what you have posted here. Just some general notes that may help you figure out your problem. First of all Range("'Sheet1'!A2").value is a good way to reference cell values instead of having to select and then do all of the other business the macro recorder does. When you "goto" or "transfer", most can be done in a similar format as Range("'Sheet2'!A2").value = Range("'Sheet1'!A2").value. Since most of your post is about copying and pasting, using this method should clear up most of what you are doing.. just replace the sheet names, columns, and rows – Eric F – 2017-06-16T18:43:48.350
Thanks Eric, that's good to know! I'll have a play around and see if that works. Currently the code isn't working at all though so wish me luck :) – Lauren – 2017-06-16T18:48:06.383
@ScottCraner You're right. I'm sorry. – music2myear – 2017-06-16T18:55:34.363
What are you talking about? I posted two days ago and the response I got didn't work, so I tried to be clearer in my answer and reposted. I posted here as well hoping more people might assist. I gave the codes and the pictures when requested. You said they weren't going to work but didn't elaborate. – Lauren – 2017-06-16T19:03:16.493