I'm trying to replicate a Worksheet several time in a Workbook and I'm doing using the following code:
```
IXLWorksheet worksheet = modelSheet.CopyTo("SheetName");
```
When I open the generated document the NamedRanges were copied, using the same name as expected, but all of them are associated to the model worksheet (source). I was expecting the NamedRange address to be changed to the target Worksheet too.
Example:
```
"=ModelSheet!$B$10:$B$10" would transform to "=TargetSheet!$B$10:$B$10"
```
Is there a way to do it or isn't it supported yet?
Many Thanks & great work my man, incredible Lib!
Cheers!
Comments: I found a few issues with Rynkadink's solution: 1. foreach loop is looking for named ranges in targetWorksheet, which doesn't have any named ranges because they were not copied by CopyTo method. That was the original issue anyway. Body of this loop will never execute. 2. Ignoring first issue with this solution, the code is modifying original named range instead of creating a copy. That means these named ranges will move to target sheet (instead of copy). 3. Finally, we have to take care of scope as well. Two named ranges cannot have same name if their scope is 'workbook'. We can solve this by giving them 'worksheet' scope. Here is modified solution: public static IXLWorksheet CopyToWithNamedRanges(this IXLWorksheet sourceSheet, String targetSheetName) { if (sourceSheet == null) { throw new ArgumentNullException("Source Worksheet is Null!"); } IXLWorksheet targetWorksheet = sourceSheet.CopyTo(sourceSheet.Workbook, targetSheetName); foreach (IXLNamedRange namedRange in sourceSheet.Workbook.NamedRanges) { string[] splitAddress = namedRange.RefersTo.Split(new char[] { '!' }, 2); string worksheetName = splitAddress[0]; string newRange = targetWorksheet.Name + "!" + splitAddress[1]; targetWorksheet.NamedRanges.Add(namedRange.Name, newRange); } return targetWorksheet; } 1. Now we are reading named ranges from workbook which contains our source sheet. 2. We are not modifying original named range. 3. We are adding named ranges to target sheet, which automatically sets the scope to worksheet. 4. Original credit goes to Rynkadink.
```
IXLWorksheet worksheet = modelSheet.CopyTo("SheetName");
```
When I open the generated document the NamedRanges were copied, using the same name as expected, but all of them are associated to the model worksheet (source). I was expecting the NamedRange address to be changed to the target Worksheet too.
Example:
```
"=ModelSheet!$B$10:$B$10" would transform to "=TargetSheet!$B$10:$B$10"
```
Is there a way to do it or isn't it supported yet?
Many Thanks & great work my man, incredible Lib!
Cheers!
Comments: I found a few issues with Rynkadink's solution: 1. foreach loop is looking for named ranges in targetWorksheet, which doesn't have any named ranges because they were not copied by CopyTo method. That was the original issue anyway. Body of this loop will never execute. 2. Ignoring first issue with this solution, the code is modifying original named range instead of creating a copy. That means these named ranges will move to target sheet (instead of copy). 3. Finally, we have to take care of scope as well. Two named ranges cannot have same name if their scope is 'workbook'. We can solve this by giving them 'worksheet' scope. Here is modified solution: public static IXLWorksheet CopyToWithNamedRanges(this IXLWorksheet sourceSheet, String targetSheetName) { if (sourceSheet == null) { throw new ArgumentNullException("Source Worksheet is Null!"); } IXLWorksheet targetWorksheet = sourceSheet.CopyTo(sourceSheet.Workbook, targetSheetName); foreach (IXLNamedRange namedRange in sourceSheet.Workbook.NamedRanges) { string[] splitAddress = namedRange.RefersTo.Split(new char[] { '!' }, 2); string worksheetName = splitAddress[0]; string newRange = targetWorksheet.Name + "!" + splitAddress[1]; targetWorksheet.NamedRanges.Add(namedRange.Name, newRange); } return targetWorksheet; } 1. Now we are reading named ranges from workbook which contains our source sheet. 2. We are not modifying original named range. 3. We are adding named ranges to target sheet, which automatically sets the scope to worksheet. 4. Original credit goes to Rynkadink.