PC Review


Reply
Thread Tools Rate Thread

CopyPasteSortCountMacro

 
 
MCheru
Guest
Posts: n/a
 
      21st Feb 2009
Here is my code.

Columns("I:J").Select
Selection.Copy
Sheets.Add
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
Range("A1:B100").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

And this is what it does now.

Copy Column I:J
Open a new worksheet
Paste Column I:J into Column A:B of new worksheet.
Sort by/Column A/Ascending


I want to add to the code I have above so that it takes these additional
steps below.

Put a “1” in every cell in Column C if Column A:B have contents inside it.
(What is in Column A is associated with Column B).

Review every row in Column A:B from A1:B1 to A100:B100. When the same
contents appear two, three, or more times in any row in Column A:B, I want
that row to be deleted and the total/tally for the 1st time those contents
appeared in a row in Column A:B to be increased by “1” in Column C. In other
words each time an exact duplicate is found in A:B that row gets deleted and
the total/tally being kept in Column C for the first time those contents
appeared gets increased by “1”

The contents are typically 10 characters long and a mixture of numbers and
letters which are sometimes connected by dashes (i.e) CF-88VBXWQ. There is
no pattern to the letters, numbers, or dashes.

Is this possible?

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      21st Feb 2009
Sub CopySheet()
Set OldSht = ActiveSheet
Sheets.Add
Set NewSht = ActiveSheet

OldSht.Columns("I:J").Copy _
Destination:=NewSht.Columns("A")
Application.CutCopyMode = False
With NewSht
Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
Set SortRange = .Rows("1:" & Lastrow)
SortRange.Sort _
Key1:=.Range("A1"), _
Order1:=xlAscending, _
Header:=xlYes
RowCount = 1
Do While .Range("A" & RowCount) <> ""
If .Range("C" & RowCount) = "" Then
.Range("C" & RowCount) = 1
End If
If .Range("A" & RowCount) = .Range("A" & (RowCount + 1)) And _
.Range("B" & RowCount) = .Range("B" & (RowCount + 1)) Then

.Range("C" & RowCount) = .Range("C" & RowCount) + 1
Rows(RowCount + 1).Delete
Else
RowCount = RowCount + 1
End If
Loop
End With
End Sub

"MCheru" wrote:

> Here is my code.
>
> Columns("I:J").Select
> Selection.Copy
> Sheets.Add
> ActiveSheet.Paste
> Range("A1").Select
> Application.CutCopyMode = False
> Range("A1:B100").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
> xlGuess, OrderCustom:=1, MatchCase:=False,
> Orientation:=xlTopToBottom, _
> DataOption1:=xlSortNormal
>
> And this is what it does now.
>
> Copy Column I:J
> Open a new worksheet
> Paste Column I:J into Column A:B of new worksheet.
> Sort by/Column A/Ascending
>
>
> I want to add to the code I have above so that it takes these additional
> steps below.
>
> Put a “1” in every cell in Column C if Column A:B have contents inside it.
> (What is in Column A is associated with Column B).
>
> Review every row in Column A:B from A1:B1 to A100:B100. When the same
> contents appear two, three, or more times in any row in Column A:B, I want
> that row to be deleted and the total/tally for the 1st time those contents
> appeared in a row in Column A:B to be increased by “1” in Column C. In other
> words each time an exact duplicate is found in A:B that row gets deleted and
> the total/tally being kept in Column C for the first time those contents
> appeared gets increased by “1”
>
> The contents are typically 10 characters long and a mixture of numbers and
> letters which are sometimes connected by dashes (i.e) CF-88VBXWQ. There is
> no pattern to the letters, numbers, or dashes.
>
> Is this possible?
>

 
Reply With Quote
 
MCheru
Guest
Posts: n/a
 
      23rd Feb 2009
Thank you. This is great.

"Joel" wrote:

