user222427
user222427

Reputation:

Match Column Int to column letters? or Write to column using Letter Header

So usually when you write to an excel worksheet it's something like this oSheet.Cells[1, 2] you supply the int row and then supply the column int.

Well I have a config file thats about 70 columns that range from H to CE to DF ect.

How do you write to a column like so? oSheet.Cells[1, AF]?

I hope this makes sense

Upvotes: 0

Views: 79

Answers (2)

user2864740
user2864740

Reputation: 61975

Worksheet.Cells returns a Range object.

The Item/indexer of the Range object already accepts Excel-style values for the column index. The difference is merely between using an integer for the column value, or a string for the value.

[Column index is a] number or string that indicates the column number of the cell you want to access, starting with either 1 [integer] or "A" [string] for the first column in the range.

So, oSheet.Cells[1, "AF"] would return the desired result (Range).

Upvotes: 0

duncan
duncan

Reputation: 456

You can reference excel Ranges using A1 notation. Try something like

oSheet.Range("AF1")

Then to do this for different letters, you can concatenate

Col = "A"
oSheet.Range( Col & "1" )

Edit: sorry, realized those should be parens (had square brackets).

Upvotes: 1

Related Questions