Left of character

Takes2ToTango

Board Regular
Joined
May 23, 2023
Messages
69
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Is there a way to change the below code to do the following:
Grab all characters LEFT of '/' from databodyrange.copy and then paste into the below range?

VBA Code:
 ws.ListObjects("List").ListColumns(5).DataBodyRange.Copy
 wsasn.Range("G2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hmm try something like this:

VBA Code:
a = ws.ListObjects("List").ListColumns(5).DataBodyRange.Value
    For i = LBound(a) To UBound(a)
        On Error Resume Next   ' just in case if there is no / in string
        a(i, 1) = Left(a(i, 1), InStr(1, a(i, 1), "/") - 1)
        On Error GoTo 0
    Next i
wsasn.Range("G2").Resize(UBound(a), 1).Value = a
 
Upvote 0
If I have understood (from the very brief description & no sample data or expected results ;)) then I think this may do them all at once.

VBA Code:
ws.ListObjects("List").ListColumns(5).DataBodyRange.Copy
With wsasn.Range("G2").Resize(ws.ListObjects("List").DataBodyRange.Rows.Count)
  .PasteSpecial Paste:=xlPasteValues
  .Replace What:="/*", Replacement:="", LookAt:=xlPart
End With
 
Upvote 0
Solution
Thank you both for your replies. I'll give you a bit more of a description to help!

So basically I have a table and one column has a list of numbers in this format - 1234567/003

Basically I want to copy and paste the 1234567 into a column of table 2 and then copy and paste the 003 into another column of table 2

Table 1

1234567/003

Table 2
Number 1Number 2
1234567003

so it would look like the above.

I hope this helps!
 
Upvote 0
Quickly (not tested):

VBA Code:
sub Abracadabra()
Dim a, b, c As Variant
a = ws.ListObjects("List").ListColumns(5).DataBodyRange.Value
    For i = LBound(a) To UBound(a)
        On Error Resume Next   ' just in case if there is no / in string
        b(i) = Left(a(i, 1), InStr(1, a(i, 1), "/") - 1)                             'whatever is before /
        c(i) = Trim(Mid(a(i, 1), InStr(1, a(i, 1), "/") + 1, 10))             'whatever is after /
        On Error GoTo 0
    Next i

wsasn.Range("G2").Resize(UBound(b), 1).Value = a        'column before /
wsasn.Range("H2").Resize(UBound(c), 1).Value = a        'column after /

end sub
 
Upvote 0
Quickly (not tested):

VBA Code:
sub Abracadabra()
Dim a, b, c As Variant
a = ws.ListObjects("List").ListColumns(5).DataBodyRange.Value
    For i = LBound(a) To UBound(a)
        On Error Resume Next   ' just in case if there is no / in string
        b(i) = Left(a(i, 1), InStr(1, a(i, 1), "/") - 1)                             'whatever is before /
        c(i) = Trim(Mid(a(i, 1), InStr(1, a(i, 1), "/") + 1, 10))             'whatever is after /
        On Error GoTo 0
    Next i

wsasn.Range("G2").Resize(UBound(b), 1).Value = a        'column before /
wsasn.Range("H2").Resize(UBound(c), 1).Value = a        'column after /

end sub
Thanks for your response.

I seem to be getting a type mismatch on the below
For i = LBound(a) To UBound(a)
 
Upvote 0
Thank you Peter, your example did what I was after.

Thank you both for all your help!
 
Upvote 0
Error in line with For? Weird. Are data there in column 5 on this Table called "List"?

BTW, obviously should be b and c, not a on bottoms lines

VBA Code:
Sheet3.Range("G2").Resize(UBound(b), 1).Value = b        'column before /
Sheet3.Range("H2").Resize(UBound(c), 1).Value = c        'column after /
 
Upvote 0
Thank you Peter, your example did what I was after.
:confused: Did it? I thought that you now wanted both columns
I want to copy and paste the 1234567 into a column of table 2 and then copy and paste the 003 into another column of table 2
Not sure it will go with tables in the destination if they are formal tables but otherwise this should give you both columns as once.

VBA Code:
ws.ListObjects("List").ListColumns(5).DataBodyRange.Copy
With wsasn.Range("G2").Resize(ws.ListObjects("List").DataBodyRange.Rows.Count)
  .PasteSpecial Paste:=xlPasteValues
  .TextToColumns DataType:=xlDelimited, Other:=True, OtherChar:="/"
End With
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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