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:

A​
CurrStocPrcRange
B
SYMBOL
C
Status
skip this cellXYZC
36.95ABCO
skip this cellDFGC
43.50UVO

<tbody>
</tbody>










SFMCvHOv6MhBneR4o09e2_kwne6Fa6ptYMDMBg3Da0s
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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