Saving files in TIF format - VBA

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
Hi all,

In Sheet1 column A, I have listed all the files that can be found in "C:\Documents\"

Then I have the following code:

Code:
 Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 ToPath = "C:\Documents\"

 For k = 1 To x
 On Error Resume Next
 Name ToPath & Sheets("Sheet1").Cells(k, 1).Value & ".TIF" As ToPath & 1000 + k * 10 & "   " & Sheets("Sheet1").Cells(k, 1).Value & ".TIF"
 Next k

That code renames the files by adding a string (1000 + k*10) to the filename.

This code works well, except if one of the files are in format other than TIF. The only other format in the folder is PDF, so it could only either be TIF or PDF.

So my question is,. how do I change above in such a way that it should just use whatever is the file extention of the file in the folder?

Thanks in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You can do this without listing the files in Column A, but since you have, why not just check the extension?

BTW, you have not defined a value for x anywhere in your code.
Code:
If Right(Cells(k,1),4)=".TIF" then Name ToPath & Sheets("Sheet1").Cells(k, 1).Value & ".TIF" As ToPath & 1000 + k * 10 & "   " & Sheets("Sheet1").Cells(k, 1).Value & ".TIF"
 
Upvote 0
You can do this without listing the files in Column A, but since you have, why not just check the extension?

BTW, you have not defined a value for x anywhere in your code.
Code:
If Right(Cells(k,1),4)=".TIF" then Name ToPath & Sheets("Sheet1").Cells(k, 1).Value & ".TIF" As ToPath & 1000 + k * 10 & "   " & Sheets("Sheet1").Cells(k, 1).Value & ".TIF"

Hi Hotpepper,

Thanks

I am still relatively new to VBA so these "obvious" solutions are not too obvious for me :)

About x, I accidentally copped my code, x is defined as:

Code:
x=Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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