VBA copy only cells with value, no formulas

Posted on 2012-12-30
Last Modified: 2013-01-03
Hello Experts,

I am feeling stupid - I need some help with something that is escaping me.

I want to copy all data from workbook 1, sheet 1, usedrange, to workbook2, sheet 1 -
however I want to only copy cells that contain an integer and skip any cell that contains a formula.  All cells are locked and all integers are positive.
My usedrange = ("D2:AA60")

The reason I am doing this is because the sheets are identical - however, I have modified the formulas in workbook 2 compared to workbook 1 - and I do not want to overwrite them.

Thank you,
Question by:mike637
LVL 48

Accepted Solution

Rgonzo1971 earned 300 total points
ID: 38732024

You could use this function to determine formula

Function IsFormula(rng As Range)
If rng.HasFormula Then IsFormula = True
End Function

Looping through the cells like
Sub DoTheJob()
Dim c As Range
Set MyUsedRange = Range("D2:d6")
For Each c In MyUsedRange
   If c.HasFormula = False Then
      c.Copy Destination:=Worksheets("Sheet2").Range(c.Address)
   End If

End Sub

Open in new window

LVL 14

Expert Comment

ID: 38732122
You might use ActiveSheet.Cells.SpecialCells(xlCellTypeConstants) to identify cells that have values other than formulas in them. Refer to Microsoft MSDN Help for full syntax.
LVL 45

Expert Comment

by:Martin Liss
ID: 38732863
Unless you really mean that if a cell contains "abc" or other text that you don't want to copy it then this should do what you want. Change the "Sheet2" destination to reflect the destination workbook and sheet.

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

Open in new window

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 200 total points
ID: 38732931
And if you do want to skip non-numerics you can do this.

Dim c As Range

For Each c In Sheets("Sheet1").Range("D2:AA60")
    If IsNumeric(c) Then
        Sheets("Sheet2").Range(c.Address).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    End If

Open in new window


Author Closing Comment

ID: 38741439
Thank you Experts.

Your help was appreciated.  My brain was not working at the time.

LVL 45

Expert Comment

by:Martin Liss
ID: 38742134
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now