VBA copy cell data to next empty cell in range

MeaclH

New Member
Joined
Apr 2, 2014
Messages
13
Hi all,

I want to run a macro that copies the data in a selected cell, and enters it in the next available cell further down the sheet. If the first cell in the range has data, it will enter it in the next cell below, stacking the data from multiple cells.

For example the first data would be entered in E60, then E61 etc etc

Cheers
Hayden
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi
Try this in sheet code
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Selection) Is Nothing Then
        Me.Range("E" & Me.Cells(Rows.Count, "E").End(xlUp).Row + 1) = Selection.Offset(-1)
    End If
End Sub
 
Upvote 0
Hi Mohadin,

It didn't seem to work so well, or perhaps I'm a bit too new to this.

Here is the sheet I am working on. I'd like to have a button run a macro that would take the data from the cell selected, and put down the bottom in the area where there is space for Open Shifts.
If the first row of open shifts already has data in it, it will then put it in the cell below, and so on.
Cheers,
Hayden

Digital Roster AUG23.xlsm
ABCDEFGH
52Line 17Rostered DAOFEEGRADO.RRostered Shift2130BC2130PB2130NCC2130BC
53Swapped w/ DAOAmended Shift
54RemarksShift Extension
55Line 18Rostered DAOREEVES.SRostered Shift0530PB0530PL0530CFDOFF
56Swapped w/ DAOAmended Shift
57RemarksShift Extension
58
59Reliever DAOs
60
61Reliever 1Rostered DAORostered Shift
62Swapped w/ DAOAmended Shift
63RemarksShift Extension
64Reliever 2Rostered DAORostered Shift
65Swapped w/ DAOAmended Shift
66RemarksShift Extension
67Reliever 3Rostered DAORostered Shift
68Swapped w/ DAOAmended Shift
69RemarksShift Extension
70Reliever 4Rostered DAORostered Shift
71Swapped w/ DAOAmended Shift
72RemarksShift Extension
73
74Open Shifts
75
76
77
78
79
27AUG23
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E4:R72Cell Valuecontains "PH CREDIT"textNO
E4:R72Cell Valuecontains "A/L"textNO
80:1048576,A75:C79,E75:XFD79,8:9,A7:B7,11:12,A10:B10,D10:XFD10,14:15,A13:B13,D13:XFD13,17:18,A16:B16,D16:XFD16,20:21,A19:B19,D19:XFD19,23:24,A22:B22,D22:XFD22,26:27,A25:B25,D25:XFD25,29:30,A28:B28,D28:XFD28,32:33,A31:B31,D31:XFD31,35:36,A34:B34,D34:XFD34Cell Valuecontains "CARERS"textNO
E4:R72Cell Valuecontains "SICK"textNO
E71:R72Cell Valuecontains "edo"textNO
E71:R72Cell Valuecontains "OFF"textNO
E61:R70Cell Valuecontains "edo"textNO
E61:R70Cell Valuecontains "OFF"textNO
E56:R59Cell Valuecontains "edo"textNO
E56:R59Cell Valuecontains "OFF"textNO
E4:R55Cell Valuecontains "edo"textNO
E4:R55Cell Valuecontains "OFF"textNO
Cells with Data Validation
CellAllowCriteria
C53List='Staff List'!$A$1:$A$18
C56List='Staff List'!$A$1:$A$18
C68List='Staff List'!$A$1:$A$18
C62List='Staff List'!$A$1:$A$18
C65List='Staff List'!$A$1:$A$18
C71List='Staff List'!$A$1:$A$18
 
Upvote 0
Still your details & requirement is not so clear.
Your post #1 & #3 are not matching.
Also there are merge cell at row 74 to 79.
Please elaborate more, so someone can understand it better & help you quickly.
 
Upvote 0
Still your details & requirement is not so clear.
Your post #1 & #3 are not matching.
Also there are merge cell at row 74 to 79.
Please elaborate more, so someone can understand it better & help you quickly.
Sorry yes I may not have been clear enough.

Say I select cell E52. I want to run a macro on that selection that would, copy that data to E74. Then if I did the same with E55 it would copy the data to E75.
It should always start at row 74, and put the data in the first empty cell working downwards.
Hope this clarifies.
Thanks for your help
 
Upvote 0
Try this
Select the cell you want copy, then run this code.
VBA Code:
Sub CopyToLastRow()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim activeCell As Range
    
    Set ws = ActiveSheet
    Set activeCell = ActiveCell
    lastRow = ws.Cells(ws.Rows.Count, activeCell.Column).End(xlUp).Row
    lastRow = lastRow + 1
    If lastRow < 74 Then
        lastRow = 74
    End If
    
    activeCell.Copy ws.Cells(lastRow, activeCell.Column)
End Sub
 
Upvote 0
Try this
Select the cell you want copy, then run this code.
VBA Code:
Sub CopyToLastRow()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim activeCell As Range
   
    Set ws = ActiveSheet
    Set activeCell = ActiveCell
    lastRow = ws.Cells(ws.Rows.Count, activeCell.Column).End(xlUp).Row
    lastRow = lastRow + 1
    If lastRow < 74 Then
        lastRow = 74
    End If
   
    activeCell.Copy ws.Cells(lastRow, activeCell.Column)
End Sub
Thanks Mehidy, I got this message when running the code. "Object variable or With block variable not set"

Any ideas?
Ta
 
Upvote 0
Try using this, this should work.
VBA Code:
Sub CopyToLastRow()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim selectCell As Range
   
    Set ws = ActiveSheet
    Set selectCell = activeCell
    
    If selectCell Is Nothing Then
        MsgBox "Please select a cell before running this macro.", vbExclamation
        Exit Sub
    End If
    
    lastRow = ws.Cells(ws.Rows.Count, activeCell.Column).End(xlUp).Row
    lastRow = lastRow + 1
    
    If lastRow < 74 Then
        lastRow = 74
    End If
   
    activeCell.Copy ws.Cells(lastRow, activeCell.Column)
End Sub
 
Upvote 0
Try using this, this should work.
VBA Code:
Sub CopyToLastRow()
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim selectCell As Range
  
    Set ws = ActiveSheet
    Set selectCell = activeCell
   
    If selectCell Is Nothing Then
        MsgBox "Please select a cell before running this macro.", vbExclamation
        Exit Sub
    End If
   
    lastRow = ws.Cells(ws.Rows.Count, activeCell.Column).End(xlUp).Row
    lastRow = lastRow + 1
   
    If lastRow < 74 Then
        lastRow = 74
    End If
  
    activeCell.Copy ws.Cells(lastRow, activeCell.Column)
End Sub
amazing!!! thanks heaps mate. one last thing, is there a way to copy and paste only the cell contents and not the cell formatting also?
legend!
 
Upvote 0
Yes, change last line to this.
VBA Code:
ws.Cells(lastRow, activeCell.Column).Value = activeCell.Value
 
Upvote 0

Forum statistics

Threads
1,215,346
Messages
6,124,417
Members
449,157
Latest member
mytux

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