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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What do you have in cell D3 on the "Search" sheet?

What does the error say?
 
Upvote 0
Try this
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.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
But you must erase all the code and put the new one.

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.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
Last code you sent worked the first time I clicked an option button.
If I click another option button error below
debug2.jpg
 
Upvote 0
Not sure what auto filter does .... but song list is already in order, no need to arrange anything.
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,798
Members
449,337
Latest member
BBV123

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