VBA search, copy and paste help

Giggzz

Well-known Member
Joined
Jul 4, 2002
Messages
990
Private Sub CommandButton3_Click()
ActiveSheet.Unprotect ("as")
Application.ScreenUpdating = False
Application.EnableEvents = False

'Account number
Worksheets(2).Range("B1").Value = Worksheets("Quote").Range("D19").Value
'names
Worksheets(2).Range("B2").Value = Worksheets("Quote").Range("B5:D5").Value
'address
Worksheets(2).Range("B3").Value = Worksheets("Quote").Range("B6:D6").Value

Application.ScreenUpdating = True
Application.EnableEvents = True
ActiveSheet.Protect ("as")
Unload Me

End Sub
Good morning all, working on a little project and need a little help. Here is what Im looking to do, to create a small data base of account #'s, names, and address's on a seperate sheet.

1. search a column on another sheet - sheet2.range("A") - to see if the value in sheet("Quote").Range("D19") is listed.

2. If the Value is "not" listed then copy Worksheets("Quote").Range("as listed above").Value to Worksheets(2).Range("as listed above").Value to the next empty row.

3. If the searched value in D19 is listed, than have a messgebox state account # already in use.

Im trying to piece some code together so thought I would ask to save some time along the way. Thanks as always.:)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If I understand your request, this should work:
Code:
Sub CopyUnique()
'Assign Target and Source variables
    TS = Sheets("Sheet2").Name
    SS = Sheets("Quote").Name

    Slastrow = Sheets(SS).UsedRange.Rows.Count
    Tlastrow = Sheets(TS).UsedRange.Rows.Count
    
For i = 1 To Slastrow
    If Not Application.WorksheetFunction.CountIf(Sheets(TS).Range("A:A"), Sheets(SS).Range("A" & i)) = 1 Then
        Sheets(TS).Range("A" & Tlastrow + 1) = Sheets(SS).Range("A" & i)
        Tlastrow = Tlastrow + 1
    Else
        MsgBox "Account #" & vbCrLf _
        & Sheets(SS).Range("A" & i) & vbCrLf _
        & "already in use."
    End If
Next i
End Sub
Note, this code looks at column A not column D, adjust as needed.
 
Upvote 0
Question -

looking at the code - where do I find the reference to cell D19 that supplies the value for the code to look up and compare it to the values listed in the column on Sheet(2). Range("A:A")?? Just trying to understand more about the code.

thanks
 
Upvote 0
Do you have a worksheet named "Sheet2"?

If not, change the code to your Target worksheet name.

Code:
Sub CopyUnique2()
'Assign Target and Source variables
    TS = Sheets("Sheet2").Name
    SS = Sheets("Quote").Name

    Slastrow = Sheets(SS).Range("D65536").End(xlUp).Row
    Tlastrow = Sheets(TS).UsedRange.Rows.Count

For i = 1 To Slastrow
    If Not Application.WorksheetFunction.CountIf(Sheets(TS).Range("A:A"), Sheets(SS).Range("D" & 19 + i)) = 1 Then

        Sheets(TS).Range("A" & Tlastrow + 1) = Sheets(SS).Range("D" & 19 + i)
        Tlastrow = Tlastrow + 1
    Else
        MsgBox "Account #" & vbCrLf _
        & Sheets(SS).Range("D" & 19 + i) & vbCrLf _
        & "already in use."
    End If
Next i
End Sub

I adjusted this code to look at column D on your Quote worksheet.
You still need to change "Sheet2" in the code to your worksheet name.
 
Last edited:
Upvote 0
TS = Worksheets("List").Name
SS = Worksheets("Quote").Name

These are the names of the sheets and the names in the code. Still getting error - not defined.
 
Upvote 0
Add this line near the top of your code:
Code:
Dim TS$, SS$, Slastrow%, Tlastrow%
 
Upvote 0
the code is firing now but a few issues.

On Worksheet("Quote"),Range("D19") is the reference cell(value) for the the code to compare to the values list on worksheet("List").Range("A:A") to see if there is a match. If there is no match from the value on worksheet(quote).Range D19 to the worksheet(List), than I want to copy the range below to the next empty row on worksheet(List).

'Account number
Worksheets(2).Range("B1").Value = Worksheets("Quote").Range("D19").Value
'names
Worksheets(2).Range("B2").Value = Worksheets("Quote").Range("B5:D5").Value
'address
Worksheets(2).Range("B3").Value = Worksheets("Quote").Range("B6:D6").Value

Than if there is a match the messagebox stating account already exsists would appear.
 
Upvote 0
Sub CopyUnique2()
Dim TS$, SS$, Slastrow%, Tlastrow%, i%
'Assign Target and Source variables
TS = Sheets("List").Name
SS = Sheets("Quote").Name

Slastrow = Sheets(SS).Range("D65536").End(xlUp).Row
Tlastrow = Sheets(TS).UsedRange.Rows.Count
For i = 1 To Slastrow
If Not Application.WorksheetFunction.CountIf(Sheets(TS).Range("A:A"), Sheets(SS).Range("D" & 19)) = 1 Then
Sheets(TS).Range("A" & Tlastrow + 1) = Sheets(SS).Range("D" & 19 + i)

Tlastrow = Tlastrow + 1

Else
MsgBox "Account #" & vbCrLf _
& Sheets(SS).Range("D" & 19 + i) & vbCrLf _
& "already in use."
End If
Next i
End Sub

I have tried t modify the code to do what Im looking for but not having much success.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,978
Members
448,934
Latest member
audette89

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