Two sheets ... copy data from one sheet if criteria met ?

lacogada

Board Regular
Joined
Jan 26, 2011
Messages
170
Appreciated the vba code to accomplish task ... beyond my skill level.

SearchSheet.jpg

SongsSheet.jpg
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this

VBA Code:
Sub Copy_Data()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Search")
  Set sh2 = Sheets("Songs")
  sh1.Range("A:A").ClearContents
  If sh1.Range("D3").Value = "" Then
    MsgBox "Select type"
    Exit Sub
  End If
  sh2.Range("A1:B1").AutoFilter 2, sh1.Range("D3").Value
  sh2.AutoFilter.Range.Range("A1:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row).Copy sh1.Range("A1")
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
End Sub
 
Upvote 0
Thanks Dante ... but it does nothing when I click on the form option.
On Search sheet I chose view code and pasted your data.
 

Attachments

  • DanteCode.jpg
    DanteCode.jpg
    54 KB · Views: 5
Upvote 0
After selecting an option from your form, you must press a button, within that button you put the macro.

Ah, another detail, you must put a first row in the "Songs" sheet, it can be a title, in order to perform the filter properly.
 
Upvote 0
Inserted a row on Songs sheet and added title in A1.

But nothing when clicking any option on the user form.

Thanks
 
Upvote 0
Ok, Looks like you have a userform with optoinbuttons
Try this, Put the following code inside the userform.

VBA Code:
Private Sub OptionButton1_Click()
  Sheets("Search").Range("D3").Value = "P"
  Call Copy_Data
End Sub
Private Sub OptionButton2_Click()
  Sheets("Search").Range("D3").Value = "A"
  Call Copy_Data
End Sub
Private Sub OptionButton3_Click()
  Sheets("Search").Range("D3").Value = "B"
  Call Copy_Data
End Sub
Private Sub OptionButton4_Click()
  Sheets("Search").Range("D3").Value = "C"
  Call Copy_Data
End Sub
Private Sub OptionButton5_Click()
  Sheets("Search").Range("D3").Value = "BC"
  Call Copy_Data
End Sub
Private Sub OptionButton6_Click()
  Sheets("Search").Range("D3").Value = "F"
  Call Copy_Data
End Sub
Private Sub OptionButton7_Click()
  Sheets("Search").Range("D3").Value = "D"
  Call Copy_Data
End Sub

Sub Copy_Data()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Search")
  Set sh2 = Sheets("Songs")
  sh1.Range("A:A").ClearContents
  If sh1.Range("D3").Value = "" Then
    MsgBox "Select type"
    Exit Sub
  End If
  sh2.Range("A1:B1").AutoFilter 2, sh1.Range("D3").Value
  sh2.AutoFilter.Range.Range("A1:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row).Copy sh1.Range("A1")
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
What do you have in cell D3 on the "Search" sheet?
 
Upvote 0

Forum statistics

Threads
1,212,931
Messages
6,110,745
Members
448,295
Latest member
Uzair Tahir Khan

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