Renaming jpg filename

Learning Excel

Board Regular
Joined
Jul 27, 2002
Messages
99
Hi,
I have a list of jpg file in a particular folder like 001.jpg,002jpg.
To make it meaningful, I would like to rename them via VB.
Appreciate help on this.

In my spreadsheet ,starting from A1 in column A are the filename 001.jpg
,002.jpg and I changed the filename in in Column B.

On another note, can the filesize be changed to say 50 KB as each .jpg is about 700KB that make the file very big when I import those pictures.

The pictures are inserted in Column C


Appreciate help on this.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Learning,

To rename your files, you should be able to use the following macro code:

Sub RenameFiles()

Dim iRow As Long

iRow = 1

Do Until IsEmpty(Cells(iRow,"A")) OR IsEmpty(Cells(iRow,"B"))
Name Cells(iRow,"A") As Cells(iRow,"B")
iRow = iRow + 1
Loop

End Sub


This code assumes that the files are in your current working directory. If they aren't, or if you want to rename the files into a different folder you will need to include the desired path along with the file names. For example 001.jpg in column A might correspond to C:\My Documents\My Pictures\Temp\Pic001.jpg in column B.

This macro should be placed in a standard macro module. To do this, go to the VBE (keyboard Alt-TMV), insert a new macro module (Alt-IM), and paste this code into the Code pane. Then run the macro via Excel Alt-TMM.

Regarding your second question, to reduce the size of the files you will need to use a picture editing program. I suggest using MS Photo Editor that comes with Office. To reduce a picture by the ratio of 50/700 in Photo Editor simply go to Image > Resize and dial the Width (and Height) to approximately 25%. Then do a SaveAs to save the reduced file. Then import the reduced files into Excel.

If you are planning to insert the pictures into column C manually, may I suggest an automated method? Do a search on this board for the ShowPicD function. This function will insert a picture into the cell containing the function.

Damon
 
Upvote 0
Hi Damon,
Thank you for the quick response, but it is not changing the name.

What I actually need is to change the filename in my computer
Example from 001.jpg to Cap.jpg
002.jpg to Bottle.jpg


Cell =A1 001.jpg ,Cap.jpg is in Cell B1
Cell =A2 002.jpg ,Bottle.jpg is in Cell B2

After running the macro, the filename in my computer will change from
001.jpg to Cap.jpg


Can add this into the macro to default the directory ?
SetPath = "C:\Documents\My Documents\My Pictures\temp\"

Appreciate your help on this.

Learning
 
Upvote 0
Hi,

Code:
Sub ReName()
Dim fPath   As String
fPath = "C:\Documents\My Documents\My Pictures\temp\"
Dim OldName, NewName

With Application.FileSearch
        .NewSearch
        .LookIn = fPath
        .Filename = "*.jpg"
        If .Execute > 0 Then
            For i = 1 To .FoundFiles.Count
                OldName = .FoundFiles(i)
                NewName = fPath & Cells(i, 2).Text
                Name OldName As NewName
            Next i
        Else
        MsgBox "There were no files found"
        Exit Sub
        End If
    End With
End Sub

HTH
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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