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 Apr 16th, 2002, 03:38 PM   #1
Visions_Fugitive
New Member
 
Join Date: Apr 2002
Location: Milwaukee, WI
Posts: 18
Default

I have a macro that needs to loop through all the selected columns, performing an operation on each of them. I just need a way to get the numbers of the first and last columns.

firstCol = Selection.??????
lastCol = Selection.??????

For x = firstCol To lastCol

Columns(x).TextToColumns DataType:=xlDelimited, _
ConsecutiveDelimiter:=False, Space:=False

Next x


Thanks in advance.


[ This Message was edited by: Visions_Fugitive on 2002-04-16 14:39 ]

[ This Message was edited by: Visions_Fugitive on 2002-04-16 14:39 ]
Visions_Fugitive is offline   Reply With Quote
Old Apr 16th, 2002, 05:05 PM   #2
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Try the following:
Code:
Dim FirstCol, LastCol As String
FirstCol = Left(Selection.Address(columnabsolute:=False), 1)
LastCol = Right(Selection.Address(columnabsolute:=False), 1)
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 16th, 2002, 06:56 PM   #3
Bertie Bagshot
 
Join Date: Feb 2002
Posts: 36
Default

Try this :-

Dim FirstCol%, LastCol%
FirstCol = Selection(1, 1).Column
LastCol = Range([A1], Selection).Columns.Count
Bertie Bagshot is offline   Reply With Quote
Old Apr 17th, 2002, 08:17 AM   #4
Visions_Fugitive
New Member
 
Join Date: Apr 2002
Location: Milwaukee, WI
Posts: 18
Default

Thanks for your help, Al and Bertie.

For the record, Bertie's code worked. My macro now runs text to columns on all selected columns. It's very useful for data that comes from Access. Often Access will store numeric data as text. Formatting the cells as numbers and running Text to Columns fixes this.

Here is the full macro, in case anyone would like it:

Code:
Sub TTC()
    
    Dim FirstCol%, LastCol%
    FirstCol = Selection(1, 1).Column
    LastCol = Range([A1], Selection).Columns.Count
    
    For x = FirstCol To LastCol
     
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Columns(x).TextToColumns DataType:=xlDelimited, _
        ConsecutiveDelimiter:=False, Space:=False
        
    Next x
    
End Sub

[ This Message was edited by: Visions_Fugitive on 2002-04-17 07:17 ]
Visions_Fugitive is offline   Reply With Quote
Old Apr 17th, 2002, 10:18 AM   #5
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

My code works if you select the whole column, but if you don't select the whole column try the following code:
Code:
Dim FirstCol, LastCol As String
FirstCol = Left(Selection.EntireColumn.Address(columnabsolute:=False), 1)
LastCol = Right(Selection.EntireColumn.Address(columnabsolute:=False), 1)
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 17th, 2002, 10:25 AM   #6
Visions_Fugitive
New Member
 
Join Date: Apr 2002
Location: Milwaukee, WI
Posts: 18
Default

Quote:
My code works if you select the whole column, but if you don't select the whole column try the following code:
Al, I was getting a type mismatch error with your code when my loop started. I tried it without dimensioning the variables and the same thing happened. Perhaps if I dimmed the variables as integers it would have worked.
Visions_Fugitive is offline   Reply With Quote
Old Apr 18th, 2002, 07:57 AM   #7
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

I would have to see your code, but if you just select, for example, A1:H5 then the following code will return:
FirstCol=A
LastCol=H
Code:
Dim FirstCol, LastCol As String
FirstCol = Left(Selection.EntireColumn.Address(columnabsolute:=False), 1)
LastCol = Right(Selection.EntireColumn.Address(columnabsolute:=False), 1)
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 18th, 2002, 08:25 AM   #8
Abdc
 
Join Date: Mar 2002
Posts: 20
Default

Quote:
On 2002-04-17 09:25, Visions_Fugitive wrote:
Quote:
My code works if you select the whole column, but if you don't select the whole column try the following code:
Al, I was getting a type mismatch error with your code when my loop started. I tried it without dimensioning the variables and the same thing happened. Perhaps if I dimmed the variables as integers it would have worked.

For Al Chara's code, you need to Dim the LastCol and First Col As String (as opposed to Bertie Bagshot's code which is "dimmed" as Integer).

You should have got a type mismatch error on Al Chara's code for LastCol only, not for Firstcol, since in the code he posted LastCol was dimmed as string while FirstCol was not assigned a variable type so has the default type Variant(which covers all variable types).
Abdc is offline   Reply With Quote
Old Apr 18th, 2002, 08:33 AM   #9
nisht
Board Regular
 
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
Default

sub firstlastcolumn ()
dim intfirstcolumn as integer
dim intlastcolumn as integer
dim i as integer
' you need to make selection now..

' or you can select current region

' currentregion.select

intfirstcolumn = Activecell.column
intlastcolumn = selection.column.count + intfirstcolumn

' you can step for to get operation by leaving one column or two column.
for i = intfirstcolumn to intlastcolumn

cells(1,i) = "Put your operation."

next i

end sub


nishith desai
http://www.pexcel.com
nisht is offline   Reply With Quote
Old Apr 18th, 2002, 08:34 AM   #10
nisht
Board Regular
 
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
Default

sub firstlastcolumn ()
dim intfirstcolumn as integer
dim intlastcolumn as integer
dim i as integer
' you need to make selection now..

' or you can select current region

' currentregion.select

intfirstcolumn = Activecell.column
intlastcolumn = selection.column.count + intfirstcolumn

' you can step for to get operation by leaving one column or two column.
for i = intfirstcolumn to intlastcolumn

cells(1,i) = "Put your operation."

next i

end sub


nishith desai
http://www.pexcel.com
nisht 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 07:40 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