Using VBA how can I make it take text and put it somewhere

San Antonio Heat

Active Member
Joined
Oct 10, 2003
Messages
333
Office Version
  1. 2019
Platform
  1. Windows
I have about 6, 000 teams in this xcel sheet. I would liek to try a small bit of code to take the list and run them from left to right. If ya notice Team 1 and team 2 uses 2 colums in A and B for their 20 team roster. Can I use some kind of code to make them list them from left to right, say team 1 will use line 6 only and team 2 will use only 17 only, so in 6 and 17 it will run 20 colums to the left and it will take the info from A nad B and put them in the proper spaces. Is this too hard?
Book1
ABCD
22003Draft
3
4League1
5League1Team1
6Pick1:LaDainianTomlinsonPick2:PlaxicoBurress
7Pick3:AmosZereouePick4:ChadJohnson
8Pick5:QuincyMorganPick6:ChrisChambers
9Pick7:MoeWilliamsPick8:AntonioBryant
10Pick9:KerryCollinsPick10:BubbaFranks
11Pick11:MarcBulgerPick12:JonKitna
12Pick13:LaBrandonToefieldPick14:Pittsburgh
13Pick15:BobbyShawPick16:JoshReed
14Pick17:JerramyStevensPick18:JustinMcCareins
15Pick19:WillisMcGaheePick20:AaronElling
16League1Team2
17Pick1:ClintonPortisPick2:EddieGeorge
18Pick3:AmaniToomerPick4:OlandisGary
19Pick5:DerrickMasonPick6:MattHasselbeck
20Pick7:AshleyLeliePick8:WesleyWalls
21Pick9:CarolinaPick10:CoreyBradford
22Pick11:LarryJohnsonPick12:DennisNorthcutt
23Pick13:BillSchroederPick14:DougJolley
24Pick15:JoeyHarringtonPick16:JohnCarney
25Pick17:ShaunMcDonaldPick18:MichaelBennett
26Pick19:QuentinGriffinPick20:FreddieMitchell
Sheet1
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Re: Using VBA how can I make it take text and put it somewhe

The new table would look like so
Book1
ABCDEF
1
22003Draft
3
4League1
5League1Team1
6Pick1:LaDainianTomlinsonPick2:PlaxicoBurressPick3:AmosZereouePick4:ChadJohnsonPick5:QuincyMorganPick6:ChrisChambers
7
8
9
10
11
12
13
14
15
16League1Team2
17Pick1:ClintonPortisPick2:EddieGeorgePick3:AmaniToomerPick4:OlandisGaryPick5:DerrickMasonPick6:MattHasselbeck
Sheet2
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

Hi, try this.
Place this code in a new module.
Code:
Sub NameMover()
  Dim c As Range
  Dim i As Integer
  Selection.Resize(10, 2).Select
  Do Until IsEmpty(ActiveCell)
  i = 0
  For Each c In Selection
    ActiveCell.Offset(0, i) = c.Value
    If i >= 2 Then c.Clear
    i = i + 1
  Next c
  Selection.Offset(11, 0).Select
  Loop
End Sub
Select the first Pick1 in your list, then run the macro

Note: Assumes that the layout is exactly as posted.

Denis
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

Thanks man i will give it a try and see if I can modify it to do all 6,000. I needed a starer to get it rolling, Thanks again
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

worked awesome but with 1 problem, it placed the 19th and 20th player instead of in Column U and V, it put them many lines down in A and B again, the rest are perfect. Maybe its because I have 6,000? It did all of them, there are actually 50 leagues with 12 teams so its not 6,000 just the original list is 6,000 lines of space since they are all in Colums A and B
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

Hi, just ran it again with a dummy recordset and it worked fine. Did you start on Pick 1 or on League 1 Team 1? That could have been the difference.

Denis
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

the code worked awesome what happened was as i said before there are 6,000 rows of data, I had 1 line missing around 3, 000 row and it was throwing it off. Picks 1 and 2 were missing so it would jump 5 into the 1 spot. Awesome qwork though, After I sorted them all with that code, I deleted all the blank spaces using teh edit go to special blank spaces, delete.

How can I learn to do a simple code like that? Better yet how can I understand what you are telling it to do? Would love to learn.

Here is the link to what youode did, great stuff
http://www.angelfire.com/empire/wwffl/Test/mkb4html.htm
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

Hi, here’s a quick description of what the code does. The secret to making it compact is to find areas where the structure repeats, and turn those into loops. So here goes:

Original code --
Code:
Sub NameMover() 
  Dim c As Range 
  Dim i As Integer 
  Selection.Resize(10, 2).Select 
  Do Until IsEmpty(ActiveCell) 
  i = 0 
  For Each c In Selection 
    ActiveCell.Offset(0, i) = c.Value 
    If i >= 2 Then c.Clear 
    i = i + 1 
  Next c 
  Selection.Offset(11, 0).Select 
  Loop 
End Sub
Housekeeping --
Sub and End Sub are the placeholders for the macro. They tell Excel that this is one macro routine.
The Dim statements are where I tell the macro what variables I want to use, and what kind of information they represent. So, c is a Range because I will use it to represent cells in a selection, and i is an integer because I will use it for counting in the loops.

Selecting the picks --
Code:
Selection.Resize(10,2).Select
You selected one cell. This expands the selection to 10 rows by 2 columns (when you refer to a range in parentheses like that, the row is always first and the column is after the comma)

The main loop --
Code:
Do Until IsEmpty(ActiveCell)
…
Loop
Tells the macro to keep repeating everything between these lines until the ActiveCell (top left cell in the selection) is empty – you’re at the end of the data. This is the loop that jumps to each team in turn.
Inside this, we’re asking Excel to do the following:

Code:
i=0
Set the variable i back to 0 (it goes to 20 for each team and needs to be reset, so the logic for the following steps is always the same)

The second loop --

Code:
For each c in Selection
…
Next c
This will do the processing of each team, putting the Picks out to the right and deleting 3 to 20 once they have been moved.

Code:
ActiveCell.Offset(0,i)=c.Value
The ActiveCell is Pick 1 in each team, because that’s where you started in the first team and the jump (Selection.Offset(11,0).Select) lands you on the next Pick 1 until all teams have been processed.
The Offset bit is relative to this: 0 rows and i columns (i changes from 0 to 19 as you cycle through each cell in the selection). So this code tells Excel to go through each cell in the range and place it on the same row as Pick 1. The changing column value makes the values run out for a total of 20 columns.

Code:
If i>=2 Then c.Clear
We needed to clear any values that had been physically relocated – 3 to 20. That’s what this line does.

Code:
i=i+1
This line increments the counter by 1, so the Pick values end up in the correct columns and the correct values get deleted.

I hope that clears up what this code did. As for learning VBA code, the best place to start is to record a macro and then check out the code. It will give you a starting point but the recorded code is pretty inefficient, and you can’t record loops. You need a book that can take you through it – and this site has one of the best that I have seen. VBA and Macros with Excel.

Denis
 
Upvote 0
Re: Using VBA how can I make it take text and put it somewhe

thanks, I am learning some things now and your tutorial is great! Thanks for taking the time.
 
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,137
Members
449,207
Latest member
VictorSiwiide

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