PC Review


Reply
Thread Tools Rate Thread

Conditional copy and row insert based on criteria

 
 
Janetzky@googlemail.com
Guest
Posts: n/a
 
      12th Dec 2010
Hi Everybody, I was hoping that anybody in this forum could help me
solve my particular vba problem in Excel. I need to add information
from one sheet to the other based on a certain criteria. This is how
my
information looks like:

Sheet "Source"
Code|Description|Explanation|RollupGroup
AA|Color:Green|ColorCodeA45|10
AB|Fabric:Cotton|NoStretch|10
AD|Pattern:Nuendo|ReferenceAD12|10
BR|Quality:3ply|8907|20
BO|Stitch:4|DoubleCross|20
CA|Yarn:1.2"|6 Threat|30
CF|Length" 23m|Excess .2|30
....
Sheet "Target"
RollupGroup|Description|ISourcetems|Cost
10|Fabric|Fabric Selections|3|12.12
20|Stitching|Stitching Instruction|2|2.33
30|Yarn Selection|2|0.56
....

I need to copy the detail information (entire row) from the source
sheet to the target sheet based on the RollupGroup information.
My new target sheet should then look like this:


Sheet "Target" (After Change)
10|Fabric|Fabric Selections|3|12.12
AA|Color:Green|ColorCodeA45|10
AB|Fabric:Cotton|NoStretch|10
AD|Pattern:Nuendo|ReferenceAD12|10
20|Stitching|Stitching Instruction|2|2.33
BR|Quality:3ply|8907|20
BO|Stitch:4|DoubleCross|20
30|Yarn Selection|2|0.56
CA|Yarn:1.2"|6 Threat|30
CF|Length" 23m|Excess .2|30
....

Anyones help is greatly appreciated!

Regards, Weitwinkel
 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      12th Dec 2010
This should do it:

Sub ConditionalCopy()
Dim Source As Worksheet
Dim Dest As Worksheet
Dim sourceRng As Range
Dim CopyRng As Range
Dim DestCell As Range
Dim CritArr

Set Source = Worksheets("Sheet1")
Set Dest = Worksheets("Sheet2")
CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
For ca = UBound(CritArr) To LBound(CritArr) Step -1
sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(ca, 1)
Debug.Print DestCell.Address

DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
1).EntireRow.Insert
Set DestCell = DestCell.End(xlUp).Offset(1)
CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
Set DestCell = DestCell.Offset(-1)
Next
sourceRng.AutoFilter
End Sub

Regarsd,
Per

On 12 Dec., 19:05, "Janet...@googlemail.com" <janet...@googlemail.com>
wrote:
> Hi Everybody, I was hoping that anybody in this forum could help me
> solve my particular vba problem in Excel. I need to add information
> from one sheet to the other based on a certain criteria. This is how
> my
> information looks like:
>
> Sheet "Source"
> Code|Description|Explanation|RollupGroup
> AA|Color:Green|ColorCodeA45|10
> AB|Fabric:Cotton|NoStretch|10
> AD|Pattern:Nuendo|ReferenceAD12|10
> BR|Quality:3ply|8907|20
> BO|Stitch:4|DoubleCross|20
> CA|Yarn:1.2"|6 Threat|30
> CF|Length" 23m|Excess .2|30
> ...
> Sheet "Target"
> RollupGroup|Description|ISourcetems|Cost
> 10|Fabric|Fabric Selections|3|12.12
> 20|Stitching|Stitching Instruction|2|2.33
> 30|Yarn Selection|2|0.56
> ...
>
> I need to copy the detail information (entire row) from the source
> sheet to the target sheet based on the RollupGroup information.
> My new target sheet should then look like this:
>
> Sheet "Target" (After Change)
> 10|Fabric|Fabric Selections|3|12.12
> AA|Color:Green|ColorCodeA45|10
> AB|Fabric:Cotton|NoStretch|10
> AD|Pattern:Nuendo|ReferenceAD12|10
> 20|Stitching|Stitching Instruction|2|2.33
> BR|Quality:3ply|8907|20
> BO|Stitch:4|DoubleCross|20
> 30|Yarn Selection|2|0.56
> CA|Yarn:1.2"|6 Threat|30
> CF|Length" 23m|Excess .2|30
> ...
>
> Anyones help is greatly appreciated!
>
> Regards, Weitwinkel


 
Reply With Quote
 
WeitWinkel
Guest
Posts: n/a
 
      13th Dec 2010
Hi Per,

thanks for your great help. See my changes in your code. On the
ubound it is not compiling -Expected Array. What is wrong?

Sub ConditionalCopy()

Dim Source As Worksheet
Dim Dest As Worksheet
Dim sourceRng As Range
Dim CopyRng As Range
Dim DestCell As Range
Dim CritArr As Range
Dim Ca As Double


