Is there a way to find a range within a range in Excel?

瘦欲@ 提交于 2019-12-22 10:33:46

问题


I have an xls file that looks this way:

http://i.stack.imgur.com/d2fux.png

Well, and that's sth like the same piece of info that depends on the headers, in this case the header is the row 22164, next header is 22178, and so on.

In the GUI, i have something that let users choose from parameters, and option to load the info from the xls file depending on their choices, for example, they can choose:

WR | random | 4 | 6 | 15

looking in that case for the info stored in A22165:O22177

I have already the sheet with all the info in the workbook, let's call that object[,] 'valueArray':

Workbook workBook = _excelApp.Workbooks.Open(censFilePath);
        Worksheet sheet = (Worksheet)workBook.Sheets[1];
        Range sheetRange = sheet.UsedRange;
        object[,] valueArray = 
            (object[,])sheetRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

and I have created another object that stores the user's choices in the same order and format that the xls has. Let's call that object[,] sth like 'look4it'

I'm trying to find a method that I can tell: I want to find 'look4it' in 'valueArray', and that method's answer would be A22164:E22164 so I can after that calculate the range to copy from 'valueArray' and use the correct info.

I've tried Range.Find but it seems to be that looks for a unique piece of info within a Range, and returns the address, but I've found nothing that allows me to looks for a Range's content within another one.

Any help is appreciated!


回答1:


I think what I would do in this scenario is first construct a look-up list consisting of each header and its location. For the key in the list, I would concatenate the header cells together to make a single look-up value. So following on immediately after your code:

Dictionary<string, int> lookUpList = new Dictionary<string, int>();

// Loop through the rows

for (int i = 0; i < valueArray.GetLength(0); i++)
{
    // Identify the header rows

    if (valueArray[i, 0].ToString() == "WR")
    {
        // Concatenate the header strings into a single string

        string header = "";

        for (int j = 0; j < valueArray.GetLength(1); j++)
        {
            header += valueArray[i, j].ToString();
        }

        // Store the header location

        lookUpList.Add(header, i + 1);
    }
}

Bear in mind that this is extempore and I haven't tested it. I'm not sure exactly how the Range.get_Value method places data into the array, so the dimensions might be the wrong way around. Also I'm making assumptions about how to identify header rows in your spreadsheet based on the image of the data you supplied.

Anyway, once constructed you can perform a simple look up to extract the row number of the header:

string userSelectedHeader = "concatenation of whatever the user selected";
int rowNumber = lookUpList[userSelectedHeader];

Does that go some way to helping answer your question?



来源:https://stackoverflow.com/questions/12948392/is-there-a-way-to-find-a-range-within-a-range-in-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!