File renaming using cell values

English Puma

New Member
Joined
Jan 25, 2010
Messages
4
Hello All.

I'm trying to write a section of an excel macro which does the folowing:

Starting with cell M2 selected and avtive:

As long as the in column J has a value in (activecell.offset(0,-3)<> vbnullstring), search for a file in a specified folder with the filename in column J.

If the file exists, move the active cell down 1 row and begin the process again

If the file search fails, search for a file which ends with the string in column L (which always has the form ".12345"). Rename that file (there will be one and only ever one), rename it to the value in column J then move the active cell down one row and begin the process again.

I think I can get the Do While set up right but, either can't phrase the file search for "*"&"L2" or the rename file to J bit. Any suggestions???

Thanks

Puma
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This is where I am at the moment (but, it won't compile as it's complaining about a loop without a do... which i'm sure is there!). I feel soooooo close!!!!

Dim fs, i
Dim A As String

Columns("M:M").Select
Selection.Insert Shift:=xlToRight
Range("M2").Select

Range("M2").Activate

Set fs = Application.FileSearch

Do While ActiveCell.Offset(0, -3) <> vbNullString
A = ActiveCell.Offset(0, -3).Value
Set fs = Application.FileSearch
With fs
.LookIn = "C:\TEMP\"
.Filename = A
If .Execute Then
For i = 1 To .FoundFiles.Count
ActiveCell.Offset(1, 0).Activate
Next
Else
Set fs = Application.FileSearch
With fs
.LookIn = "C:\TEMP\"
.Filename = "*" & ActiveCell.Offset(0, -1).Value
If .Execute Then
For i = 1 To .FoundFiles.Count
Name Application.FileSearch.Filename As "*" & ActiveCell.Offset(0, -1).Value
ActiveCell.Offset(1, 0).Activate
Next
End If
End With
Loop
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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