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: 14
I thought you wanted what you showed in column C? Are you saying that you want BOTH column B and column C of your original post?
(I thought column B was what you were currently getting, but did not want that, you wanted what was in column C).

If you want BOTH, then try this:
VBA Code:
Sub GetFileName2()

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

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

End Sub
If that is not what you are after, please post a sample showing EXACTLY what you want to see in both columns B and C.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I thought you wanted what you showed in column C? Are you saying that you want BOTH column B and column C of your original post?
(I thought column B was what you were currently getting, but did not want that, you wanted what was in column C).

If you want BOTH, then try this:
VBA Code:
Sub GetFileName2()

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

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

End Sub
If that is not what you are after, please post a sample showing EXACTLY what you want to see in both columns B and C.
thanks for your reply. It's been working the way I wanted it to. but what if in the future I want to add criteria that I want to remove?
 
Upvote 0
thanks for your reply. It's been working the way I wanted it to. but what if in the future I want to add criteria that I want to remove?
You can see how I added multiple arrays, one for each condition and used the Split command to split it there.
Just add another array variable and follow the same pattern.
 
Upvote 0
You can see how I added multiple arrays, one for each condition and used the Split command to split it there.
Just add another array variable and follow the same pattern.
Sorry for the late reply, thanks for the help.
thanks for the explanation and guide.
you are a master.
 
Upvote 0
Dear Mr. Joe,
I have a little problem with the code you provided and my problem is marked in yellow.
PROBLEM.JPG

the result should be like below:
RESULT.JPG

Thanks
roykana
 
Upvote 0
Because you have leading zeroes you wish to maintain, we need to pre-format the cell as "Text" so it won't drop them, i.e.
Rich (BB code):
Sub GetFileName2()

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

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

End Sub
 
Upvote 0
Because you have leading zeroes you wish to maintain, we need to pre-format the cell as "Text" so it won't drop them, i.e.
Rich (BB code):
Sub GetFileName2()

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

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

End Sub
Dear Mr. Joe,
thanks and it worked perfectly. One more I how to code without using selection so I want to use sheet name ("MASTER") and start from range ("A2"). because if in the selection it might run slowly and also without the selection so it's easier. I hope you have a solution so it goes fast

Thanks
roykana
 
Upvote 0
Dear Mr. Joe,
thanks and it worked perfectly. One more I how to code without using selection so I want to use sheet name ("MASTER") and start from range ("A2"). because if in the selection it might run slowly and also without the selection so it's easier. I hope you have a solution so it goes fast

Thanks
roykana

and also there are thousands of records, do you need to use a range array or other solution that you definitely know
 
Upvote 0
thanks and it worked perfectly. One more I how to code without using selection so I want to use sheet name ("MASTER") and start from range ("A2"). because if in the selection it might run slowly and also without the selection so it's easier. I hope you have a solution so it goes fast
Note that the use of "Selection" in the code is not slowing anything down, because the code itself is not selecting anything, it is just running off the range that is selected at the time the code is run.
We can suppress screen updating, which should help speed up the code.
We can also dynamically run it against all records in column A with data.

So those updates would look like this:
VBA Code:
Sub GetFileName2()

    Dim lr As Long
    Dim rng As Range
    Dim arr1() As String
    Dim arr2() As String
    Dim arr3() As String

    Application.ScreenUpdating = False

'   Find last row in column A with data
    Sheets("Master").Select
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Pre-format column C for text
    Columns("C:C").NumberFormat = "@"

'   Loop through every cell in column A starting in row 2
    For Each rng In Range("A2:A" & lr)
        arr1 = Split(rng.Value, "\")
        rng.Offset(0, 1).Value = arr1(UBound(arr1, 1))
        arr2 = Split(arr1(UBound(arr1, 1)), "(")
        arr3 = Split(arr2(0), "-")
        rng.Offset(0, 2).Value = Replace(arr3(0), ".jpg", "")
    Next rng

    Application.ScreenUpdating = True

End Sub
Let me know if that is fast enough. If not, I have another idea that does not involve loops.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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