Tuesday, May 10, 2016

Excel VBA: Convert R1C1 to A1

It's very convenient to use numbers for rows and columns when dealing with the Excel cells in VB. But there doesn't appear to be a way to convert numerical row/column (R1C1) to alphanumeric cell address (A1). Here's one way to do so:

  1. Private Function ConvR1C1ToAddr(pRow, pCol) As String  
  2.     ConvR1C1ToAddr = Range(Cells(pRow, pCol), Cells(pRow, pCol)).Address  
  3. End Function  

Since the column names can go from "A" to some like "CXZ", it's hard to build that without a lot of code.

For going the other way, simply use Range("A1").Row and Range("A1").Column.

No comments:

Post a Comment