Need vba : Extract data after mark , (comma)

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hello all,

perhaps, you can help me how to replace or extract data after mark , (comma) using vba code

like see this :

[TABLE="width: 500"]
<tbody>[TR]
[TD]sample raw [/TD]
[TD]after macro[/TD]
[/TR]
[TR]
[TD]Alam Subur, CV/Komala Budiman[/TD]
[TD]Alam Subur[/TD]
[/TR]
[TR]
[TD]Genteng Garuda Riau Agung, CV[/TD]
[TD]Genteng Garuda Riau Agung[/TD]
[/TR]
[TR]
[TD]Husni, M/Toko Sepatu Bata[/TD]
[TD]Husni[/TD]
[/TR]
[TR]
[TD]Indah Jaya, PT[/TD]
[TD]Indah Jaya[/TD]
[/TR]
[TR]
[TD]etc...[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

for anything for me, thanks in advance...

m.susanto
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
muhammad susanto,

Sample raw data:


Excel 2007
AB
1Alam Subur, CV/Komala Budiman
2Genteng Garuda Riau Agung, CV
3Husni, M/Toko Sepatu Bata
4Indah Jaya, PT
5
Sheet1


After the macro:


Excel 2007
AB
1Alam Subur, CV/Komala BudimanAlam Subur
2Genteng Garuda Riau Agung, CVGenteng Garuda Riau Agung
3Husni, M/Toko Sepatu BataHusni
4Indah Jaya, PTIndah Jaya
5
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub ExtractBeforeComma()
' hiker95, 11/11/2013
' http://www.mrexcel.com/forum/excel-questions/738486-need-visual-basic-applications-extract-data-after-mark-comma.html
Dim a As Variant, b As Variant
Dim i As Long, s
a = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
For i = 1 To UBound(a, 1)
  If InStr(Trim(a(i, 1)), ",") > 0 Then
    s = Split(a(i, 1), ",")
    b(i, 1) = s(0)
  End If
Next i
Range("B1").Resize(UBound(b, 1), UBound(b, 2)) = b
Columns(2).AutoFit
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ExtractBeforeComma macro.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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