Replacing column value through VBA

alibawa

New Member
Joined
Sep 21, 2006
Messages
17
Hi,

I have lets say more than 1000 files. What i want to achieve is two things

1. Change the name of all the files.
2. Change the value of the first column in each file.

For e.g. File Names are like "XYZ_201205" , "XYZ_201204". I want to rename them to "ABC_201205', "ABC_201204".

Similary column A has 100 records (no. of records may vary in each file) and the value is fixed "BH1234" in all the files. I want to replace this value with "AE1234" in all the files.

Appreciate ur help and Thanks so much in advance
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,

I have lets say more than 1000 files. What i want to achieve is two things

1. Change the name of all the files.
2. Change the value of the first column in each file.

For e.g. File Names are like "XYZ_201205" , "XYZ_201204". I want to rename them to "ABC_201205', "ABC_201204".

Similary column A has 100 records (no. of records may vary in each file) and the value is fixed "BH1234" in all the files. I want to replace this value with "AE1234" in all the files.

Appreciate ur help and Thanks so much in advance

This will change your file names, but I did not include anything for the records in column A because the parameters are too vague. No sheet specified. It appears that you only want the first two characters changed, which is simple enough, but is it the value that is fixed or the configuration, ie. two alpha, four numeric?

Here is the code to change the file names:

Code:
Sub ChngFileNm()
Dim OldName As String, newName As String, sPath As String
sPath = ThisWorkbook.Path 'Change to path to be searched if other than ThisWorkbook.
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
SName = Dir(sPath & "*.xls*")
Do
If Left(SName, 4) = "xyz_" Then
OldName = SName
newName = Replace(SName, "xyz", "abc")
Name OldName As newName
End If
SName = Dir
Loop While SName <> ""
End Sub
Code:
 
Upvote 0
Just on the chance that your workbooks are one sheet workbooks and that you have a consistent two alpha prefix of "BH" or whatever it might be, but consistent, here is a procedure that should do the whole job.

Code:
Sub ChngFileNm()
Dim OldName As String, newName As String, sPath As String, c As Range
sPath = ThisWorkbook.Path 'Change to path to be searched if other than ThisWorkbook.
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
SName = Dir(sPath & "*.xls*")
Do
If Left(SName, 4) = "xyz_" Then
OldName = SName
newName = Replace(SName, "xyz", "abc")
Name OldName As newName
Set wb = Workbooks.Open(newName)
lr = wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
For Each c In wb.Sheets(1).Range("A2:A" & lr)
c = Replace(c, "BH", "AE")
Next
End If
SName = Dir
Loop While SName <> ""
End Sub
Code:
 
Upvote 0
Oops! Forgot to close the workbooks. You don't want 100 open workbooks.

Code:
Sub ChngFileNm()
Dim OldName As String, newName As String, sPath As String, c As Range
sPath = ThisWorkbook.Path 'Change to path to be searched if other than ThisWorkbook.
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
SName = Dir(sPath & "*.xls*")
Do
If Left(SName, 4) = "xyz_" Then
OldName = SName
newName = Replace(SName, "xyz", "abc")
Name OldName As newName
Set wb = Workbooks.Open(newName)
lr = wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
For Each c In wb.Sheets(1).Range("A2:A" & lr)
c = Replace(c, "BH", "AE")
Next
wb.Close True
End If
SName = Dir
Loop While SName <> ""
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,427
Messages
6,178,585
Members
452,860
Latest member
jroberts02

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