VBA Code overwrites data

blueorchid00

New Member
Joined
Jul 6, 2023
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Hi Everyone,I followed a youtube tutorial to copy data from one sheet into another sheet in the same workbook. Mind you, I have no experience in VBA. Everything was working fine until I noticed that when i add a new entry it overwrites the previous row. I want the code to find the last empty row and add the newest entry there. So, if rows 3 and 4 have information, then the newest information gets transferred to row 5 and so forth. Here is the code:


VBA Code:
Private Sub CommandButton1_Click()
'Create and set variables for the Time Tracker & Master Data worksheets
Dim TTrk As Worksheet, MDat As Worksheet

Set TTrk = Sheet1
Set MDat = Sheet2

'Create and set variables for each cell in the Time Tracker sheet
Dim Name As Range, AddDate As Range, TotalHours As Range, Activity As Range, Client As Range, Category As Range, AddHours As Range, TimeSpent As Range, Additional As Range

Set Name = TTrk.Range("C2")
Set AddDate = TTrk.Range("C3")
Set TotalHours = TTrk.Range("C5")
Set Activity = TTrk.Range("B8:B15")
Set Client = TTrk.Range("C8:C15")
Set Category = TTrk.Range("D8:D15")
Set AddHours = TTrk.Range("E8:E15")
Set TimeSpent = TTrk.Range("F8:F15")
Set Additional = TTrk.Range("G8:G15")

'Create a variable for the paste cell in the Master Data worksheet
Dim DestCell As Range
If MDat.Range("A2") = "" Then 'If A2 is empty   
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else   
Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...otherwise the next empty row
If MDat.Range("A2") = "" Then 'If A2 is empty   
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else   
Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...otherwise the next empty row   
End If

'Copy and paste data from the Time Tracker worksheet to the Master Data worksheet
Name.Copy DestCell
AddDate.Copy DestCell.Offset(0, 1)
TotalHours.Copy DestCell.Offset(0, 2)
Activity.Copy DestCell.Offset(0, 3)
Client.Copy DestCell.Offset(0, 4)
Category.Copy DestCell.Offset(0, 5)
AddHours.Copy DestCell.Offset(0, 6)
TimeSpent.Copy DestCell.Offset(0, 7)
Additional.Copy DestCell.Offset(0, 8)

'Clear the contents in the Time Tracker worksheet
AddDate.ClearContents
TotalHours.ClearContents
Activity.ClearContents
Client.ClearContents
Category.ClearContents
AddHours.ClearContents
TimeSpent.ClearContents
Additional.ClearContents

End Sub

Thank you so much!
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
VBA Code:
Set DestCell = MDat.Range("A" & mdat.cells(mdat.rows.count,"A").end(xlup).row + 1 )
 
Upvote 0
VBA Code:
Set DestCell = MDat.Range("A" & mdat.cells(mdat.rows.count,"A").end(xlup).row + 1 )
Hi Dave, thank you for responding! So sorry to ask, but where in the code should I put this? Thank you!
 
Upvote 0
VBA Code:
Set DestCell = MDat.Range("A" & mdat.cells(mdat.rows.count,"A").end(xlup).row + 1 )
Hi again, so in my Master Data sheet, if the "Name" and "Date" (Columns A and B) are blank, then if I type information on the Time Tracker sheet and submit it, then data still overwrites the information because there are blanks on columns A and B. I think these columns need to have the name and date to be copied down every time. Can I fix this in my original code? Thanks!
 

Attachments

  • VBA Code Error.PNG
    VBA Code Error.PNG
    18.1 KB · Views: 8
Upvote 0
Hi,
I have only glanced at your code but try replacing this part of your code

VBA Code:
If MDat.Range("A2") = "" Then 'If A2 is empty
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else
Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...otherwise the next empty row
If MDat.Range("A2") = "" Then 'If A2 is empty
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else
Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...otherwise the next empty row
End If

with this

VBA Code:
With MDat
  Set DestCell = .Cells(.Cells(.Rows.Count, "D").End(xlUp).Row + 1, 1)
 End With

and see if this resolves your issue

Dave
 
Upvote 0
Solution
Hi,
I have only glanced at your code but try replacing this part of your code

VBA Code:
If MDat.Range("A2") = "" Then 'If A2 is empty
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else
Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...otherwise the next empty row
If MDat.Range("A2") = "" Then 'If A2 is empty
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else
Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...otherwise the next empty row
End If

with this

VBA Code:
With MDat
  Set DestCell = .Cells(.Cells(.Rows.Count, "D").End(xlUp).Row + 1, 1)
 End With

and see if this resolves your issue

Dave

You are a genius and this works! Thank you so much for your time and help :)
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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