need help so that the results can be as I want

roykana

Active Member
Joined
Mar 8, 2018
Messages
309
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: 10

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,502
Office Version
  1. 365
Platform
  1. Windows
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.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

roykana

Active Member
Joined
Mar 8, 2018
Messages
309
Office Version
  1. 2010
Platform
  1. Windows
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?
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,502
Office Version
  1. 365
Platform
  1. Windows
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.
 

roykana

Active Member
Joined
Mar 8, 2018
Messages
309
Office Version
  1. 2010
Platform
  1. Windows
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.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,502
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
:)
 

roykana

Active Member
Joined
Mar 8, 2018
Messages
309
Office Version
  1. 2010
Platform
  1. Windows
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,502
Office Version
  1. 365
Platform
  1. Windows
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
 

roykana

Active Member
Joined
Mar 8, 2018
Messages
309
Office Version
  1. 2010
Platform
  1. Windows
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
 

roykana

Active Member
Joined
Mar 8, 2018
Messages
309
Office Version
  1. 2010
Platform
  1. Windows
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,502
Office Version
  1. 365
Platform
  1. Windows
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.
 

Forum statistics

Threads
1,176,122
Messages
5,901,507
Members
434,898
Latest member
dez091

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
Top