MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 15th, 2002, 01:03 AM   #1
woody1
New Member
 
Join Date: May 2002
Posts: 2
Default

I need to copy the text from a cell in column A that contains a specific text string

i.e."Tech 1" to a variable number of cells in column J.

The above needs to continue until it finds a cell containing "tech 2" in column A.

For example, if A9 contains "tech 1", and A75 contains "tech 2", cells J14:J64 should

contain "tech 1".

The macro then needs to loop as above, exept finding text string ("Tech 2") copying that to

a variable number of cells in column J, and so on, until it cannot find any more instances

of ("Tech *") in column A.

The position of "tech 1", "tech 2" etc within column A is always variable, but the position

of the first line of the cell the text needs to be copied to is always constant relative to

the position of "tech 1", "tech 2" etc, (is always 9 columns accross, 5 rows down).


On finding a blank cell in column A, the macro should terminate.

Have struggled on this for several months.... is it possible ?
woody1 is offline   Reply With Quote
Old May 15th, 2002, 05:21 PM   #2
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

Try the following:

Code:
Sub test()
Dim lastrow As Long, x As Long
Dim lastcol As Integer
Dim Rng1 As Range, Rng2 As Range
Dim MyArray

With ActiveSheet
.UsedRange
lastcol = .Cells.SpecialCells(xlCellTypeLastCell).Column
lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row

Set Rng1 = .Range("A1:A" & lastrow)
.Columns(lastcol + 2).ClearContents
Rng1.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Cells(1, lastcol + 2), Unique:=True
.Cells(1, lastcol + 2).Delete shift:=xlUp

Set Rng2 = .Range(.Cells(1, lastcol + 2), .Cells(1, lastcol + 2).End(xlDown))

MyArray = Rng2.Value
.Columns(lastcol + 2).ClearContents
ReDim Preserve MyArray(1 To Rng2.Rows.Count, 1 To 3)
For x = LBound(MyArray) To UBound(MyArray)
    MyArray(x, 2) = Application.Match(MyArray(x, 1), .Range("A:A"), 0)
    MyArray(x, 3) = WorksheetFunction.CountIf(.Range("A:A"), MyArray(x, 1))
    .Cells(MyArray(x, 2), 1).Offset(5, 9).Resize(MyArray(x, 3)) = MyArray(x, 1)
Next x
End With
End Sub
Please note that I assumed two things:

1. That you have a category header row in row 1.

2. That column A is sorted (this can probably be relaxed).

Please try this out and report any problems. Please work with a copy of your data until you are comfortable with the results.

Bye,
Jay

EDIT: The code above basically copies the data to the new column. All that for nothing!

Try the following, which doesn't require the sort...

Code:
Sub test()
Dim lastrow As Long, x As Long
Dim lastcol As Integer
Dim Rng1 As Range, Rng2 As Range
Dim MyArray, RowCount As Long

With ActiveSheet
.UsedRange
lastcol = .Cells.SpecialCells(xlCellTypeLastCell).Column
lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row

Set Rng1 = .Range("A1:A" & lastrow)
.Columns(lastcol + 2).ClearContents
Rng1.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Cells(1, lastcol + 2), Unique:=True
.Cells(1, lastcol + 2).Delete shift:=xlUp

Set Rng2 = .Range(.Cells(1, lastcol + 2), .Cells(1, lastcol + 2).End(xlDown))
RowCount = Rng2.Rows.Count
MyArray = Rng2.Value
.Columns(lastcol + 2).ClearContents
ReDim Preserve MyArray(1 To Rng2.Rows.Count, 1 To 2)
For x = 1 To RowCount
    MyArray(x, 2) = Application.Match(MyArray(x, 1), .Range("A:A"), 0)
Next x

For x = 1 To RowCount - 1
    .Range(.Cells(MyArray(x, 2), 1), .Cells(MyArray(x + 1, 2) - 1, 1)).Offset(5, 9) = MyArray(x, 1)
Next x
    .Cells(MyArray(RowCount, 2), 1).Offset(5, 9) = MyArray(RowCount, 1)
End With
End Sub
[ This Message was edited by: Jay Petrulis on 2002-05-15 18:03 ]
Jay Petrulis is offline   Reply With Quote
Old May 15th, 2002, 05:36 PM   #3
PHMayfield
New Member
 
Join Date: Apr 2002
Location: Phoenix, AZ, USA
Posts: 29
Default

Try in Cell J14

=if(left(A9,4)="tech",A9,J13)

You have to drag it down as far as you data goes, and maybe modify if the 0's bug you that it will split out before the first occurrence of a "tech" cell.

[ This Message was edited by: PHMayfield on 2002-05-15 16:37 ]
PHMayfield is offline   Reply With Quote
Old May 22nd, 2002, 10:19 AM   #4
woody1
New Member
 
Join Date: May 2002
Posts: 2
Default

Sorted with Left command.
Shows how little I know about Excel...
Thanks.
woody1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 10:15 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes