PC Review


Reply
Thread Tools Rate Thread

Copy Rows based on column value

 
 
Moneyball Wilson
Guest
Posts: n/a
 
      6th Sep 2008
Hi,

This may be a pretty complicated coding situation but I was hoping
that somebody might be able to help out.

I am trying to copy rows based on a certain value found in Column B

For example, If the value 1 exists in B I would like to copy all of
the rows that contain that value in column B and paste it into a new
workbook.

If the value in column B equals 2 then I would like to copy all of
those values into a different workbook

My problem arises because sometimes column B may contain values up to
15 in one instance while in another it may contain values up to 18 so
I cannot hard code the macro.

So could anybody help me with some code to scan column B and copy rows
based on the values that exist.

Here is an example of the code I have found to copy 1 value to a
different workbook

this macro assumes that your first row of data is a header row.
'will copy a row from one worksheet, to another blank workbook
'IF there is a 0 in column N
'Variables used by the macro
Application.ScreenUpdating = False
Dim FilterCriteria
Dim CurrentFileName As String
Dim NewFileName As String

'Get the current file's name
CurrentFileName = ActiveWorkbook.Name
'Select Range
'(note you can change this to meet your requirements)
Range("A1:AS3000").Select
'Apply Autofilter
Selection.AutoFilter
FilterCriteria = 1
'NOTE - this filter is on column N (field:=14), to change
'to a different column you need to change the field number
Selection.AutoFilter field:=2, Criteria1:=FilterCriteria
'Select the visible cells (the filtered data)
Selection.SpecialCells(xlCellTypeVisible).Select
'Copy the cells
Selection.Copy
'Open a new file
Workbooks.Add Template:="Workbook"
'Get this file's name
NewFileName = ActiveWorkbook.Name
'Make sure you are in cell A1
Range("A1").Select
'Paste the copied cells
ActiveSheet.Paste
'Clear the clipboard contents
Application.CutCopyMode = False
'Go back to the original file
Workbooks(CurrentFileName).Activate
'Clear the autofilter
Selection.AutoFilter field:=1
'Take the Autofilter off
Selection.AutoFilter
'Go to A1
Range("A1").Select
Application.ScreenUpdating = True
End Sub

Thanks so much for the help.

Sincerely,
Ben
 
Reply With Quote
 
 
 
 
Barb Reinhardt
Guest
Posts: n/a
 
      6th Sep 2008
Do you want to selectively copy values (you have a list to copy), or do you
just want to split all the different values in Column B to a new workbook for
each value?

Can the values in column B be sorted?

Barb Reinhardt




"Moneyball Wilson" wrote:

> Hi,
>
> This may be a pretty complicated coding situation but I was hoping
> that somebody might be able to help out.
>
> I am trying to copy rows based on a certain value found in Column B
>
> For example, If the value 1 exists in B I would like to copy all of
> the rows that contain that value in column B and paste it into a new
> workbook.
>
> If the value in column B equals 2 then I would like to copy all of
> those values into a different workbook
>
> My problem arises because sometimes column B may contain values up to
> 15 in one instance while in another it may contain values up to 18 so
> I cannot hard code the macro.
>
> So could anybody help me with some code to scan column B and copy rows
> based on the values that exist.
>
> Here is an example of the code I have found to copy 1 value to a
> different workbook
>
> this macro assumes that your first row of data is a header row.
> 'will copy a row from one worksheet, to another blank workbook
> 'IF there is a 0 in column N
> 'Variables used by the macro
> Application.ScreenUpdating = False
> Dim FilterCriteria
> Dim CurrentFileName As String
> Dim NewFileName As String
>
> 'Get the current file's name
> CurrentFileName = ActiveWorkbook.Name
> 'Select Range
> '(note you can change this to meet your requirements)
> Range("A1:AS3000").Select
> 'Apply Autofilter
> Selection.AutoFilter
> FilterCriteria = 1
> 'NOTE - this filter is on column N (field:=14), to change
> 'to a different column you need to change the field number
> Selection.AutoFilter field:=2, Criteria1:=FilterCriteria
> 'Select the visible cells (the filtered data)
> Selection.SpecialCells(xlCellTypeVisible).Select
> 'Copy the cells
> Selection.Copy
> 'Open a new file
> Workbooks.Add Template:="Workbook"
> 'Get this file's name
> NewFileName = ActiveWorkbook.Name
> 'Make sure you are in cell A1
> Range("A1").Select
> 'Paste the copied cells
> ActiveSheet.Paste
> 'Clear the clipboard contents
> Application.CutCopyMode = False
> 'Go back to the original file
> Workbooks(CurrentFileName).Activate
> 'Clear the autofilter
> Selection.AutoFilter field:=1
> 'Take the Autofilter off
> Selection.AutoFilter
> 'Go to A1
> Range("A1").Select
> Application.ScreenUpdating = True
> End Sub
>
> Thanks so much for the help.
>
> Sincerely,
> Ben
>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      6th Sep 2008
Hi Moneyball