Set Source = Worksheets("Sheet1")
Set Dest = Worksheets("Sheet2")
CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
For Ca = UBound(CritArr) To LBound(CritArr) Step -1
sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(Ca, 1)
Debug.Print DestCell.Address

DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
1).EntireRow.Insert
Set DestCell = DestCell.End(xlUp).Offset(1)
CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
Set DestCell = DestCell.Offset(-1)
Next
sourceRng.AutoFilter
End Sub



On Dec 12, 5:00*pm, Per Jessen <perjesse...@hotmail.com> wrote:
> This should do it:
>
> Sub ConditionalCopy()
> Dim Source As Worksheet
> Dim Dest As Worksheet
> Dim sourceRng As Range
> Dim CopyRng As Range
> Dim DestCell As Range
> Dim CritArr
>
> Set Source = Worksheets("Sheet1")
> Set Dest = Worksheets("Sheet2")
> CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
> Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
> Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
> Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
> For ca = UBound(CritArr) To LBound(CritArr) Step -1
> * * sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(ca, 1)
> * * Debug.Print DestCell.Address
>
> DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
> 1).EntireRow.Insert
> * * Set DestCell = DestCell.End(xlUp).Offset(1)
> * * CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
> * * Set DestCell = DestCell.Offset(-1)
> Next
> sourceRng.AutoFilter
> End Sub
>
> Regarsd,
> Per
>
> On 12 Dec., 19:05, "Janet...@googlemail.com" <janet...@googlemail.com>
> wrote:
>
> > Hi Everybody, I was hoping that anybody in this forum could help me
> > solve my particular vba problem in Excel. I need to add information
> > from one sheet to the other based on a certain criteria. This is how
> > my
> > information looks like:

>
> > Sheet "Source"
> > Code|Description|Explanation|RollupGroup
> > AA|Color:Green|ColorCodeA45|10
> > AB|Fabric:Cotton|NoStretch|10
> > AD|Pattern:Nuendo|ReferenceAD12|10
> > BR|Quality:3ply|8907|20
> > BO|Stitch:4|DoubleCross|20
> > CA|Yarn:1.2"|6 Threat|30
> > CF|Length" 23m|Excess .2|30
> > ...
> > Sheet "Target"
> > RollupGroup|Description|ISourcetems|Cost
> > 10|Fabric|Fabric Selections|3|12.12
> > 20|Stitching|Stitching Instruction|2|2.33
> > 30|Yarn Selection|2|0.56
> > ...

>
> > I need to copy the detail information (entire row) from the source
> > sheet to the target sheet based on the RollupGroup information.
> > My new target sheet should then look like this:

>
> > Sheet "Target" (After Change)
> > 10|Fabric|Fabric Selections|3|12.12
> > AA|Color:Green|ColorCodeA45|10
> > AB|Fabric:Cotton|NoStretch|10
> > AD|Pattern:Nuendo|ReferenceAD12|10
> > 20|Stitching|Stitching Instruction|2|2.33
> > BR|Quality:3ply|8907|20
> > BO|Stitch:4|DoubleCross|20
> > 30|Yarn Selection|2|0.56
> > CA|Yarn:1.2"|6 Threat|30
> > CF|Length" 23m|Excess .2|30
> > ...

>
> > Anyones help is greatly appreciated!

>
> > Regards, Weitwinkel


 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      13th Dec 2010
CritArr has to be declared as Variant not as Range which you did.

I declared CritArr implicit as Variant, as a declared variable without
an explicit data type is declared as Variant.

Also, I would declare Ca as Long, because it is an Integer variable.

Per

On 13 Dec., 01:58, WeitWinkel <janet...@googlemail.com> wrote:
> Hi Per,
>
> *thanks for your great help. See my changes in your code. On the
> ubound it is not compiling -Expected Array. What is wrong?
>
> Sub ConditionalCopy()
>
> Dim Source As Worksheet
> Dim Dest As Worksheet
> Dim sourceRng As Range
> Dim CopyRng As Range
> Dim DestCell As Range
> Dim CritArr As Range
> Dim Ca As Double
>
> Set Source = Worksheets("Sheet1")
> Set Dest = Worksheets("Sheet2")
> CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
> Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
> Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
> Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
> For Ca = UBound(CritArr) To LBound(CritArr) Step -1
> * * sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(Ca, 1)
> * * Debug.Print DestCell.Address
>
> DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
> 1).EntireRow.Insert
> * * Set DestCell = DestCell.End(xlUp).Offset(1)
> * * CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
> * * Set DestCell = DestCell.Offset(-1)
> Next
> sourceRng.AutoFilter
> End Sub
>
> On Dec 12, 5:00*pm, Per Jessen <perjesse...@hotmail.com> wrote:
>
>
>
> > This should do it:

