Gustav Brock
Gustav at cactus.dk
Sat Jan 21 07:26:02 CST 2006
Hi Lambert I've found that you don't need Select for anything else than if the worksheet operates in interactive mode and you wish to display a range for the user. Select exists in much code as it is what you get when you use the macro recorder for creating what you may call rough code. Thus, you can reduce your central code lines like this: xlApp.Worksheets(sSheetName).Activate rng = Range(sRegionStart).Address xlApp.ActiveWorkbook.Names.Add sRangeName, "=" & sSheetName & "!" & rng Can you specify the range including the $ signs, like: $F$5:$J$7 you don't even need Range: xlApp.Worksheets(sSheetName).Activate xlApp.ActiveWorkbook.Names.Add sRangeName, "=" & sSheetName & "!" & sRegionStart /gustav >>> Lambert.Heenan at AIG.com 20-01-2006 22:22:23 >>> Here's an example of creating named ranges.... Sub Excel_CreateNamedRange(sPath As String, sRangeName As String, _ sSheetName As String, Optional sRegionStart As String = "A1") ' Creates a named range (sRangeName) in the given spreadsheet (sPath) ' on the given worksheet (sSheetName). ' The region of data to include in the named range includes the cell address in sRegionStart Dim xlApp As Excel.Application Dim rng As String 'Check to see if the file name passed in to 'the procedure is valid If Not FileExists(sPath) Then ' MsgBox sPath & " isn't a valid path!" Exit Sub Else Set xlApp = CreateObject("Excel.Application") 'xlApp.Visible = True xlApp.Workbooks.Open sPath xlApp.Worksheets(sSheetName).Activate xlApp.ActiveSheet.Range(sRegionStart).Select rng = xlApp.Selection.CurrentRegion.Address xlApp.ActiveWorkbook.Names.Add sRangeName, "=" & sSheetName & "!" & rng End If Excel_CloseWorkBook xlApp, True Set xlApp = Nothing End Sub