VBA to Paste from Clipboard to Empty Cells

jmchowbizarre

New Member
Joined
Feb 13, 2015
Messages
6
I am in need of help for VBA code to paste a formula that I will have copied into the clipboard into empty cells only of a range of cells.

Example, my formula is:
=IF(ISNA(VLOOKUP(F100,gpcucndata,3,FALSE)),"",IF(VLOOKUP(F100,gpcucndata,3,FALSE)=0,"",VLOOKUP(F100,gpcucndata,3,FALSE)))

I need to paste that into the empty cells only in selection C6:C18744 (some cells have data I do not want to overwrite)

The range can vary and I'd gladly manually select the range to paste to if necessary.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try something like this:

Code:
Sub CopyFormula()

  'Declare variables:
  Dim rngCopyFrm As Range
  Dim rngPasteTo As Range
  
  'Set the range objects:
  Set rngCopyFrm = Range("A1")  'cell containing formula to copy
  Set rngPasteTo = Range("C6:C18744") 'cells in which to paste formula
  
'  -----------------------------------------
'  Or if you want to use the selected range:
'  Set rngPasteTo = Selection
'  -----------------------------------------
  
  'Re-set paste destination to blank cells only:
  Set rngPasteTo = rngPasteTo.SpecialCells(xlCellTypeBlanks)
  
  'Do the copy and paste special:
  rngCopyFrm.Copy
  rngPasteTo.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats

End Sub
 
Upvote 0
Thank you for the code. I get a 400 error when I try to run the Macro. I've tried to see what the problem is but cannot. Any direction you can provide would be great. Again, thank you for the code.
 
Upvote 0
And here's what I put in based off of reading your comments. I wanted to copy C6 and paste it to the selection I made (which I highlighted C7 to C33 as a test)

Code:
Sub CopyFormula()

  'Declare variables:
  Dim rngCopyFrm As Range
  Dim rngPasteTo As Range
  
  'Set the range objects:
  Set rngCopyFrm = Range("C6")  'cell containing formula to copy
  Set rngPasteTo = Selection
  
  'Re-set paste destination to blank cells only:
  Set rngPasteTo = rngPasteTo.SpecialCells(xlCellTypeBlanks)
  
  'Do the copy and paste special:
  rngCopyFrm.Copy
  rngPasteTo.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats


End Sub
 
Upvote 0
I get a 400 error when I try to run the Macro.

And here's what I put in based off of reading your comments. I wanted to copy C6 and paste it to the selection I made (which I highlighted C7 to C33 as a test)

Code:
Sub CopyFormula()

  'Declare variables:
  Dim rngCopyFrm As Range
  Dim rngPasteTo As Range
  
  'Set the range objects:
  Set rngCopyFrm = Range("C6")  'cell containing formula to copy
  Set rngPasteTo = Selection
  
  'Re-set paste destination to blank cells only:
  Set rngPasteTo = rngPasteTo.SpecialCells(xlCellTypeBlanks)
  
  'Do the copy and paste special:
  rngCopyFrm.Copy
  rngPasteTo.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats


End Sub

You should always tell us the error message as well as the error number. Was it, perhaps, "No cells were found."? If so, then the problem is that the selection is beyond the range of cells with data.. SpecialCells(xlCellTypeBlanks) only finds blanks within the UsedRange for the sheet it is processing.
 
Upvote 0
Why must the formula be pasted from the clipboard?

You can just get the formula directly from the cell that originally contains it (say that was A6 for example)..

Range("C6:C18744").Formla = Range("A6").Formula
 
Upvote 0
I am definitely for trying new things. How would I modify the VBA code to incorporate the above? I've stared at it for a few minutes and am trying to figure out what it would replace. I appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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