See
http://www.rondebruin.nl/copy5.htm#workbook

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Moneyball Wilson" <(E-Mail Removed)> wrote in message
news:8ef27b0e-3395-4f54-80fa-(E-Mail Removed)...
> Hi,
>
> This may be a pretty complicated coding situation but I was hoping
> that somebody might be able to help out.
>
> I am trying to copy rows based on a certain value found in Column B
>
> For example, If the value 1 exists in B I would like to copy all of
> the rows that contain that value in column B and paste it into a new
> workbook.
>
> If the value in column B equals 2 then I would like to copy all of
> those values into a different workbook
>
> My problem arises because sometimes column B may contain values up to
> 15 in one instance while in another it may contain values up to 18 so
> I cannot hard code the macro.
>
> So could anybody help me with some code to scan column B and copy rows
> based on the values that exist.
>
> Here is an example of the code I have found to copy 1 value to a
> different workbook
>
> this macro assumes that your first row of data is a header row.
> 'will copy a row from one worksheet, to another blank workbook
> 'IF there is a 0 in column N
> 'Variables used by the macro
> Application.ScreenUpdating = False
> Dim FilterCriteria
> Dim CurrentFileName As String
> Dim NewFileName As String
>
> 'Get the current file's name
> CurrentFileName = ActiveWorkbook.Name
> 'Select Range
> '(note you can change this to meet your requirements)
> Range("A1:AS3000").Select
> 'Apply Autofilter
> Selection.AutoFilter
> FilterCriteria = 1
> 'NOTE - this filter is on column N (field:=14), to change
> 'to a different column you need to change the field number
> Selection.AutoFilter field:=2, Criteria1:=FilterCriteria
> 'Select the visible cells (the filtered data)
> Selection.SpecialCells(xlCellTypeVisible).Select
> 'Copy the cells
> Selection.Copy
> 'Open a new file
> Workbooks.Add Template:="Workbook"
> 'Get this file's name
> NewFileName = ActiveWorkbook.Name
> 'Make sure you are in cell A1
> Range("A1").Select
> 'Paste the copied cells
> ActiveSheet.Paste
> 'Clear the clipboard contents
> Application.CutCopyMode = False
> 'Go back to the original file
> Workbooks(CurrentFileName).Activate
> 'Clear the autofilter
> Selection.AutoFilter field:=1
> 'Take the Autofilter off
> Selection.AutoFilter
> 'Go to A1
> Range("A1").Select
> Application.ScreenUpdating = True
> End Sub
>
> Thanks so much for the help.
>
> Sincerely,
> Ben


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy rows to multiple sheets based off column value =?Utf-8?B?V2VzdGVy?= Microsoft Excel Programming 0 19th Oct 2006 04:12 AM
Copy rows of one sheet into mutiple sheets based on column value Wesley Breshears Microsoft Excel Misc 0 18th Oct 2006 03:19 PM
Convert columns to rows: create duplicate rows based on column val =?Utf-8?B?Q2FycmllUg==?= Microsoft Access 3 30th Aug 2006 07:07 PM
MACRO - copy rows based on value in column to another sheet =?Utf-8?B?TWljaGFlbCBB?= Microsoft Excel Misc 1 5th Mar 2005 02:15 AM
MACRO - copy rows based on value in column to another sheet =?Utf-8?B?TWlrZQ==?= Microsoft Excel Programming 2 5th Mar 2005 12:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:23 AM.