format the cells in column

KlausW

Active Member
Joined
Sep 9, 2020
Messages
386
Office Version
  1. 2016
Platform
  1. Windows
Hi
I use this VBA code to rename images and it works well.
I have done something so that the text in column D, new image name cannot be recognised. And I for the following error message.
I can't figure out how to format the cells in column D so that the VBA code can use them.
Anyone who can help?
Any help will be appreciated.
Best regards
Klaus W

File for help

2023-04-27.png







VBA Code:
Sub Rektangelafrundedehjørner1773_Klik()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Ark1")

Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
 
Dim new_name As String
 
Set fo = fso.GetFolder(sh.Range("e1").Value)
 
For Each f In fo.Files
      new_name = Application.VLookup(f.Name, sh.Range("A:d"), 4, 0)
      f.Name = new_name
Next
 
MsgBox "Done"


End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The problem is that you are now using numbers as file names. Is it just for testing or it will always be like this?
 
Upvote 0
So let's go back to post #2 (as in Monopoly) updated to the layout of latest attached file (sheet Ark1). Works only with names that aren't single numbers:
VBA Code:
Sub Rektangelafrundedehjørner1773_Klik()
    Dim sh     As Worksheet
    Set sh = ThisWorkbook.Sheets("Ark1")
    Dim fso    As New FileSystemObject
    Dim fo     As Folder
    Dim f      As File
    Dim new_name As String
    Set fo = fso.GetFolder(sh.Range("E1").Value)
    On Error Resume Next
    For Each f In fo.Files
        new_name = Application.VLookup(Left(f.Name, InStr(f.Name, ".") - 1), sh.Range("A:D"), 4, 0)
        If Not new_name = "" Then f.Name = new_name
    Next
    MsgBox "Done"
End Sub
 
Upvote 0
Solution
Thankssssssssssssssss allot, now I see, no numbers. It works, just a question, is it not allowed to have numbers in the file name?
Have a nice weekend
Best regards Klaus W
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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