473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Copying/Pasting Text from/to Cells

9
Hello There,
I am trying to write a macro that will copy part of Text in a Cell & then paste in another cell.
For example,
assume cell A1="ABE_BC1 AbTS 11 bisTSport 2", I would like to copy/Paste "11" in cell A2 & 2 IN cell A3 .How do i achieve this with a macro instead?
Can anyone help?

CELL A1 = ADDRESS CELL B1 = BOARD CELL C1 = SLOT
CELL A2 = ABE_BC1 AbTS 11 bisTSport 2 CELL B2 = 11 CELL C2 = 12
CELL A3 = ABE_BC1 |AbTS 11 AbTSport 3 CELL B3 = 11 CELL C3 = 3
CELL A4 = ABE_BC1 |AbTS 11 AbTSport 3 CELL B4 = 11 CELL C4 = 3
CELL A5 = ABE_BC1 |AbTS 14 AbTSport 4 CELL B5 = 14 CELL C5 = 4
CELL A6 = UMH_BC1 |AbTS 2 AbTSport 5 CELL B6 = 2 CELL C6= 5
CELL A7 = UMH_BC1 |AbTS 2 AbTSport 5 CELL B7 = 2 CELL C7 = 5
Nov 1 '06 #1
5 1913
Killer42
8,435 Expert 8TB
Hello There,
I am trying to write a macro that will copy part of Text in a Cell & then paste in another cell.
For example,
assume cell A1="ABE_BC1 AbTS 11 bisTSport 2", I would like to copy/Paste "11" in cell A2 & 2 IN cell A3 .How do i achieve this with a macro instead?
Can anyone help?

CELL A1 = ADDRESS CELL B1 = BOARD CELL C1 = SLOT
CELL A2 = ABE_BC1 AbTS 11 bisTSport 2 CELL B2 = 11 CELL C2 = 12
CELL A3 = ABE_BC1 |AbTS 11 AbTSport 3 CELL B3 = 11 CELL C3 = 3
CELL A4 = ABE_BC1 |AbTS 11 AbTSport 3 CELL B4 = 11 CELL C4 = 3
CELL A5 = ABE_BC1 |AbTS 14 AbTSport 4 CELL B5 = 14 CELL C5 = 4
CELL A6 = UMH_BC1 |AbTS 2 AbTSport 5 CELL B6 = 2 CELL C6= 5
CELL A7 = UMH_BC1 |AbTS 2 AbTSport 5 CELL B7 = 2 CELL C7 = 5
Try this.
Expand|Select|Wrap|Line Numbers
  1. Sub DoIt()
  2.   Dim I As Long
  3.   For I = 2 To 7
  4.     Cells(I, 2).Value = Word(3, Cells(I, 1).Value)
  5.     Cells(I, 3).Value = Word(5, Cells(I, 1).Value)
  6.   Next
  7. End Sub
  8.  
  9.  
  10. Private Function Word(DesiredWord As Long, FromString As String) As String
  11.   Dim I As Long, J As Long, Char As String * 1
  12.   Dim WordNum As Long, StartPos As Long, NextSpace As Long
  13.   Dim WhatWasFound As String
  14.   StartPos = 1
  15.   If FromString = "" Then Exit Function
  16.   Do
  17.     WordNum = WordNum + 1
  18.     NextSpace = InStr(StartPos, FromString, " ")
  19.     If NextSpace = 0 Then
  20.       If WordNum = DesiredWord Then
  21.         WhatWasFound = Mid$(FromString, StartPos)
  22.       End If
  23.       Exit Do
  24.     End If
  25.     If WordNum = DesiredWord Then
  26.       WhatWasFound = Mid$(FromString, StartPos, NextSpace - StartPos)
  27.       Exit Do
  28.     End If
  29.     StartPos = NextSpace + 1
  30.   Loop
  31.   Word = Trim$(WhatWasFound)
  32. End Function
  33.  
I have made a lot of assumptions, of course. But I tried to make it flexible where possible. For one thing, the Word() function lets you specify that you want the 1st word, 2nd word or whatever, from the input string. This may not match what you want to do.

My results...
Before
Expand|Select|Wrap|Line Numbers
  1. ADDRESS                           BOARD   SLOT
  2. ABE_BC1 AbTS 11 bisTSport 2              
  3. ABE_BC1 |AbTS 11 AbTSport 3              
  4. ABE_BC1 |AbTS 11 AbTSport 3              
  5. ABE_BC1 |AbTS 14 AbTSport 4              
  6. UMH_BC1 |AbTS 2 AbTSport 5               
  7. UMH_BC1 |AbTS 2 AbTSport 5               
After
Expand|Select|Wrap|Line Numbers
  1. ADDRESS                           BOARD   SLOT
  2. ABE_BC1 AbTS 11 bisTSport 2     11      2
  3. ABE_BC1 |AbTS 11 AbTSport 3     11      3
  4. ABE_BC1 |AbTS 11 AbTSport 3     11      3
  5. ABE_BC1 |AbTS 14 AbTSport 4     14      4
  6. UMH_BC1 |AbTS 2 AbTSport 5      2       5
  7. UMH_BC1 |AbTS 2 AbTSport 5      2       5
Nov 2 '06 #2
Killer42
8,435 Expert 8TB
P.S. One of the assumption I made is that the 12 in C2 was a typo.
Nov 2 '06 #3
DONE1
9
P.S. One of the assumption I made is that the 12 in C2 was a typo.
Thank you, The code works.
But i would like to know what the arguement (DesiredWord) in the Function (Word)
is for.

Private Function Word(DesiredWord As Long, FromString As String) As String
Nov 2 '06 #4
Killer42
8,435 Expert 8TB
Thank you, The code works.
But i would like to know what the arguement (DesiredWord) in the Function (Word)
is for.
Private Function Word(DesiredWord As Long, FromString As String) As String
You use that to say which word you want to pull out of the string. If I remember correctly, in the example I passed it "3" and "5" to extract the 3rd and 5th words. Try passing different numbers, to see what you get.
Nov 2 '06 #5
DONE1
9
You use that to say which word you want to pull out of the string. If I remember correctly, in the example I passed it "3" and "5" to extract the 3rd and 5th words. Try passing different numbers, to see what you get.
Thanks for your help
Nov 3 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeremy | last post by:
I have a pretty straightforward database that is designed to record free-form information about products (date, source and a memo field). This is searched and updated via a form. For new records,...
3
by: winshent | last post by:
I have copied approx 20 controls from a form to a tab control for reasons of space. i now cannot run code triggered by the events of these controls. for example, the following will not trigger...
4
by: John Doe | last post by:
I am interested in creating a database of my mp3 files. What I am looking to do is paste a series of lines of text into a series of table cells in access. Every time I try to do it, it pastes ALL...
0
by: postings | last post by:
Hi I'm a little confused. I've deployed my ASP.NET project by using the "copy project" feature of VS2003. Works fine. I'm stuck however in copying individual files to the deployed solution. ...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
10
by: Michael | last post by:
Hi Everyone. I have been designing a form with about 100 or so controls and today I pasted some code from another test project into this one and then all the controls on the form disapeared from...
4
by: gordon | last post by:
Hi I hav e a smallish app that has a datagridview. A user can select some columns in the datagrid, and i have a button that i would like to use to copy the rows that are selected to the...
1
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. ...
48
by: mantrid | last post by:
Hello Is there a way to prevent users copying a full size image from a web page. Displaying the image with a smaller width and height only affects the image as viewed, the actual full size image...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.