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
 
Last code you sent worked the first time I clicked an option button.
If I click another option button error below

And the first time it worked well?
That is, the macro correctly put the songs?
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Take that back ... I shut down and restarted ... put code in .... chose option I knew had least amount of songs, and it copied the whole list (233 songs)
 
Upvote 0
Again, delete the above and put the following code.

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, lr As Long
  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
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  sh2.Range("A1:B" & lr).AutoFilter 2, sh1.Range("D3").Value
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  sh2.Range("A1:A" & lr).Copy sh1.Range("A1")
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  Application.ScreenUpdating = True
End Sub

Note: My version of Excel is 2007 and your version is 2003, which is why I have to change some things. I also have no way to prove that it works in Excel 2003, so you will have to continue testing yourself.
 
Upvote 0
Please, again delete the above and put the following code.

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, lr As Long
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Search")
  Set sh2 = Sheets("Songs")
  sh1.Range("A2:A" & Rows.Count).ClearContents
  If sh1.Range("D3").Value = "" Then
    MsgBox "Select type"
    Exit Sub
  End If
  If sh2.AutoFilterMode Then sh2.AutoFilterMode = False
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  sh2.Range("A1:B" & lr).AutoFilter 2, sh1.Range("D3").Value
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  If lr > 1 Then
    sh2.Range("A1:A" & lr).SpecialCells(xlCellTypeVisible).Copy sh1.Range("A1")
  End If
  If sh2.AutoFilterMode Then sh2.AutoFilterMode = False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'm glad, thanks for the feedback.

I had already prepared another method.
Here I put it, in case you want to try it.

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, lr As Long, i As Long, j As Long
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Search")
  Set sh2 = Sheets("Songs")
  sh1.Range("A2:A" & Rows.Count).ClearContents
  If sh1.Range("D3").Value = "" Then
    MsgBox "Select type"
    Exit Sub
  End If
  If sh2.AutoFilterMode Then sh2.AutoFilterMode = False
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  j = 2
  For i = 2 To lr
    If sh2.Cells(i, "B").Value = sh1.Range("D3").Value Then
      sh1.Cells(j, "A").Value = sh2.Cells(i, "A").Value
      j = j + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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