Solved

Add column for primary key.

Posted on 2011-03-14
14
252 Views
Last Modified: 2012-05-11
I need to add a primary key column labeled PK right before the empty Pos# column.  The PK# column should create static numbers, centered horizontally, for each row starting with 1 at the top and ascending toward the bottom.  Since the initial order of items is crucial, this primary key will serve as a basis for sorting the sheet moving forward.  
Option Explicit

Public Sub CopyTradeHistory()
  Dim wks As Worksheet
  Dim rngSrc As Range
  Dim strSheetname As String
  Dim lastrow As Long
  Dim Cell As Variant
  
  On Error Resume Next
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Set wks = ActiveSheet
  strSheetname = wks.Name
  Set rngSrc = wks.Cells.Find("Account Trade History")
  Set rngSrc = wks.Range(rngSrc, rngSrc.End(xlDown).End(xlDown).Offset(0, 11))
  ActiveWorkbook.Worksheets(strSheetname & "_output").Delete
  Set wks = ActiveWorkbook.Worksheets.Add
  wks.Name = strSheetname & "_output"

  rngSrc.Copy wks.Range("A1")
  wks.Columns(3).Insert
  wks.Columns(5).Insert
  wks.Cells(2, 3).Value = "Spread#"
  wks.Cells(2, 4).Value = "Spread"
  wks.Cells(2, 5).Value = "Leg#"
  wks.Rows("1:2").Font.Bold = True
  lastrow = wks.Range("a" & 64000).End(xlUp).Row
  wks.Range("E3:E" & lastrow).Formula = "=IF(OR(H3<>H2,E2=""leg2""),""Leg1"",""Leg2"")"
  wks.Range("E3:E" & lastrow).Value = wks.Range("E3:E" & lastrow).Value
  wks.Range("C3:C" & lastrow).Formula = "=counta(B3:B$3)"
  wks.Range("C3:C" & lastrow).NumberFormat = "General"
  wks.Range("C3:C" & lastrow).Value = wks.Range("C3:C" & lastrow).Value
  For Each Cell In wks.Range("I3:I" & lastrow)
    With Cell
        
            .Value = DateSerial(2000 + Day(.Value), Month(.Value), 1)
            .NumberFormat = "mmm yyyy"
       
    End With
  Next Cell
  wks.Columns(3).Insert
  wks.Cells(2, 3).Value = "Pos#"
  With wks.Range("P3:P" & lastrow)
    .Formula = "=text(J3,""mmm yyyy"") & "" "" & K3 & "" "" & L3"
    .Value = .Value
    .Copy
    wks.Range("J3:J" & lastrow).PasteSpecial
    wks.Range("J2").Value = "Instrument"
    Application.CutCopyMode = False
    .ClearContents
    '.Formula = "=I3 & E3"
    '.Value = .Value
  End With
  
  
  wks.Range("K:L").Delete shift:=xlToLeft
  wks.Range("B1").Value = wks.Range("A1").Value
  wks.Range("A:A").Delete shift:=xlToLeft
  wks.Columns("A:M").EntireColumn.AutoFit
  wks.Columns("B:C").HorizontalAlignment = xlCenter
  wks.Columns("G:G").HorizontalAlignment = xlCenter
  wks.Columns("D:D").HorizontalAlignment = xlJustify
  wks.Rows("3:3").Select
  ActiveWindow.FreezePanes = True
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

Open in new window

TradeHist-2.4a.xlsm
0
Comment
Question by:rtod2
  • 6
  • 4
  • 3
