bobbythefish
Technical User
I am trying to copy/paste special (to overwrite embedded conditional) a range of cell in one worksheet, based upon a variable in a sheet from another book. Any suggestions?
thanks
bobby
thanks
bobby
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Account Range
------- -------
123 'A1:A26
124 'B1:B37
125 'C1:C18
Account Range Name
------- ----------
123 'XYZ_DATA
124 'ABC_DATA
125 'FOO_DATA
A B C
1 1234 1235 1236
2 IBM MSFT C
3 MRK F PFI
4 INTC HUF APC
A B C D
1 1234 LAST 1235 LAST
2 IBM 40.51 MSFT 80.55
3 MRK 50.24 F 10.23
4 INTC 18.11 HUF 9.75
F1: 'Column:'
G1: =MATCH(Y42,'[ALL-Options.xls]holdings'!$A$1:$DZ$1,0)
B7: =INDEX('[ALL-Options.xls]holdings'!$A$1:$DZ$32,CELL("row",B7),$G$1)
Option Explicit
Const BOOK_BFOPTIONS = "BF-Options.xls"
Const BOOK_ALL = "ALL-Options.xls"
Const SHEET_BFOPTIONS = "Sheet1"
Const SHEET_HOLDINGS = "holdings"
Const SHEET_STRIKES = "strikes"
Const ROW_ACCOUNTLIST = 1
Const ROW_FIRSTTICKER = 7
Const ROW_MAXROWS = 33
Const ROW_POSTSTRIKES = 5
Const COL_TICKERS = 2 'Column "B"
Const COL_PRICES = 8 'Column "H"
Const ROW_COPYFROM = 7
Const ROW_COPYTO = 5
Const ROWS_TO_CLEAR = 26
Const COLS_TO_CLEAR = 2
Const CELL_ACCOUNTID = "Y42"
Const CELLS_LOOKUP = "$A1:$IV1"
Sub GrabStrikePrices()
' Routine to copy holdings and prices
' from this sheet to BOOK_ALL, SHEET_STRIKES
' Copy ticker symbols from COL_TICKERS
' and strike prices from COL_PRICES
Dim rngLookup As Range
Dim rngFound As Range
Dim rngTickers As Range
Dim rngPrices As Range
Dim rngCopyFrom As Range
Dim rngCopyTo As Range
Dim rngToClear As Range
Dim sAccountID As String
Dim nLastRow As Integer
Dim nCopyToColumn As Integer
Dim nAccountColumn As Integer
Dim nRowsToClear As Integer
Windows(BOOK_BFOPTIONS).Activate
Worksheets(SHEET_BFOPTIONS).Activate
' Find last row with a ticker
Set rngLookup = Range(Cells(ROW_COPYFROM, COL_TICKERS), _
Cells(ROW_MAXROWS, COL_TICKERS))
Set rngFound = rngLookup.Find(What:="0", LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
nLastRow = ROW_MAXROWS
On Error Resume Next
nLastRow = rngFound.Row - 1
On Error GoTo 0
' Set Tickers range
Set rngTickers = Range(Cells(ROW_COPYFROM, COL_TICKERS), _
Cells(nLastRow, COL_TICKERS))
' Set Prices range
Set rngPrices = Range(Cells(ROW_COPYFROM, COL_PRICES), _
Cells(nLastRow, COL_PRICES))
' Set copy-from range and copy to clipboard
Set rngCopyFrom = Union(rngTickers, rngPrices)
Application.CutCopyMode = False
rngCopyFrom.Copy
' Remember some items from this book before leaving...
sAccountID = Range(CELL_ACCOUNTID).Value
nRowsToClear = ROWS_TO_CLEAR - rngCopyFrom.Rows.Count
' Find where to copy to -- do it quietly
Application.ScreenUpdating = False
Windows(BOOK_ALL).Activate
Worksheets(SHEET_STRIKES).Activate
Set rngLookup = Range(CELLS_LOOKUP)
Set rngFound = rngLookup.Find(What:=sAccountID, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
nAccountColumn = -1
On Error Resume Next
nAccountColumn = rngFound.Column
On Error GoTo 0
If nAccountColumn = -1 Then
' Not foundWindows(BOOK_BFOPTIONS).Activate
Windows(BOOK_BFOPTIONS).Activate
Worksheets(SHEET_BFOPTIONS).Activate
Application.ScreenUpdating = True
Application.CutCopyMode = False
MsgBox "Couldn''t find account " + sAccountID _
+ " in sheet " + SHEET_STRIKES + " -- job cancelled"
Exit Sub
End If
' Paste new data
Set rngCopyTo = Cells(ROW_COPYTO, nAccountColumn)
ActiveSheet.Paste rngCopyTo
Application.CutCopyMode = False
'Clear unused rows
If nRowsToClear > 0 Then
Set rngToClear = Range(rngCopyTo.Cells(ROWS_TO_CLEAR - nRowsToClear + 1, 1), _
rngCopyTo.Cells(ROWS_TO_CLEAR, COLS_TO_CLEAR))
rngToClear.Clear
End If
' Wrap it up
rngCopyTo.Select
Windows(BOOK_BFOPTIONS).Activate
Worksheets(SHEET_BFOPTIONS).Activate
Application.ScreenUpdating = True
Set rngPrices = Nothing
Set rngTickers = Nothing
Set rngCopyFrom = Nothing
Set rngCopyTo = Nothing
Set rngFound = Nothing
Set rngLookup = Nothing
Set rngToClear = Nothing
End Sub
A1: '4001123
B1: '4001124
C1: '4001125
D1: '4001126
C1: 4001
E1: 123
Y42: =C1&E1
G1: =MATCH(Y42,'[ALL-Options.xls]holdings'!$A$1:$F$1,0)
=CELL("row",B7)
F1: 'Column:'
G1: =MATCH(Y42,'[ALL-Options.xls]holdings'!$A$4:$DX$4,0)
B7: =INDEX('[ALL-Options.xls]holdings'!$A$5:$DX$30,CELL("row",B7)-6,$G$1)
Const ROW_ACCOUNTLIST = 4
'<--- changed from 1
Const CELLS_LOOKUP = "$A4:$IV4"
'<---- changed from row 1
OPT-Sheet
Const SHEET_BFOPTIONS = "OPT-Sheet"
'<--- changed from "Sheet1"
Const BOOK_BFOPTIONS = "BF-Auto-options.xls"
'<--- changed from "BF-Options.xls"
Const SHEET_BFOPTIONS = "OPT-SHEET"
'<--- changed from "OPT-Sheet"
Const SHEET_HOLDINGS = "sheet4"
'<--- changed from "holdings"
const
Application.ScreenUpdating = False
Windows(BOOK_ALL).Activate
Worksheets(SHEET_STRIKES).Activate
Set rngLookup = Range(CELLS_LOOKUP)
Set rngFound = rngLookup.Find(What:=sAccountID, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
nAccountColumn = -1
On Error Resume Next
nAccountColumn = rngFound.Column
On Error GoTo 0
If nAccountColumn = -1 Then
' Not foundWindows(BOOK_BFOPTIONS).Activate
Set rngPrices = Range(Cells(ROW_COPYFROM, COL_PRICES), _
Cells(nLastRow, COL_PRICES))
Set rngPrices = Range(Cells(7, 8), Cells(nLastRow, 8))
Set rngPrices = Range("H7:H" & nLastRow)
Option Explicit
G1: =MATCH(Y42,'[ALL-Options.xls]sheet4'!$4:$4,0)
const