Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

VBA Excel: how to get row and column number of named range?

I have the following problem and it's driving me NUTS! I work on a large database program in Excel, and I'm trying to retrieve the row and column numbers of specific named ranges. The following code used to work (temp_entryvalue is the name of the range):

temp_datafile_row = Workbooks(datafile_filepath).Worksheets(temp_datafile_worksheet).Range(temp_entryvalue).Row
temp_datafile_col = Workbooks(datafile_filepath).Worksheets(temp_datafile_worksheet).Range(temp_entryvalue).Column

I get the ole' Error 1004. Troubleshooting a bit, the problem here seems to be that the range associated with temp_entryvalue is hidden in the excel sheet, however the name is defined and there is a row and column number assigned to.

This however, all used to work. Until I changed one thing: instead of writing directly to the database excel worksheet, I first put everything in an array, so that only at the very end I open the database worksheet and copy all the data. Before I did this, everything used to work fine.

So how do I fix this? How do I retrieve the row and column number of a Named Range that is hidden, but DOES have a row and column number associated to it?

Thanks.

question from:https://stackoverflow.com/questions/65888635/row-and-column-of-a-named-excel-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's a strange question you are having there. However, in general getting the column and row of a hidden worksheet should be trivial, thus I suppose you are making some small mistake somewhere.

Open a new Excel file and try this code:

Option Explicit    
Public Sub TestMe()      

    Dim tempEntryvalue As Range

    Set tempEntryvalue = Worksheets(1).Range("A1:Z10")
    Worksheets(1).Visible = xlVeryHidden        
    Debug.Print tempEntryvalue.Row
    Debug.Print tempEntryvalue.Column  

End Sub

Or if you really mean NamedRange (your code looks like if the range is declared as a variable), then this is probably the easiest solution:

Public Sub TestMe()    

    Debug.Print [temp_entryvalue].Row
    Debug.Print [temp_entryvalue].Column
    Debug.Print Range("temp_entryvalue").Row
    Debug.Print Range("temp_entryvalue").Column    

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...