Solved

Validate Excel via VBA

Posted on 2013-12-17
8
311 Views
Last Modified: 2013-12-18
Hi Experts,

I have excel file with one spreadsheet and one column and in this column I have data.
Some of the cells have in different places substring " - abc .... " which can be a different length. What I would do is to find this substring and whatever is after it till the end of line
copy and paste in another cell and then remove from original cell.

so basically here is example :

Input :
A
aaaaaaaa   - abc 012345
bbbbbbb   - abc 01282319274
cccccccc   - abc 0112

Output :

A               B
aaaaaaaa   - abc 012345
bbbbbbb   - abc 01282319274
cccccccc   - abc 0112


Thanks.
0
Comment
Question by:fpoyavo
  • 3
  • 3
  • 2
8 Comments
 
LVL 22

Expert Comment

by:Flyster
ID: 39725721
Assuming your data is in column A, this will get you the first part:

=LEFT(A1,FIND(" ",A1))

And this will give you the second part:

=RIGHT(A1,LEN(A1)-FIND(" ",A1))

You can then hide column A.

Flyster
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39725770
Not sure if you understood the specs. I didn't ask to hide anything. I needed To find and copy to another cell and then to remove it from original cell.
0
 
LVL 22

Accepted Solution

by:
Flyster earned 500 total points
ID: 39725815
Sorry about that. Here's a macro that will split the string for you:
Sub SplitString()
Dim stra, strb As String
Dim i As Integer

i = InStr(Range("A" & 1), " ")
  For i = 1 To ActiveSheet.UsedRange.Rows.Count
    stra = Left(Range("A" & i), InStr(Range("A" & i), " "))
      strb = Right(Range("A" & i), Len(Range("A" & i)) - InStr(Range("A" & i), " "))
      Range("B" & i).Value = strb
    Range("A" & i).Value = stra
  Next i

End Sub

Open in new window

0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 39726079
You can also use the "Text to Columns" function.

Select the column of data and then on the Data tab click the Text to Columns button.

Step 1 - select Delimited, click OK
Step 2 - check the boxes next to Space and Other and next to Other type - in the box. AT the top right check the box for treating consecutive delimiters as one
Step 3 - Click Finish

This will split the data into two columns.

Thanks
Rob H
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:fpoyavo
ID: 39726429
Hi Rob,

It will not work since not all cells have this " - abc ..."

Thanks.
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 39726463
So those that do not have the  "- abc" will be left as they are. Would that be the result you want anyway?

Thanks
Rob
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39727193
Hi Flyster,

I t gets where " - abc ..." correctly but where I don't have " - abc .." it should not remove in column A anything ... now it does leaves A empty and copies this data into B.

Thanks.
0
 
LVL 22

Expert Comment

by:Flyster
ID: 39727350
Try this one. It will copy only the cells that contains "- abc", the rest will be left untouched:
Sub SplitString()
Dim stra, strb As String
Dim i As Integer

  For i = 1 To ActiveSheet.UsedRange.Rows.Count
    If InStr(1, Range("A" & i), "- abc") <> 0 Then
      stra = Left(Range("A" & i), InStr(Range("A" & i), " "))
      strb = Right(Range("A" & i), Len(Range("A" & i)) - InStr(Range("A" & i), " "))
      Range("B" & i).Value = strb
      Range("A" & i).Value = stra
    End If
  Next i

End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Merging-Splitting-Multiple-Rows 33 46
Message Logon Windows 2012 R2 Server 16 16
VB 2005 Tooltips on Form Load Event 15 16
Vlookup formula error 15 12
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now