> Sub CopySheet()
> Set OldSht = ActiveSheet
> Sheets.Add
> Set NewSht = ActiveSheet
>
> OldSht.Columns("I:J").Copy _
> Destination:=NewSht.Columns("A")
> Application.CutCopyMode = False
> With NewSht
> Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
> Set SortRange = .Rows("1:" & Lastrow)
> SortRange.Sort _
> Key1:=.Range("A1"), _
> Order1:=xlAscending, _
> Header:=xlYes
> RowCount = 1
> Do While .Range("A" & RowCount) <> ""
> If .Range("C" & RowCount) = "" Then
> .Range("C" & RowCount) = 1
> End If
> If .Range("A" & RowCount) = .Range("A" & (RowCount + 1)) And _
> .Range("B" & RowCount) = .Range("B" & (RowCount + 1)) Then
>
> .Range("C" & RowCount) = .Range("C" & RowCount) + 1
> Rows(RowCount + 1).Delete
> Else
> RowCount = RowCount + 1
> End If
> Loop
> End With
> End Sub
>
> "MCheru" wrote:
>
> > Here is my code.
> >
> > Columns("I:J").Select
> > Selection.Copy
> > Sheets.Add
> > ActiveSheet.Paste
> > Range("A1").Select
> > Application.CutCopyMode = False
> > Range("A1:B100").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
> > xlGuess, OrderCustom:=1, MatchCase:=False,
> > Orientation:=xlTopToBottom, _
> > DataOption1:=xlSortNormal
> >
> > And this is what it does now.
> >
> > Copy Column I:J
> > Open a new worksheet
> > Paste Column I:J into Column A:B of new worksheet.
> > Sort by/Column A/Ascending
> >
> >
> > I want to add to the code I have above so that it takes these additional
> > steps below.
> >
> > Put a “1” in every cell in Column C if Column A:B have contents inside it.
> > (What is in Column A is associated with Column B).
> >
> > Review every row in Column A:B from A1:B1 to A100:B100. When the same
> > contents appear two, three, or more times in any row in Column A:B, I want
> > that row to be deleted and the total/tally for the 1st time those contents
> > appeared in a row in Column A:B to be increased by “1” in Column C. In other
> > words each time an exact duplicate is found in A:B that row gets deleted and
> > the total/tally being kept in Column C for the first time those contents
> > appeared gets increased by “1”
> >
> > The contents are typically 10 characters long and a mixture of numbers and
> > letters which are sometimes connected by dashes (i.e) CF-88VBXWQ. There is
> > no pattern to the letters, numbers, or dashes.
> >
> > Is this possible?
> >

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      23rd Feb 2009
For the code to weork properly you need to sort on columns A and B. Try this
change

from
SortRange.Sort _
Key1:=.Range("A1"), _
Order1:=xlAscending, _
Header:=xlYes

to
SortRange.Sort _
Key1:=.Range("A1"), _
Order1:=xlAscending, _
Key2:=.Range("B1"), _
Order2:=xlAscending, _
Header:=xlYes


"MCheru" wrote:

> Thank you. This is great.
>
> "Joel" wrote:
>
> > Sub CopySheet()
> > Set OldSht = ActiveSheet
> > Sheets.Add
> > Set NewSht = ActiveSheet
> >
> > OldSht.Columns("I:J").Copy _
> > Destination:=NewSht.Columns("A")
> > Application.CutCopyMode = False
> > With NewSht
> > Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
> > Set SortRange = .Rows("1:" & Lastrow)
> > SortRange.Sort _
> > Key1:=.Range("A1"), _
> > Order1:=xlAscending, _
> > Header:=xlYes
> > RowCount = 1
> > Do While .Range("A" & RowCount) <> ""
> > If .Range("C" & RowCount) = "" Then
> > .Range("C" & RowCount) = 1
> > End If
> > If .Range("A" & RowCount) = .Range("A" & (RowCount + 1)) And _
> > .Range("B" & RowCount) = .Range("B" & (RowCount + 1)) Then
> >
> > .Range("C" & RowCount) = .Range("C" & RowCount) + 1
> > Rows(RowCount + 1).Delete
> > Else
> > RowCount = RowCount + 1
> > End If
> > Loop
> > End With
> > End Sub
> >
> > "MCheru" wrote:
> >
> > > Here is my code.
> > >
> > > Columns("I:J").Select
> > > Selection.Copy
> > > Sheets.Add
> > > ActiveSheet.Paste
> > > Range("A1").Select
> > > Application.CutCopyMode = False
> > > Range("A1:B100").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
> > > xlGuess, OrderCustom:=1, MatchCase:=False,
> > > Orientation:=xlTopToBottom, _
> > > DataOption1:=xlSortNormal
> > >
> > > And this is what it does now.
> > >
> > > Copy Column I:J
> > > Open a new worksheet
> > > Paste Column I:J into Column A:B of new worksheet.
> > > Sort by/Column A/Ascending
> > >
> > >
> > > I want to add to the code I have above so that it takes these additional
> > > steps below.
> > >
> > > Put a “1” in every cell in Column C if Column A:B have contents inside it.
> > > (What is in Column A is associated with Column B).
> > >
> > > Review every row in Column A:B from A1:B1 to A100:B100. When the same
> > > contents appear two, three, or more times in any row in Column A:B, I want
> > > that row to be deleted and the total/tally for the 1st time those contents
> > > appeared in a row in Column A:B to be increased by “1” in Column C. In other
> > > words each time an exact duplicate is found in A:B that row gets deleted and
> > > the total/tally being kept in Column C for the first time those contents
> > > appeared gets increased by “1”
> > >
> > > The contents are typically 10 characters long and a mixture of numbers and
> > > letters which are sometimes connected by dashes (i.e) CF-88VBXWQ. There is
> > > no pattern to the letters, numbers, or dashes.
> > >
> > > Is this possible?
> > >

 
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



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:34 AM.