need help so that the results can be as I want

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear All master,
if i use the code below after select range in column a then the results appear in column b and i want after column b the desired results appear in column c. Please solve

thanks
roykana
VBA Code:
Sub GetFileNamebykana()

Dim title As String
Dim Rng As Range
Dim selection As Range
Dim splitpath As Variant

On Error Resume Next
title = "VBA by kana"


Set selection = Application.selection
Set selection = Application.InputBox("Range", title, selection.Address, Type:=8)
For Each Rng In selection
    splitpath = VBA.Split(Rng.Value, "\")
    Rng.Offset(0, 1).Value = splitpath(UBound(splitpath, 1))

Next
End Sub
 

Attachments

  • desired result.JPG
    desired result.JPG
    54.3 KB · Views: 12

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I think you just need to change this:
VBA Code:
Rng.Offset(0, 1)
to this:
VBA Code:
Rng.Offset(0, 2)
 
Upvote 0
I think you just need to change this:
VBA Code:
Rng.Offset(0, 1)
to this:
VBA Code:
Rng.Offset(0, 2)
dear mr. joe4,
thanks for your reply. I tried the code from you, after selecting the range column a, the results appear in column b & c but the results in column c are not what I want, you can see the screenshot that I marked in yellow below.
thanks
roykana
 

Attachments

  • desired result.JPG
    desired result.JPG
    54.3 KB · Views: 4
Upvote 0
I cannot do anything with am image of the data like that. Can you please use the "XL2BB" tool to post your data? That lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
dear mr. joe,
VBA-Extract Filename From Full Path.xlsm
ABC
1PATHFILE NAME WITH EXT DESIRED RESULT
2C:\Users\ADMIN2\Documents\foto 03-08-2021\891170(1).jpg891170(1).jpg891170
3C:\Users\ADMIN2\Documents\foto 03-08-2021\89109.jpg89109.jpg89109
4\\server-pc\Katalog\KATALOG MALFIN 2015\AEP\56008(1)-A.jpg56008(1)-A.jpg56008
5\\server-pc\Katalog\KATALOG MALFIN 2015\AEP\56127-A.jpg56127-A.jpg56127
6\\server-pc\Katalog\KATALOG_TravelPlus\P013M..jpgP013M..jpgP013M.
7\\server-pc\Katalog\KATALOG_KaryaAsiaJaya\K9569L..jpgK9569L..jpgK9569L.
8\\server-pc\Katalog\KATALOG_KaryaAsiaJaya\2019 ..jpg2019 ..jpg2019 .
Sheet2


thanks
roykana
 
Upvote 0
OK, I misunderstood your question initially.
So what you are saying is that what you are showing in column B is what you are currently getting, but you want to actually get what you show in column C. Is that correct?
If so, I think further explanation is required.

It initially looks like you just want to remove the ".jpg" off of the end of each entry, but then it appears that you also sometimes want to remove things like "(1)" and "-A".
So I thought maybe you wanted to remove everything up to the first text letter it finds, but then that would not work for your last three entries.

So I am not seeing the pattern in determining which characters you want to show and which you do not.
Can you please list out all the rules (in plain English) we need to follow in order to determine what it is that you want returned?
 
Upvote 0
OK, I misunderstood your question initially.
So what you are saying is that what you are showing in column B is what you are currently getting, but you want to actually get what you show in column C. Is that correct?
Yes that's correct
It initially looks like you just want to remove the ".jpg" off of the end of each entry, but then it appears that you also sometimes want to remove things like "(1)" and "-A".
So I thought maybe you wanted to remove everything up to the first text letter it finds, but then that would not work for your last three entries.

I want to remove something like (.jpg,(1),(2),(3),(1)-A,-A,-1) and if there is an addition that I want to remove apart from mentioning please can you comment on the code so I can add it alone.
do you understand from my answer?
Thanks
 
Upvote 0
OK, here is just a "guess" as how you want it to work. It works on all your sample data, but I don't know if I have interpretted all your conditions correctly (or if you provided a complete list):
VBA Code:
Sub GetFileName2()

    Dim rng As Range
    Dim temp As String
    Dim arr1() As String
    Dim arr2() As String
    Dim arr3() As String

    For Each rng In selection
        temp = Replace(rng.Value, ".jpg", "")
        arr1 = Split(temp, "\")
        arr2 = Split(arr1(UBound(arr1, 1)), "(")
        arr3 = Split(arr2(0), "-")
        rng.Offset(0, 1).Value = arr3(0)
    Next rng

End Sub
 
Upvote 0
OK, here is just a "guess" as how you want it to work. It works on all your sample data, but I don't know if I have interpretted all your conditions correctly (or if you provided a complete list):
VBA Code:
Sub GetFileName2()

    Dim rng As Range
    Dim temp As String
    Dim arr1() As String
    Dim arr2() As String
    Dim arr3() As String

    For Each rng In selection
        temp = Replace(rng.Value, ".jpg", "")
        arr1 = Split(temp, "\")
        arr2 = Split(arr1(UBound(arr1, 1)), "(")
        arr3 = Split(arr2(0), "-")
        rng.Offset(0, 1).Value = arr3(0)
    Next rng

End Sub
thanks for your reply. I tried your code and it worked and I tried modifying it with the input box with range but I want the file name to appear in the column B with the extension
VBA Code:
Sub GetFileName2()
    Dim title As String
    Dim rng As Range
    Dim temp As String
    Dim arr1() As String
    Dim arr2() As String
    Dim arr3() As String
On Error Resume Next
title = "VBA by kana"


Set selection = Application.selection
Set selection = Application.InputBox("Range", title, selection.Address, Type:=8)
    For Each rng In selection
        temp = Replace(rng.Value, ".jpg", "")
        arr1 = Split(temp, "\")
        arr2 = Split(arr1(UBound(arr1, 1)), "(")
        arr3 = Split(arr2(0), "-")
        rng.Offset(0, 2).Value = arr3(0)
    Next rng

End Sub
 
Upvote 0
thanks for your reply. I tried your code and it worked and I tried modifying it with the input box with range but I want the file name to appear in the column B with the extension
VBA Code:
Sub GetFileName2()
    Dim title As String
    Dim rng As Range
    Dim temp As String
    Dim arr1() As String
    Dim arr2() As String
    Dim arr3() As String
On Error Resume Next
title = "VBA by kana"


Set selection = Application.selection
Set selection = Application.InputBox("Range", title, selection.Address, Type:=8)
    For Each rng In selection
        temp = Replace(rng.Value, ".jpg", "")
        arr1 = Split(temp, "\")
        arr2 = Split(arr1(UBound(arr1, 1)), "(")
        arr3 = Split(arr2(0), "-")
        rng.Offset(0, 2).Value = arr3(0)
    Next rng

End Sub
and I have moved the desired results in column c
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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