14 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35130307
Try this:
Public Sub CopyTradeHistory()
  Dim wks As Worksheet
  Dim rngSrc As Range
  Dim strSheetname As String
  Dim lastrow As Long
  Dim Cell As Variant
  
  On Error Resume Next
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Set wks = ActiveSheet
  strSheetname = wks.Name
  Set rngSrc = wks.Cells.Find("Account Trade History")
  Set rngSrc = wks.Range(rngSrc, rngSrc.End(xlDown).End(xlDown).Offset(0, 11))
  ActiveWorkbook.Worksheets(strSheetname & "_output").Delete
  Set wks = ActiveWorkbook.Worksheets.Add
  wks.Name = strSheetname & "_output"

  rngSrc.Copy wks.Range("A1")
  wks.Columns(3).Insert
  wks.Columns(5).Insert
  wks.Cells(2, 3).Value = "Spread#"
  wks.Cells(2, 4).Value = "Spread"
  wks.Cells(2, 5).Value = "Leg#"
  wks.Rows("1:2").Font.Bold = True
  lastrow = wks.Range("a" & 64000).End(xlUp).Row
  wks.Range("E3:E" & lastrow).Formula = "=IF(OR(H3<>H2,E2=""leg2""),""Leg1"",""Leg2"")"
  wks.Range("E3:E" & lastrow).Value = wks.Range("E3:E" & lastrow).Value
  wks.Range("C3:C" & lastrow).Formula = "=counta(B3:B$3)"
  wks.Range("C3:C" & lastrow).NumberFormat = "General"
  wks.Range("C3:C" & lastrow).Value = wks.Range("C3:C" & lastrow).Value
  For Each Cell In wks.Range("I3:I" & lastrow)
    With Cell
        
            .Value = DateSerial(2000 + Day(.Value), Month(.Value), 1)
            .NumberFormat = "mmm yyyy"
       
    End With
  Next Cell
  wks.Columns(3).Insert
  wks.Cells(2, 3).Value = "Pos#"
  With wks.Range("P3:P" & lastrow)
    .Formula = "=text(J3,""mmm yyyy"") & "" "" & K3 & "" "" & L3"
    .Value = .Value
    .Copy
    wks.Range("J3:J" & lastrow).PasteSpecial
    wks.Range("J2").Value = "Instrument"
    Application.CutCopyMode = False
    .ClearContents
    '.Formula = "=I3 & E3"
    '.Value = .Value
  End With
  wks.Columns(3).Insert
  wks.Cells(2, 3).Value = "PK"
  With wks.Range("C3:C" & lastrow)
   .Value = Evaluate("INDEX(ROW(" & .Address & ")-2,0)")
   .NumberFormat = "0"
   .HorizontalAlignment = xlHAlignCenter
   End With
  wks.Range("K:L").Delete shift:=xlToLeft
  wks.Range("B1").Value = wks.Range("A1").Value
  wks.Range("A:A").Delete shift:=xlToLeft
  wks.Columns("A:M").EntireColumn.AutoFit
  wks.Columns("B:C").HorizontalAlignment = xlCenter
  wks.Columns("G:G").HorizontalAlignment = xlCenter
  wks.Columns("D:D").HorizontalAlignment = xlJustify
  wks.Rows("3:3").Select
  ActiveWindow.FreezePanes = True
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

Open in new window

0
 

Author Comment

by:rtod2
ID: 35130489
0
 

Author Comment

by:rtod2
ID: 35134170
This answer caused the Instrument column to be removed.  I also need an I/S# if possible.  If the Instrument and the Symbol are the same on another row, then they should both have the same I/S number.
0
 
LVL 10

Accepted Solution

by:
Makrini earned 500 total points
ID: 35134383
TradeHist-2-4b.xlsm

This resolves the problem and adds the i/s#
0
 

Author Comment

by:rtod2
ID: 35134499
Almost!

Row 134 and 145 for instance would have the same I/S#.  The horizontal and vertical spacing is no longer adjusting to contents and the I/S# should be named that at the top of the column.
0
 

Author Comment

by:rtod2
ID: 35134503
It is the instrument AND the symbol that will make up the I/S#
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35135693
@rtod2
The "Hire Me" button on a member's profile is there if you want to pay for work to be done on contract, not to draw attention to your questions! ;)
0
 

Author Comment

by:rtod2
ID: 35135752
Actually, you are a top expert.  Let's talk about that.  I am looking to make a web app out of the final.
0
 

Author Comment

by:rtod2
ID: 35135758
Actually, you are a top expert.  Let's talk about that.  I am looking to make a web app out of the final.  Note that I am only responding.  For mods,  I am not offering payment.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35136132
Web apps are not my area, I'm afraid, whether you are paying or not.
0
 
LVL 10

Expert Comment

by:Makrini
ID: 35136141
Not only is Rorya a top expert, he is one of the most amazing Excel and VBA gurus I have ever come across.  I learn from his posts all the time
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35136157
Ta very much! I shall spend the rest of the day feeling bigheaded now. :)
0
 
LVL 10

Expert Comment

by:Makrini
ID: 35136175
lol - you should.  I don't think I've been to a forum where you aren't one of the most prevalent
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now