VBA code to copy a column as value

onche414

New Member
Joined
Aug 7, 2023
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I would like to create a VBA code which will go through a folder where there are excel files and copy as value the column U of the first sheet of each excel file to the column V.

I came up with this for now but I am getting an error and this code is more likely to copy the column U as value on the column U itself while I want to copy it on column V

VBA Code:
Sub OpenAndPerformMacros()
Dim strF As String, strP As String
Dim wb As Workbook
Dim ws As Worksheet


'Edit this declaration to your folder name
strP = "Path\New folder"


strF = Dir(strP & "\*.xlsx") 'Change as required


Do While strF <> vbNullString


    Set wb = Workbooks.Open(strP & "\" & strF)
    Set ws = wb.Sheets(1).Column("U") 
ws.Cells.Copy
ws.Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
    wb.Close True
    
    strF = Dir()
Loop

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I only tested the copy part but give this a try:

VBA Code:
Sub OpenAndPerformMacros()
Dim strF As String, strP As String
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range

'Edit this declaration to your folder name
strP = "Path\New folder"

strF = Dir(strP & "\*.xlsx") 'Change as required

Do While strF <> vbNullString
    Set wb = Workbooks.Open(strP & "\" & strF)
    Set ws = wb.Sheets(1)
    Set rng = Intersect(ws.Columns("U"), ws.UsedRange)
    rng.Copy
        rng.Offset(, 1).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    wb.Close True
    
    strF = Dir()
Loop

End Sub
 
Upvote 0
Perhaps something like this .

VBA Code:
Sub OpenAndPerformMacros()
    Dim strF As String, strP As String
    Dim wb As Workbook
    Dim ws As Worksheet
    
    
    'Edit this declaration to your folder name
    strP = "Path\New folder"
    strF = Dir(strP & "\*.xlsx") 'Change as required
    
    Do While strF <> vbNullString
        Set wb = Workbooks.Open(strP & "\" & strF)
        Set ws = wb.Sheets(1)
        ws.Columns("V").Value = ws.Columns("U").Value
        wb.Close True
        strF = Dir()
    Loop
End Sub
 
Upvote 0
@rlv01 - you get a noticeable delay by doing the whole column. How about adding something like this.
Power Query:
        With Intersect(ws.Columns("U"), ws.UsedRange)
            .Offset(, 1).Value = .Value
        End With
 
Upvote 0

Forum statistics

Threads
1,223,631
Messages
6,173,465
Members
452,516
Latest member
archcalx

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