Pasting not working properly

Gundisalvus

New Member
Joined
Jan 27, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello all I did a macro in VBA that should check column D for the first empty cell then paste on that row but on column C, and when adding new info in the table it should take the first empty cell again, but it is replacing data, I don't check column C for first row because i have an filled cell midway, and if data were to replace that cell it should add a new row and avoid that

VBA Code:
Sub CopyPasteToAnotherSheet()
 
  'Declare the variables
  Dim sourceRange As Range 'The range of cells to be copied
  Dim targetRange As Range 'The range of cells where the data will be pasted
  Dim lastRow As Long 'The last row of data in the target sheet
  Dim firstEmptyRow As Long 'The first empty row in the target sheet
 
  'Set the source range to the selected cells
  Set sourceRange = Selection
 
  'Set the target range to the first cell in column D where data will be pasted
  Set targetRange = Sheets("PARKING").Range("D18")
 
  'Find the last row of data in the target sheet
  lastRow = targetRange.End(xlDown).Row
 
  'Find the first empty row in column D after the last row of data
  firstEmptyRow = Sheets("PARKING").Range("D" & lastRow).End(xlUp).Row + 1
 
  'If the last row of data is the first row in the target sheet, insert a new row
  If lastRow = targetRange.Row Then
    targetRange.EntireRow.Insert
  End If
 
  'Check if the corresponding cell in column C is empty
  If Sheets("PARKING").Range("C" & firstEmptyRow).Value <> "" Then
    'If it is not empty, move down one row
    firstEmptyRow = firstEmptyRow + 1
  End If
 
  'Set the target range to the first empty cell in column C
  Set targetRange = Sheets("PARKING").Range("C" & firstEmptyRow)
 
  'Copy the source range
  sourceRange.Copy
 
  'Paste the data as values only into the target range
  targetRange.PasteSpecial xlPasteValues
 
End Sub

Book1.xlsx
ABCDEFGHIJKLMNOP
17###xxxxxxxxxxxxxxxxxxxxxxxxx#######mm/dd/yyyyxxxxxxxxxxxx###(xx)xxxxAAAA #,###,###,###.## xxxxxxxxxx############ xxxx9999999RESERVED FOR SAP
181
192
203
214
225
236
247
258
269
2710
2811
2912
3013
3114
3215
3316
3417
3518
3619
3720
3821
3922
4023
4124
4225
4326
4427
4528
4629
4730
4831
4932
5033
5134
5235
5336
5437
5538
5639
5740
5841
5942
6043
6144
6245
6346
6447
6548
6649
6750
6851
6952
7053
7154
7255
7356
7457
7558
7659
7760
7861
7962
8063
8164
8265
8366
8467
8568
8669
8770
8871
8972
9073
9174
9275
9376
9477
9578
9679
9780
9881
9982
10083
10184
10285
10386
10487
10588
10689
10790
10891
10992
11093
11194
11295
11396
11497
11598
11699
117100
118101
119102
120103
121104
122105
123106
124107
125108
126109
127110
128111
129112
130113
131114
132115
133116
134*DO NOT DELETE THIS ROW
135
136
PARKING



also if it would keep numbering the filled rows when adding new data it would be great I am clueless on how I should do that
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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