VBA - Copying formulas from a workbook - Paste Values

rniculae

New Member
Joined
Apr 9, 2013
Messages
41
Hello,

I've recently created this macro that opens, one by one, all excels in a designated folder and copies the rows from worksheet 2, to a new workbook. Every new excel rows are copied below the one before.

Well, now i want to paste only the values which contains formulas.

I've tried to use .PasteSpecial xlPasteValues and it's not working.
Can you help me?

Thanks,
Razvan

Code:
Public Sub MergeWorkbooks()
    Dim ROOT_FOLDER As String
    Dim wbTarget As Workbook
    Dim Filename As String
    Dim filenames As Variant
    Dim numrows As Long
    Dim nextrow As Long
    ROOT_FOLDER = Sheets("Concatenare fisiere XLS magazin").Range("D11").Value
    
    Set wbTarget = Workbooks.Add
        
    Filename = Dir(ROOT_FOLDER & "*.xlsx")
    ReDim filenames(1 To 1)
     
    nextrow = 1
    rand = 3
    Do While Filename <> ""
         
        Workbooks.Open ROOT_FOLDER & Filename
        With ActiveWorkbook
             
            With .Worksheets(2)
                 
                numrows = .Cells(.Rows.Count, "A").End(xlUp).Row
                If nextrow <> 1 Then rand = 2
                .Rows(rand).Resize(numrows - rand).Copy.PasteSpecial wbTarget.Worksheets(1).Cells(nextrow, "A")
                nextrow = nextrow + numrows - rand
            End With
             
            .Close SaveChanges:=False
        End With
         
        Filename = Dir
    Loop
     
     'do something with wbTarget
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You may want to consider selecting cells that contain a formula. The following code select cells that contain a formula from range("B2:J25") and paste them to sheets(3), starting form A1 down.

Code:
Dim rng1 As Range
Dim cell As Range
Dim rowno As Integer

rowno = 1

Set rng1 = ThisWorkbook.Sheets(2).Range("B2:J25").SpecialCells(xlCellTypeFormulas)

For Each cell In rng1

ThisWorkbook.Sheets(3).Cells(rowno, 1) = cell

rowno = rowno + 1
Next
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,913
Members
449,132
Latest member
Rosie14

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