>
> > Sub ConditionalCopy()
> > Dim Source As Worksheet
> > Dim Dest As Worksheet
> > Dim sourceRng As Range
> > Dim CopyRng As Range
> > Dim DestCell As Range
> > Dim CritArr

>
> > Set Source = Worksheets("Sheet1")
> > Set Dest = Worksheets("Sheet2")
> > CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
> > Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
> > Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
> > Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
> > For ca = UBound(CritArr) To LBound(CritArr) Step -1
> > * * sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(ca, 1)
> > * * Debug.Print DestCell.Address

>
> > DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
> > 1).EntireRow.Insert
> > * * Set DestCell = DestCell.End(xlUp).Offset(1)
> > * * CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
> > * * Set DestCell = DestCell.Offset(-1)
> > Next
> > sourceRng.AutoFilter
> > End Sub

>
> > Regarsd,
> > Per

>
> > On 12 Dec., 19:05, "Janet...@googlemail.com" <janet...@googlemail.com>
> > wrote:

>
> > > Hi Everybody, I was hoping that anybody in this forum could help me
> > > solve my particular vba problem in Excel. I need to add information
> > > from one sheet to the other based on a certain criteria. This is how
> > > my
> > > information looks like:

>
> > > Sheet "Source"
> > > Code|Description|Explanation|RollupGroup
> > > AA|Color:Green|ColorCodeA45|10
> > > AB|Fabric:Cotton|NoStretch|10
> > > AD|Pattern:Nuendo|ReferenceAD12|10
> > > BR|Quality:3ply|8907|20
> > > BO|Stitch:4|DoubleCross|20
> > > CA|Yarn:1.2"|6 Threat|30
> > > CF|Length" 23m|Excess .2|30
> > > ...
> > > Sheet "Target"
> > > RollupGroup|Description|ISourcetems|Cost
> > > 10|Fabric|Fabric Selections|3|12.12
> > > 20|Stitching|Stitching Instruction|2|2.33
> > > 30|Yarn Selection|2|0.56
> > > ...

>
> > > I need to copy the detail information (entire row) from the source
> > > sheet to the target sheet based on the RollupGroup information.
> > > My new target sheet should then look like this:

>
> > > Sheet "Target" (After Change)
> > > 10|Fabric|Fabric Selections|3|12.12
> > > AA|Color:Green|ColorCodeA45|10
> > > AB|Fabric:Cotton|NoStretch|10
> > > AD|Pattern:Nuendo|ReferenceAD12|10
> > > 20|Stitching|Stitching Instruction|2|2.33
> > > BR|Quality:3ply|8907|20
> > > BO|Stitch:4|DoubleCross|20
> > > 30|Yarn Selection|2|0.56
> > > CA|Yarn:1.2"|6 Threat|30
> > > CF|Length" 23m|Excess .2|30
> > > ...

>
> > > Anyones help is greatly appreciated!

>
> > > Regards, Weitwinkel- Skjul tekst i anførselstegn -

>
> - Vis tekst i anførselstegn -


 
Reply With Quote
 
WeitWinkel
Guest
Posts: n/a
 
      13th Dec 2010
On Dec 12, 9:03*pm, Per Jessen <perjesse...@hotmail.com> wrote:
> CritArr has to be declared as Variant not as Range which you did.
>
> I declared CritArr implicit as Variant, as a declared variable without
> an explicit data type is declared as Variant.
>
> Also, I would declare Ca as Long, because it is an Integer variable.
>
> Per
>
> On 13 Dec., 01:58, WeitWinkel <janet...@googlemail.com> wrote:
>
> > Hi Per,

>
> > *thanks for your great help. See my changes in your code. On the
> > ubound it is not compiling -Expected Array. What is wrong?

>
> > Sub ConditionalCopy()

>
> > Dim Source As Worksheet
> > Dim Dest As Worksheet
> > Dim sourceRng As Range
> > Dim CopyRng As Range
> > Dim DestCell As Range
> > Dim CritArr As Range
> > Dim Ca As Double

>
> > Set Source = Worksheets("Sheet1")
> > Set Dest = Worksheets("Sheet2")
> > CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
> > Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
> > Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
> > Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
> > For Ca = UBound(CritArr) To LBound(CritArr) Step -1
> > * * sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(Ca, 1)
> > * * Debug.Print DestCell.Address

>
> > DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
> > 1).EntireRow.Insert
> > * * Set DestCell = DestCell.End(xlUp).Offset(1)
> > * * CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
> > * * Set DestCell = DestCell.Offset(-1)
> > Next
> > sourceRng.AutoFilter
> > End Sub

