Forum Discussion
BrianP475
May 21, 2025Copper Contributor
Move up to next blank row after copy/paste from previous sheet.
Snowman got me rolling with code that does almost exactly what Im trying to do. After I hit end of day button Im trying to get the copied data to move up to the next blank cell in column B within a range. If I have any data in B2:B9,B11:B21,B23:B29 marked with a t next to it in column A and hit the end of day button only data in cells marked with that t are moved to the next sheet in the same cells they came from. What Ive noticed is only data in B2:B9 go to the exact same cell. B11:B21 are offset by 1, and B23:B29 are offset by 2. This is fine as the data is still moving. What I want it to do is for those ranges move up to the next blank cell in column B. So if I have data only in B4, and B6 I want that to transfer to next sheet in B2,B3. The same for the other 2 ranges. I also dont want it to overwrite anything that may already be in a cell on the next sheet. If I have "Tree" in B2 of the next sheet I want the data to go to B3,B4. Same for the other two ranges. I have tried xlUp, xlDn, and played with the code that was given to me by Snowman to try and make it work. Im not getting anywhere with this. I thought maybe if I create another macro that after I transfer the data would move everything up into blank cells then Id be okay with that also. Im not having any luck with that either. I even recorded a macro for copy/paste but that wont work if there is data in a cell already that I need to keep, and not be overwritten. I have attached a copy of the workbook. Hopefully this time it will allow it to be published with this query.
4 Replies
Sort By
- BrianP475Copper Contributor
Snowman you gave me exactly what I was asking for the first time. Im also trying to learn and I dont want to just ask for everything that I intend to do because I feel like that paints me as lazy. Especially since I am getting free code. I like getting a sense of accomplishment when I figure something out on my own but it was kicking my butt. I tried for a couple weeks then broke down and made another post. What I am doing is taking a daily/monthly planner for work and creating a digital one. So at the end of the day anything left undone I hit a button and transfer to the next day. On Friday's I hit a separate button to transfer to Monday. At the end of the month I was just going to retype into the next month. I also have a column for accomplishments that I want to transfer to a separate book to track what I did for that year to put on my review. I did manage to figure out how to transfer my meetings to an excel calendar so I can look at the calendar for any upcoming meetings. I really do appreciate your time and many thanks to you. I will place this code in my book probably tomorrow and try it out. Again thank you for the work you did. It will save me a lot of time.
- SnowMan55Bronze Contributor
No, you should not be trying to create a separate process to move data up to blank cells on the next worksheet.
"What Ive noticed is only data in B2:B9 go to the exact same cell…"
That's because I wrote the code as you seemed to want it (you wrote "paste them in the same cells on the next sheet…", with no mention of avoiding non-blank cells in the destination), where the destination sheet is pulling data from the source sheet. Your described requirement was not clear.But if the source sheet is (conditionally) pushing data to the next sheet, as I can see from this new post, but pushed from three separate source ranges to three separate destination ranges (also not mentioned in your earlier post). That requirement allows for simpler—though longer—code.
See the following.
Sub EndofDay() '---- Get references to this worksheet and the next. Dim wksSourceSheet As Worksheet Dim wksNextSheet As Worksheet ' Set wksSourceSheet = ActiveSheet On Error Resume Next Set wksNextSheet = ActiveWorkbook.Sheets(wksSourceSheet.Index + 1) On Error GoTo 0 ' -- Verify that there is a next worksheet. If wksNextSheet Is Nothing Then Call MsgBox("There is no next sheet in this workbook.") Exit Sub End If '---- Define variables used in each of the next three code blocks. Dim strRange As String Dim rngSourceCell As Range Dim rngColumnACell As Range Dim strColumnAValue As String Dim rngDestnCell As Range 'destination cell '---- Conditionally copy the cells starting from the top of the IMPERATIVE ' range thru the end of that range to successive blank cells in the ' same range in column B on the next worksheet. strRange = ("B2:B9") Set rngDestnCell = wksNextSheet.Range("B2") For Each rngSourceCell In wksSourceSheet.Range(strRange) Set rngColumnACell = rngSourceCell.Offset(0, 1 - rngSourceCell.Column) strColumnAValue = rngColumnACell.Value If InStr(1, strColumnAValue, "T", vbTextCompare) > 0 Then ' Note: As long as there are no T's to the left of the IMPORTANT and ' FOLLOW UPS special cells, no further selection logic is needed. ' -- Find the next empty cell in the destination column. Do Until rngDestnCell.Value = "" Set rngDestnCell = rngDestnCell.Offset(1, 0) Loop ' -- Copy the source cell to that cell. rngDestnCell.Value = rngSourceCell.Value '***You could add statements to copy other cell properties also. End If Next rngSourceCell '---- Conditionally copy the cells starting from the top of the IMPORTANT ' range thru the end of that range to successive blank cells in the ' same range in column B on the next worksheet. strRange = ("B11:B21") Set rngDestnCell = wksNextSheet.Range("B11") For Each rngSourceCell In wksSourceSheet.Range(strRange) Set rngColumnACell = rngSourceCell.Offset(0, 1 - rngSourceCell.Column) strColumnAValue = rngColumnACell.Value If InStr(1, strColumnAValue, "T", vbTextCompare) > 0 Then ' Note: As long as there are no T's to the left of the IMPORTANT and ' FOLLOW UPS special cells, no further selection logic is needed. ' -- Find the next empty cell in the destination column. Do Until rngDestnCell.Value = "" Set rngDestnCell = rngDestnCell.Offset(1, 0) Loop ' -- Copy the source cell to that cell. rngDestnCell.Value = rngSourceCell.Value '***You could add statements to copy other cell properties also. End If Next rngSourceCell '---- Conditionally copy the cells starting from the top of the FOLLOW UPS ' range thru the end of that range to successive blank cells in the ' same range in column B on the next worksheet. strRange = ("B23:B29") Set rngDestnCell = wksNextSheet.Range("B23") For Each rngSourceCell In wksSourceSheet.Range(strRange) Set rngColumnACell = rngSourceCell.Offset(0, 1 - rngSourceCell.Column) strColumnAValue = rngColumnACell.Value If InStr(1, strColumnAValue, "T", vbTextCompare) > 0 Then ' Note: As long as there are no T's to the left of the IMPORTANT and ' FOLLOW UPS special cells, no further selection logic is needed. ' -- Find the next empty cell in the destination column. Do Until rngDestnCell.Value = "" Set rngDestnCell = rngDestnCell.Offset(1, 0) Loop ' -- Copy the source cell to that cell. rngDestnCell.Value = rngSourceCell.Value '***You could add statements to copy other cell properties also. End If Next rngSourceCell End Sub
And yes, during my working years I was very familiar with clients not giving me the full and correct definitions of what they needed/wanted on the first try.
- BrianP475Copper Contributor
I dont think it attached last time due to macros enabled. Ive created another book, and hopefully it uploads this time.
- NikolinoDEGold Contributor
I don't see any inserted file, but here is an approach to your plan...I hope I understood it correctly.
If not...insert the file 🙂.
Sub CopyMarkedDataToNextBlankRows() Dim wsSrc As Worksheet, wsDst As Worksheet Dim rangesToCheck As Variant Dim srcRng As Range, cell As Range Dim destRangeStart As Long, destRangeEnd As Long Dim destCell As Range, dataToCopy As Collection Dim i As Long Set wsSrc = ThisWorkbook.Sheets("Sheet1") ' Source Sheet Set wsDst = ThisWorkbook.Sheets("Sheet2") ' Destination Sheet ' Define your source ranges (must match destination logic) rangesToCheck = Array("2:9", "11:21", "23:29") For i = 0 To UBound(rangesToCheck) Set dataToCopy = New Collection ' Set source range (Column A and B) Set srcRng = wsSrc.Range("A" & rangesToCheck(i).Split(":")(0) & ":A" & rangesToCheck(i).Split(":")(1)) ' Collect values from column B where A has "t" For Each cell In srcRng If LCase(Trim(cell.Value)) = "t" Then dataToCopy.Add wsSrc.Cells(cell.Row, "B").Value End If Next cell ' Define target range in destination sheet destRangeStart = CLng(rangesToCheck(i).Split(":")(0)) destRangeEnd = CLng(rangesToCheck(i).Split(":")(1)) ' Paste values into next available blank cells in that destination range For Each cell In wsDst.Range("B" & destRangeStart & ":B" & destRangeEnd) If cell.Value = "" And dataToCopy.Count > 0 Then cell.Value = dataToCopy(1) dataToCopy.Remove 1 End If Next cell Next i MsgBox "Data transferred successfully!" End Sub
My answers are voluntary and without guarantee!
Hope this will help you.