2016-07-11 67 views
0

(第一次海報,所以請讓我知道,如果我做了什麼不對的問題,格式化/位置/內容。謝謝)錯誤「範圍類的選擇方法失敗」在VB.Net Excel工作簿中選擇範圍時

簡介:我正在開發由我公司其他人員在Visual Studio 2010 for Excel 2007/2010中開發的Excel工作簿。我現在回來用Visual Studio 2012添加要在Excel 2016中使用的功能。

問題:在程序中的某個點,代碼應該打開一個新的Excel工作簿,並在其中插入一些新數據它。這一切工作正常,但我每當我嘗試在新的工作簿中選擇一個特定的單元格(例如wksht.Range(「A1」)。選擇),我得到錯誤「選擇範圍類失敗的方法」。在嘗試在新工作簿中選擇範圍之前,我正在激活工作表。我知道盡可能避免選擇範圍,但這是來自以前開發人員的舊代碼,我試圖保持原樣。有趣的是,當我在Excel 2010中打開工作簿時,它完美地工作。只有當我嘗試使用完全相同的Excel 2016代碼完成同樣的事情時,纔會出現問題。我們現在只更新到2016年,所以這就是爲什麼我們以前從未遇到過這個錯誤。使用VB.Net在Excel 2016中選擇範圍的方法與以前版本相比有什麼不同?

錯誤消息:

************** Exception Text ************** 
System.Runtime.InteropServices.COMException (0x800A03EC): Select method of Range class failed 
    [location of line where error occurred, edited to preserve employer's property] 

************** Loaded Assemblies ************** 
mscorlib 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1076.0 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll 
---------------------------------------- 
Microsoft.VisualStudio.Tools.Office.Runtime 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Office.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Office.Runtime.dll 
---------------------------------------- 
System.Core 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1055.0 built by: NETFXREL2 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll 
---------------------------------------- 
System 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1075.0 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll 
---------------------------------------- 
System.Configuration 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1055.0 built by: NETFXREL2 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll 
---------------------------------------- 
System.Xml 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1067.0 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll 
---------------------------------------- 
System.Security 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1080.0 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Security/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Security.dll 
---------------------------------------- 
Microsoft.VisualStudio.Tools.Applications.Hosting 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Hosting/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Hosting.dll 
---------------------------------------- 
Microsoft.VisualStudio.Tools.Applications.Runtime 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.dll 
---------------------------------------- 
System.Deployment 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1055.0 built by: NETFXREL2 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Deployment/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll 
---------------------------------------- 
Microsoft.VisualStudio.Tools.Applications.ServerDocument 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.ServerDocument/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 
---------------------------------------- 
System.Windows.Forms 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1055.0 built by: NETFXREL2 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll 
---------------------------------------- 
System.Drawing 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1068.2 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll 
---------------------------------------- 
System.Xml.Linq 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1055.0 built by: NETFXREL2 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll 
---------------------------------------- 
Microsoft.Office.Tools 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.dll 
---------------------------------------- 
Microsoft.Office.Tools.Excel.Implementation 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Excel.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Excel.Implementation.dll 
---------------------------------------- 
Microsoft.Office.Tools.Common.Implementation 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.Implementation.dll 
---------------------------------------- 
WindowsBase 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1080.0 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll 
---------------------------------------- 
PresentationFramework 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1080.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll 
---------------------------------------- 
PresentationCore 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.6.1080.0 built by: NETFXREL3STAGE 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll 
---------------------------------------- 
Microsoft.Office.Tools.Excel 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Excel/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Excel.dll 
---------------------------------------- 
Microsoft.Office.Tools.Common 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.60301.0 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.dll 
---------------------------------------- 
Microsoft.Office.Tools.Excel.v4.0.Utilities 
    Assembly Version: 10.0.0.0 
    Win32 Version: 10.0.30319.1 
    CodeBase: file:///C:/Users/[yo my name]/Documents/Test%20Bed/Microsoft.Office.Tools.Excel.v4.0.Utilities.DLL 
---------------------------------------- 
Microsoft.VisualBasic 
    Assembly Version: 10.0.0.0 
    Win32 Version: 14.6.1055.0 built by: NETFXREL2 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll 

代碼瓦特/問題(wksht送入作爲Excel.Worksheet此功能):

   'AUTO SIZE ROWS 
       wksht.Activate() 
       For iRow = valRowDataStart To (valRowDataStart + iValidCount) 
        wksht.Range("A" & iRow).EntireRow.Select() 

        wksht.Range(valColLocation & iRow).WrapText = True 
        wksht.Range(valColLoadsControlled & iRow).WrapText = True 

        wksht.Range("A" & iRow).EntireRow.AutoFit() 
        wksht.Range("A" & iRow).RowHeight = wksht.Range("A" & iRow).RowHeight + 5 
       Next iRow 

就像我剛纔所說,這是我第一次張貼問題,所以請讓我知道如果我做錯了什麼或者如果你只是需要更多的信息。非常感謝你的幫助!

回答

0

嘗試wksht.Application.Goto(wksht.Range("A1"), True)而不是

+0

這樣做!非常感謝!你有什麼想法爲什麼Range.Select將不再在2016年工作? – rsmith

+0

您可能必須在'wksht.Range(「A1」)之前'wksht.Select()'。Select()'http://stackoverflow.com/questions/10949893/vba-error-1004-select-method-of -range-class失敗了,但是我發現'Application.Goto'更可靠一點點。 – Slai