As always, show all of your code for suggestions
Don,
I was hoping there would be a generic answer to my question, so didn't. But
below is the code for that macro.
The workbook containing the relevant worksheets (of which three are manually
selected), contains about seven worksheets. The relevant worksheets are the
results of some stock screening software. This macro formats the data so it is
more legible, and adds a few columns to perform certain calculations on the
results of the screened data. The end result is a value which is the number of
shares to purchase in order to bring the position to a predetermined value.
The three worksheets are identified, within this macro, by the name containing
either Value, Growth or Tiny, and a different position size constant is used
depending on which sheet it is.
And the format of the three worksheets is not the same (different numbers of
columns), so the extra columns go in a different location depending on which
sheet we are dealing with.
(This is something I've done manually for years; now trying to automate more of
the procedure).
===============================
Option Explicit
Option Compare Text
Sub SetUpSheets()
Dim c As Range
Dim CurPosns As Range
Dim sWSName As String
Dim lNumStocks As Long
Dim sPosnSize As String
Dim ws As Worksheet
For Each ws In Application.ActiveWindow.SelectedSheets
ws.Activate
sWSName = ActiveSheet.Name
lNumStocks = Application.WorksheetFunction.CountA(Range("A:A")) - 1
GetCurrPosns CurPosns
If CurPosns Is Nothing Then
MsgBox ("Need to Copy MSMoney Portfolio Report" & _
vbLf & " Sorted by Position" & vbLf & _
" to Positions Worksheet")
Exit Sub
End If
Worksheets(sWSName).Activate
If Range("A1") <> "Ticker" Then
MsgBox ("Not a Valid Worksheet")
Exit Sub
End If
If InStr(1, sWSName, "Tiny", vbTextCompare) > 0 Then
sPosnSize = "PosnSizeTT"
ElseIf InStr(1, sWSName, "Value", vbTextCompare) > 0 Then
sPosnSize = "PosnSizeValue"
ElseIf InStr(1, sWSName, "Growth", vbTextCompare) > 0 Then
sPosnSize = "PosnSizeGrowth"
Else
MsgBox ("Not a Valid Worksheet")
Exit Sub
End If
Application.ScreenUpdating = False
Rows("1:4").Insert Shift:=xlDown
Set c = Range("A5").End(xlToRight)
c.Offset(-1, 1).FormulaR1C1 = "Current"
c.Offset(0, 1).FormulaR1C1 = "Price"
c.Offset(-1, 2).FormulaR1C1 = "Current"
c.Offset(0, 2).FormulaR1C1 = "Holdings"
c.Offset(-1, 3).FormulaR1C1 = "Shares to"
c.Offset(0, 3).FormulaR1C1 = "Purchase"
c.Offset(0, 4).FormulaR1C1 = "Amount"
With Range("A5").CurrentRegion
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ISODD(ROW())"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
.FormatConditions(1).StopIfTrue = False
End With
c.Offset(1, 1).FormulaR1C1 = "=MSNStockQuote(RC1,""Last"",""US"")"
c.Offset(1, 1).Style = "Currency"
c.Offset(1, 2).FormulaR1C1 = "=IFERROR(VLOOKUP(RC1," &
CurPosns.Worksheet.Name _
& "!" & CurPosns.Address(True, True, xlR1C1) & ",5,FALSE),0)"
c.Offset(1, 2).NumberFormat = "0;0;;"
c.Offset(1, 3).FormulaR1C1 = "=INT(" & sPosnSize & "/RC[-2])-RC[-1]"
c.Offset(1, 3).NumberFormat = "#,##0_);[Red](#,##0)"
c.Offset(1, 4).FormulaR1C1 = "=RC[-1]*RC[-3]+8"
c.Offset(1, 4).NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
Set c = c.Offset(1, 1)
Set c = c.Resize(lNumStocks, 4)
c.FillDown
Set c = c.Offset(-2).Resize(lNumStocks + 2)
c.Columns.AutoFit
Application.ScreenUpdating = True
Next ws
End Sub
===============================
--ron