VBA: Copy and Paste value to new sheet based on criteria from other columns

jhsu0303

New Member
Joined
Jan 29, 2013
Messages
6
I have a data set that has Farmer name in column A, Fruit in column B and Ready? in column C. I want to copy and paste the farmer's name based on the fruit they plant onto their designated sheet (for example, APPLE under the sheet named APPLE), but I only want they want has yea under the Ready? column.

I was wondering if anyone tell me what is the best way to do this in VBA that creates the new sheet and copy and paste the criteria that I described above.

I am very new to VBA and writing this on my own is total out of my league.

Farmer FruitReady?
AAPPLEYES
CAPPLENO
GAPPLEYES
EGRAPESYES
BORANGEYES
FORANGENO
DPEARSNO
HPEARSYES

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
1. will there be only 'apple', 'grapes', 'orange' and 'pears' or other fruit?
2. do these sheets already exist or do they need to be created by the code?
Do you want the headers in the new sheets?

FarmerScott
 
Upvote 0
Yes, there will be other fruits.

preferably I would like to create new sheet based on the fruit category, so it know when there is a new fruit and a new sheet needs to be created.
 
Upvote 0
Hi,

can you test this code to see if a sheet is created for each fruit.

I will add more code for the copying, once we get this right.

I have assumed that the list of fruits is in Col B as per your post above.

Code:
Sub New_sheet_for_each_fruit()
Dim Rng As Range
Dim cell As Range
Dim sh As Worksheet

Dim sheetname As String


lr = Worksheets("Data").Range("B" & Rows.Count).End(xlUp).Row

Set Rng = Worksheets("Data").Range("B1:B" & lr)

'loop through range with fruit names
For Each cell In Rng

If cell.Value <> "" Then
sheetname = cell.Value

'test if the cell value has an equivalent sheet name
If Not SheetExists(sheetname) Then



Sheets.Add.Name = sheetname
End If
End If
Next
End Sub



Function SheetExists(sheetname As String) As Boolean
SheetExists = False
For Each sh In Sheets
If sh.Name = sheetname Then
SheetExists = True
Exit For
End If
Next sh
End Function

FarmerScott
 
Upvote 0

Forum statistics

Threads
1,215,364
Messages
6,124,510
Members
449,166
Latest member
hokjock

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