Change way VBA pastes formula

jas1980

New Member
Joined
Oct 14, 2016
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
I have a formula in P2 that creates a string formula the following macro then places it in to A5, if what is up to (2) is ran it works fine, up to (3) pastes before SendKeys runs so formula isn't active and, if I run to end with or with without (2) then protect is activated before formula.

Is there a better way to place and activate this formula in the whole range so sheet still protects?

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Dim wsheet As Worksheet
For Each wsheet In ActiveWorkbook.Worksheets
wsheet.UNPROTECT Password:="TRADE"
Next wsheet
'(1)****************************************************************************************
Range("P2").Copy
Range("A5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
SendKeys "{F2}"
SendKeys "{ENTER}"
'(2)*****************************************************************************************
Range("A5").Copy Range("A6:A12")
'(3)*****************************************************************************************
filetoopen = ThisWorkbook.Worksheets("CSV COLLECT").Range("V3")
Workbooks.Open fileName:=filetoopen
ActiveWorkbook.Close ("A5")
For Each wsheet In ActiveWorkbook.Worksheets
wsheet.PROTECT Password:="TRADE"
Next wsheet
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If you are just trying to enter the string in P2 as a formula in cell A5, then just replace this:
VBA Code:
Range("P2").Copy
Range("A5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
SendKeys "{F2}"
SendKeys "{ENTER}"
with this:
VBA Code:
Dim frm As String
frm = Range("P2")
Range("A5").Formula = frm
 
Upvote 0
Solution
Amazing, sometimes finding a couple of lines fixes everything. Thankyou
 
Upvote 0
You are welcome!

Yes, I usually try to avoid using "SendKeys" in my VBA code whenever possible. Often times, they are other ways of accomplishing the task (like in this instance).
 
Upvote 0
You are welcome!

Yes, I usually try to avoid using "SendKeys" in my VBA code whenever possible. Often times, they are other ways of accomplishing the task (like in this instance).
Yes I did, I spent a couple of days trying to find an alternative but ended up here, is always good to learn new stuff : )
 
Upvote 0
Yes I did, I spent a couple of days trying to find an alternative but ended up here, is always good to learn new stuff : )
That is what we are here for. Always glad to help!

The trick with this one was setting to a variable first.
When I tried this first:
VBA Code:
Range("A5").Formula = Range("P2")
it did not work.
So I just started experimenting, and found that setting it equal to a variable first worked.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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