>
> > On Dec 12, 5:00*pm, Per Jessen <perjesse...@hotmail.com> wrote:

>
> > > This should do it:

>
> > > Sub ConditionalCopy()
> > > Dim Source As Worksheet
> > > Dim Dest As Worksheet
> > > Dim sourceRng As Range
> > > Dim CopyRng As Range
> > > Dim DestCell As Range
> > > Dim CritArr

>
> > > Set Source = Worksheets("Sheet1")
> > > Set Dest = Worksheets("Sheet2")
> > > CritArr = Dest.Range("A2", Dest.Range("A2").End(xlDown)).Value
> > > Set sourceRng = Source.Range("A1", Source.Range("D1").End(xlDown))
> > > Set CopyRng = sourceRng.Offset(1).Resize(sourceRng.Rows.Count - 1, 4)
> > > Set DestCell = Dest.Range("A1").End(xlDown).Offset(1)
> > > For ca = UBound(CritArr) To LBound(CritArr) Step -1
> > > * * sourceRng.AutoFilter Field:=4, Criteria1:=CritArr(ca, 1)
> > > * * Debug.Print DestCell.Address

>
> > > DestCell.Resize(CopyRng.SpecialCells(xlCellTypeVisible).Rows.Count,
> > > 1).EntireRow.Insert
> > > * * Set DestCell = DestCell.End(xlUp).Offset(1)
> > > * * CopyRng.SpecialCells(xlCellTypeVisible).Copy DestCell
> > > * * Set DestCell = DestCell.Offset(-1)
> > > Next
> > > sourceRng.AutoFilter
> > > End Sub

>
> > > Regarsd,
> > > Per

>
> > > On 12 Dec., 19:05, "Janet...@googlemail.com" <janet...@googlemail.com>
> > > wrote:

>
> > > > Hi Everybody, I was hoping that anybody in this forum could help me
> > > > solve my particular vba problem in Excel. I need to add information
> > > > from one sheet to the other based on a certain criteria. This is how
> > > > my
> > > > information looks like:

>
> > > > Sheet "Source"
> > > > Code|Description|Explanation|RollupGroup
> > > > AA|Color:Green|ColorCodeA45|10
> > > > AB|Fabric:Cotton|NoStretch|10
> > > > AD|Pattern:Nuendo|ReferenceAD12|10
> > > > BR|Quality:3ply|8907|20
> > > > BO|Stitch:4|DoubleCross|20
> > > > CA|Yarn:1.2"|6 Threat|30
> > > > CF|Length" 23m|Excess .2|30
> > > > ...
> > > > Sheet "Target"
> > > > RollupGroup|Description|ISourcetems|Cost
> > > > 10|Fabric|Fabric Selections|3|12.12
> > > > 20|Stitching|Stitching Instruction|2|2.33
> > > > 30|Yarn Selection|2|0.56
> > > > ...

>
> > > > I need to copy the detail information (entire row) from the source
> > > > sheet to the target sheet based on the RollupGroup information.
> > > > My new target sheet should then look like this:

>
> > > > Sheet "Target" (After Change)
> > > > 10|Fabric|Fabric Selections|3|12.12
> > > > AA|Color:Green|ColorCodeA45|10
> > > > AB|Fabric:Cotton|NoStretch|10
> > > > AD|Pattern:Nuendo|ReferenceAD12|10
> > > > 20|Stitching|Stitching Instruction|2|2.33
> > > > BR|Quality:3ply|8907|20
> > > > BO|Stitch:4|DoubleCross|20
> > > > 30|Yarn Selection|2|0.56
> > > > CA|Yarn:1.2"|6 Threat|30
> > > > CF|Length" 23m|Excess .2|30
> > > > ...

>
> > > > Anyones help is greatly appreciated!

>
> > > > Regards, Weitwinkel- Skjul tekst i anførselstegn -

>
> > - Vis tekst i anførselstegn -


GENIUS! You just made my day!
 
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
Macro Help - Insert row/copy based on criteria Katerinia Microsoft Excel Programming 3 7th Apr 2010 12:08 AM
Conditional Formatting based on 2 criteria Still learning@work Microsoft Excel Worksheet Functions 4 7th Apr 2009 10:16 PM
Conditional Formatting based on multiple criteria rasinc Microsoft Excel Programming 2 9th Mar 2008 02:18 AM
Conditional format based on criteria =?Utf-8?B?U3lhaGlyYQ==?= Microsoft Excel Misc 2 26th Sep 2007 07:32 AM
conditional formula - based on 2 separate criteria =?Utf-8?B?YW5kcmV3by1z?= Microsoft Excel Worksheet Functions 10 14th Mar 2005 03:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:32 AM.