Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,560
Default beginning programming: stop when last column in sheet

Please help: I would like to change the hard coded 10 in the for loop to a
variable that represents the last column in the sheet.


Sub Macro1()
Worksheets(1).Activate

For i = 1 To 10

Worksheets(1)Columns(i).Select

Selection.TextToColumns , DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 2), Array(20, 1)),
TrailingMinusNumbers:=True

ActiveCell.Offset(columnOffset:=1).Select

Next i

End Sub



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default beginning programming: stop when last column in sheet

Sub Macro1()
Dim lastCol as Long
Dim i as Long
With Worksheets(1)
.Activate
lastCol = .Usedrange.columns( _
.UsedRange.columns.count).column
End with
For i = 1 To lastCol
Worksheets(1).Columns(i).Select
Selection.TextToColumns, DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 2), Array(20, 1)), _
TrailingMinusNumbers:=True
Next i
End Sub

--
Regards,
Tom Ogilvy

"David" wrote in message
...
Please help: I would like to change the hard coded 10 in the for loop to a
variable that represents the last column in the sheet.


Sub Macro1()
Worksheets(1).Activate

For i = 1 To 10

Worksheets(1)Columns(i).Select

Selection.TextToColumns , DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 2), Array(20, 1)),
TrailingMinusNumbers:=True

ActiveCell.Offset(columnOffset:=1).Select

Next i

End Sub





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default beginning programming: stop when last column in sheet

Hi,

How would that apply for a variable that represents the last row of a
dataset in a worksheet?

Also, rather than a hardcoded range such as a1:k22225, is there a
variable which automatically represents the range of all the columns and
rows in a dataset. Could this same variable be applied to all
worksheets whether the dataset is 2 columns and 5 rows or 100 columns
and 50,000 rows?

One more question - how can you specify the range of a column which is
not hardcoded by A,B,C, etc. but identified by a certain label in the
first row (the column heading). For example, if this first row label
were "name" - how could this range be specified?

Anne Nasser


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default beginning programming: stop when last column in sheet

How would that apply for a variable that represents the last row of a
dataset in a worksheet?


Dim lastrow as Long, rng as Range
lastRow = 2000
set rng = Range("A1:K" & lastrow)

------------
Dim rng as Range
set rng = Activesheet.Range("A1").CurrentRegion

should define your database. It is equivalent to clicking in A1, then doing
Ctrl+shift+8. This floods out in all directions until it reaches a
completely blank row/column or the edge of the worksheet. This assumes the
data starts in A1. You can anchor from any cell in the database.

-------------

Dim res as Variant, rng as Range
res = Application.Match("name",Rows(1),0)
if not iserror(res) then
set rng = Range("A1").Cells(1,res)
set rng = Range(rng, rng.End(xldown))
' or if there are spaces embedded in the data in that column
' set rng = Range(rng,cells(rows.count,rng.column).End(xlup))
End if

--
Regards,
Tom Ogilvy


"Anne Nasser" wrote in message
...
Hi,

How would that apply for a variable that represents the last row of a
dataset in a worksheet?

Also, rather than a hardcoded range such as a1:k22225, is there a
variable which automatically represents the range of all the columns and
rows in a dataset. Could this same variable be applied to all
worksheets whether the dataset is 2 columns and 5 rows or 100 columns
and 50,000 rows?

One more question - how can you specify the range of a column which is
not hardcoded by A,B,C, etc. but identified by a certain label in the
first row (the column heading). For example, if this first row label
were "name" - how could this range be specified?

Anne Nasser


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default beginning programming: stop when last column in sheet


Tom,

Thanks for those. For the first one though regarding stopping at the
last row, if you don`t know the number of the last line, for example,
2000 - what variable could be put to represent this?

Anne


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default beginning programming: stop when last column in sheet

Hi Anne,

Tom's response warrants careful consideration; it effectively represents a
mini-tutorial. Certainly, a proper assimilation would enable you to respond
to your own post and more.

Tom already shows you how to define the lastCell range. To return the
corresponding row number only requires the appendage of .Row.

Thus, for column A:

lastRow = Cells(Rows.Count, "A").End(xlUp).Row


---
Regards,
Norman



"Anne Nasser" wrote in message
...

Tom,

Thanks for those. For the first one though regarding stopping at the
last row, if you don`t know the number of the last line, for example,
2000 - what variable could be put to represent this?

Anne


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop excel from dropping the 0 in the beginning of a number? Rosewood Setting up and Configuration of Excel 12 April 4th 23 02:12 PM
Stop Excel Removing Zero at beginning of number bird007 Excel Discussion (Misc queries) 1 February 19th 07 07:44 AM
Stop Excel from dropping the 0 in the beginning of a number Mo Setting up and Configuration of Excel 2 February 4th 06 07:12 PM
need enter key to move to the next column beginning j pantozzi Excel Worksheet Functions 2 December 5th 05 04:57 PM
Add a alphabetic character to the beginning of an entire column of Karise Excel Discussion (Misc queries) 1 August 18th 05 10:24 PM


All times are GMT +1. The time now is 07:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"