Friday, July 31, 2009

How to recieve the column number from the column name in microsoft excel by using macros.?

In an excel sheet i have to recieve the column number ranging from 1 to 256 while the input will be A or B or C upto IV. Please provide me a solution by providing me some macro syntax.

How to recieve the column number from the column name in microsoft excel by using macros.?
Why using a macro when you can use a function?


Put this in B2, to read column name in A2


=column( indirect( A2%26amp;"1"))


and whenever you put a column name in A2 (Like HA), B2 will give you the number of that column











If you insist about macro then use this..





Function ColumnNumber(ColumnName)


Range("A2").Value = ColumnName


Range("B2").FormulaR1C1 = "=column(indirect(rc[-1]%26amp;1))"


ColumnNumber = Range("B2").Value


End Function


Which is using the same functions (COLUMN and INDIRECT) but in VBA.





Read my profile, I am XLMan
Reply:I am not sure what you need:





1. If you just need to know what is the serial number of each column, you can go to Options %26gt; General %26gt; R1C1





2. If you want a loop that will span all columns, the syntax is this:


' //


' // Start Code


' //


Sub abdullah()


For Column_Counter = 1 to 256


ActiveSheet.Cells(1, Column_Counter) = "Lalala"


Next Column_Counter


End Sub


' //


' // End Code


' //





3. If you want to convert a letter to a column (D %26gt;%26gt; 4), it can be done - but please elaborate your reason for needing it, because I am pretty sure you are asking for it just due to a lack of VBA know-how, and I am pretty certain it is both unncecessary and unhealthy code writing.


No comments:

Post a Comment