Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 1
Default How to write a macro to selectively output certain data to a different table based on

Hi, I am trying to write a macro that does the following:
Original
Name Risk Rank
John A 1
John A 2
Mark C 3
Nancy A 2
Diane A 1
Diane B 1
Diane B 2
Diane C 2
Judy A 1
Judy A 2
Judy A 3


New
Name Risk Rank
John A 1
Mark C 3
Nancy A 2
Diane A 1
Diane B 1
Judy A 1


The table on the top is the original data and the one on the bottom is the output. Basically, I am trying to let the macro loop through the original table and only output to another table the rows with the lowest rank for the same person and risk. I am pretty new to the excel macro, thanks a lot!!!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default How to write a macro to selectively output certain data to a different table based on

loveitlive formulated on Saturday :
Hi, I am trying to write a macro that does the following:
Original
Name Risk Rank
John A 1
John A 2
Mark C 3
Nancy A 2
Diane A 1
Diane B 1
Diane B 2
Diane C 2
Judy A 1
Judy A 2
Judy A 3


New
Name Risk Rank
John A 1
Mark C 3
Nancy A 2
Diane A 1
Diane B 1
Judy A 1


The table on the top is the original data and the one on the bottom is
the output. Basically, I am trying to let the macro loop through the
original table and only output to another table the rows with the lowest
rank for the same person and risk. I am pretty new to the excel macro,
thanks a lot!!!


May be it is too complicated... but it works.

===============================================
Public Sub SpecialNewTable()
Dim SourceRange As Range, NoDups As New Collection
Dim TargetRange As Range, i As Range, k As Long

' Definitions -----------------
Set TargetRange = [Sheet1!E1]
Set SourceRange = [Sheet1!A1]
' -----------------------------

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set SourceRange = Range(SourceRange, SourceRange.End(xlDown))
For Each i In SourceRange
On Error GoTo Dup_Err
NoDups.Add i(1, 1) & "*" & i(1, 2) & "*" & i(1, 3), _
CStr(i(1, 1) & "*" & i(1, 2) & "*" & i(1, 3))
k = k + 1
TargetRange(k, 1) = i(1, 1) & "*" & i(1, 2) & "*" & i(1, 3)
Continue:
On Error GoTo 0
Next

Set TargetRange = Range(TargetRange, TargetRange.End(xlDown))
TargetRange.Sort _
Key1:=TargetRange(1, 1), _
Order1:=xlAscending, _
Orientation:=xlSortColumns, _
MatchCase:=False

For Each i In TargetRange
i(1, 2) = Mid(i, InStr(1, i, "*") + 1, _
Len(i) - InStrRev(i, "*", -1))
i(1, 3) = Right(i, Len(i) - InStrRev(i, "*", -1))
i(1, 1) = Left(i, InStrRev(i, "*", -1) - 1)
Next

For k = TargetRange.Count - 1 To 1 Step -1
If TargetRange(k) = TargetRange(k + 1) Then
TargetRange(k + 1, 1).ClearContents
TargetRange(k + 1, 2).ClearContents
TargetRange(k + 1, 3).ClearContents
End If
Next

Range(TargetRange, TargetRange.Offset(, 3)).Sort _
Key1:=TargetRange(1, 1), _
Order1:=xlAscending, _
Orientation:=xlSortColumns, _
MatchCase:=False

Set TargetRange = Range(TargetRange(1, 1), TargetRange.End(xlDown))
For Each i In TargetRange
i(1, 1) = Left(i, InStr(1, i, "*") - 1)
Next

Exit_Sub:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Exit Sub

Dup_Err:
Resume Continue

End Sub
=================================================

Bruno


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default How to write a macro to selectively output certain data to adifferent table based on

Hello,

Here is a different approach that also seems to work. This macro copies all data to a new sheet, sorts by Rank, removes duplicates based on Name and Risk, then sorts by name.

Sub ReduceRisk()
Dim rData As Range
Dim ws As Worksheet
Dim lRows As Long

Set rData = Sheet1.Range("A2:C13") 'Data range with Headers
Set ws = Worksheets.Add

rData.Copy ws.Range("A1")
Set rData = ws.UsedRange
lRows = rData.Rows.Count

With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("C2:C" & lRows) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=ws.Range("A2:A" & lRows) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A2:C" & lRows)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ws.Range("A2:C" & lRows).RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo

With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("A2:A" & lRows) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A2:C" & lRows)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default How to write a macro to selectively output certain data to a different table based on

Ben McClave wrote :

Hello,

Here is a different approach that also seems to work. This macro copies all
data to a new sheet, sorts by Rank, removes duplicates based on Name and
Risk, then sorts by name.


I used the very same technique, with the only difference of using the
same (TargetRange) range in the originally sheet.

But there should be a more elegant way of FINDing with multiple WHAT:=
May be with SQL query???

Bruno


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sum cells in same column selectively based on value of different . Carlos GarcĂ­a Excel Programming 0 February 25th 06 09:37 PM
Open CSV file, format data and write output to a text file. BristolBloos Excel Programming 1 October 18th 05 03:50 PM
output based on data in a graph Patrick C. Excel Worksheet Functions 1 September 15th 05 04:49 PM
is it possible to execute write to the fields in another .xsl form a macro in another .xsl? e.g. some way to load another .xsl into an .xsl macro and write to its data? Daniel Excel Worksheet Functions 1 June 23rd 05 11:38 PM
How do I write a macro that hides a row based on data in Excel? JJO Excel Programming 3 May 25th 05 02:04 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"