Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the column index of a range

Tags:

excel

vba

In the following subcode, I want to restrict what it does (replacing substring in hyperlinks) to a specific column. I have writen in '* what my idea is for a quick fix. But I can't seem to find a good way to get the column value of a cell saved as a Range variable.

Dim MyDoc As Worksheet
Dim MyCell As Range
    ...
        For Each MyCell In MyDoc.UsedRange
            If MyCell.Hyperlinks.Count > 0 Then
               '* if mycell's columnnumber = 1 then
                    LinkURL = MyCell(1).Hyperlinks(1).Address
                    FindPos = InStr(1, LinkURL, FindString)
                    If FindPos > 0 Then 'If FindString is found
                        ReplaceLen = Len(FindString)
                        URLLen = Len(LinkURL)
                        PreStr = Mid(LinkURL, 1, FindPos - 1)
                        PostStr = Mid(LinkURL, FindPos + ReplaceLen, URLLen)
                        NewURL = PreStr & ReplaceString & PostStr
                        MyCell(1).Hyperlinks(1).Address = NewURL 'Change the URL
                    End If
               '* End if
            End If
         Next MyCell
like image 460
Øyvind Avatar asked Aug 09 '12 10:08

Øyvind


People also ask

How do you find the index of a column in a DataFrame?

You can get the column index from the column name in Pandas using DataFrame. columns. get_loc() method.

How do I find a column reference in Excel?

The COLUMN function returns the column number of the given cell reference. For example, the formula =COLUMN(D10) returns 4, because column D is the fourth column.


1 Answers

You can simply call the Column property:

If MyCell.Column = 1 Then ...

This is the absolute column (column A of the spreadsheet), not the first column of the range.

If you want to check if it is the first column of the range, you can first calculate it:

firstCol = yourRange.Cells(1, 1).Column
If MyCell.Column = firstCol Then ...
like image 95
assylias Avatar answered Nov 13 '22 04:11

assylias