VBA to Paste to a Named Range skipping some cells based on Adjacent Cell value in another Named range

AndyCoder

New Member
Joined
Oct 9, 2017
Messages
5
I have search for 2 days only to find complex solutions. What i want to do is simpler. I have a current working VBA sub that selects a formula from one cell, named ("StockPriceFormula") and copy/pastes down a named range ("CurrStockPrcRange").

Current Working VBA:
=====================
Sub UpdateAllQuotes()

Range("StockPriceFormula").Select
Selection.Copy

Application.Goto Reference:="CurrStockPrcRange"
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

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

What I want to add to that code is to SKIP pasting the formula to cells correspond to a value of the letter "C" in an adjacent Named Range. i.e. Range("StatusRange) <> "C"

What I want to add to the above VBA:
=====================
Sub UpdateAllQuotes()

Range("").Select
Selection.Copy

Application.Goto Reference:="CurrStockPrcRange"

'(ADD VBA code/Loop here to ONLY Paste if adjacent cell in Range("StatusRange") does
NOT have the letter "C" in it)

Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

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

I want to use "Named Ranges" not Column/Cell Letters/Numbers because the size and location of ranges changes depending on how my items are in the worksheet.



Example Worksheet:

[TABLE="class: grid, width: 10, align: left"]
<tbody>[TR]
[TD]
A​
CurrStocPrcRange[/TD]
[TD="align: center"]B
SYMBOL[/TD]
[TD="align: center"]C
Status[/TD]
[/TR]
[TR]
[TD]skip this cell[/TD]
[TD]XYZ[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]36.95[/TD]
[TD]ABC[/TD]
[TD]O[/TD]
[/TR]
[TR]
[TD]skip this cell[/TD]
[TD]DFG[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]43.50[/TD]
[TD]UV[/TD]
[TD]O[/TD]
[/TR]
</tbody>[/TABLE]










SFMCvHOv6MhBneR4o09e2_kwne6Fa6ptYMDMBg3Da0s
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Any Help on This?????

Update: I found a solution to skip Locked cells (see code below). Tested it and it works. However still haven't come up with away to skip cells based on the adjacent cell value as described above.

===================
Sub UpdateStockQuotes()
'Copy/paste (Fill down) formula to specified range (Skipping Locked Cells)


Dim cell As Range, MyRange As Range, SelStockRange As Range
Set MyRange = Range("CurrStockPrcRange")

For Each cell In MyRange.Cells 'Select Non-Locked Cells (Closed Trades) ONLY and assign those cells to SelStockRange Variable
If Not cell.Locked Then
If Not SelStockRange Is Nothing Then
Set SelStockRange = Union(cell, SelStockRange)
Else
Set SelStockRange = cell
End If
End If
Next cell

Range("StockPriceFormula").Select 'selects template formula location
Selection.Copy
Application.Goto Reference:=SelStockRange 'go to SelStockRange (selected non-locked cells) and paste formulas to those cells
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False


End Sub
======================
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top