get one value from a csv file and copy it in a fixed cell in all the csv files in a folder

Status
Not open for further replies.

quiellx

New Member
Joined
Nov 6, 2022
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hi all, I have been looking for a macro that could allow me to

1- read a CSV file that has a column with a series of id values, similar to this.

enter image description here

2- Get those values and, one by one, open all the CSV files in a certain folder then copy a value in a fixed cell for each one, in the same order in which those IDs were ordered in the "master" CSV.
The result would be a number n of CSV files that were previously present in a certain folder and that now have an ID value in a particular column.

Any idea how I could do it with a macro or within excel?

Thanks for any suggestions or ideas.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

browsing a bit i found this piece of code

Sub AllWorkbooks()
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim MyFile As String 'Filename obtained by DIR function
Dim wbk As Workbook 'Used to loop through each workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder & "*.csv", vbNormal) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
'Replace the line below with the statements you would want your macro to perform
Sheets(1).Range("a1").Value = 20
wbk.Close savechanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub

This one is correctly iterating all the files in a selected folder, and in this case, i tried copying to each one of those CSV files the number 20 in cell a1, and worked.. Now my doubt is that i need to copy a list of values in a master workbook one by one in each of those CSV files that I previously iterated. Any clue about I could do it?
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

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