copy on double click in sheet1 and then paste into next available row in sheet2.

Sonican

New Member
Joined
Jun 20, 2016
Messages
13
I am very new to vba, which i why i am asking this, I'm sure to most people on here this is a very simple process, but I just cant get it to work.
So here is what i am trying to do.
In sheet one i have a table. in column A, i have an ID number with leading zeros. what i need to happen is when i double click this number, I need it to copy over into sheet2, keeping the leading zeros and starting at cell a1 and from then on each ID number i double click be pasted into the next cell below.

Thank you so much in advance, i'm so lost its sad :(
 

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Right click sheet1, VIEW CODE and copy paste this:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lastrow As Long
If Target.Column = 1 Then
On Error Resume Next
lastrow = 0
lastrow = Sheet2.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Resume Next
Sheet2.Cells(lastrow + 1, 1).NumberFormat = "@"
Sheet2.Cells(lastrow + 1, 1) = Target
End If
End Sub
I didn't build in anything to consider duplicates
 

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Here this one checks for if the user is trying to copy blanks or duplicates:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lastrow As Long
If Target.Column = 1 Then


'if sheet1 cell is blank
If Trim(Target) = "" Then
Exit Sub
End If


'check for duplicates
If IsError(Application.Match(Target, Sheet2.Range("A:A"), 0)) = False Then
MsgBox Target & " already on " & Sheet2.Name & " tab", vbInformation, "ALERT"
Exit Sub
End If


On Error Resume Next
lastrow = 0
lastrow = Sheet2.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Resume Next
Sheet2.Cells(lastrow + 1, 1).NumberFormat = "@"
Sheet2.Cells(lastrow + 1, 1) = Target
End If
End Sub
 

Forum statistics

Threads
1,081,545
Messages
5,359,445
Members
400,528
Latest member
Ratish52

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top