Excel VBA问题 – SUM,范围,值等

我一直在MS Access中使用VBA多年,并具有一些通用的VBA能力。 我现在需要在Excel中开始使用VBA,并遇到各种各样的问题。 我很抱歉把你们全都弄错了,但是我不能把这个工作做好,我花了一天时间研究networking,仔细testing每个选项。

最终,我试图从Excel电子表格中自动导入Access。 我认为这是一个相当普遍的目标,我认为我的Excel VBA计划非常简单:

1)从电子表格中获取logging计数(用于validation成功导入)2)获取特定列中的所有值的总和(也用于validation成功导入)3)添加一个新列以包含文件名(添加头然后将一个固定值 – 文件名 – 添加到每行数据)

我不知道我的问题是我如何参考Workbook / Worksheet / Cell / Range等,或者对VBA的一些其他根本的误解。

还有一件令人沮丧的事情是,我第一次运行这个程序的时候,有些部分是可以工作的,但是随后每次运行都会失败。 我正在使用Access&Excel 2010。

我已经将所有这些过程归结为一个单一的function,有两个原因。 首先,它使testing更容易。 其次,我不知道如何创build单独的function,可以在开放/有效的电子表格上工作。 (所以如果你对此有所了解,我也欢迎)。

代码粘贴在下面,评论包括什么工作/不工作的信息。

非常感谢您对我的代码问题的任何见解。

DGP


Public Function fnProcessExcelDataImportFiles(strFileNameAndPath As String, strUseWorksheet As String, strSUMColumn As String, strAddColumnLabel As String, varAddColumnFixedValue As Variant) On Error GoTo HandleError 'Variables Dim strColumnCLabel As String Dim varColumnCLabel As Variant Dim intLastCol As Long Dim intLastRow As Long Dim intNextCol As Long Dim intRecordCount As Long Dim varSumExcelColumns As Variant Dim strSUMRange As String Dim strNewFixedValueInsteadofVARIANT As String Dim oXLApp As Excel.Application 'Declare the object variables Dim oXLBook As Excel.Workbook Dim oXLSheet As Excel.Worksheet Set oXLApp = New Excel.Application 'Create a new instance of Excel Set oXLBook = oXLApp.Workbooks.Open(strFileNameAndPath) 'Open the existing workbook Set oXLSheet = oXLBook.Worksheets(1) 'Work with the first worksheet oXLSheet.Activate 'Activate the Worksheet oXLApp.Visible = True 'Show it to the user oXLApp.UserControl = True 'For Testing: Verify that function can read from Worksheet 'THESE WORKED!! varColumnCLabel = oXLSheet.Cells(1, 13).Value strColumnCLabel = oXLSheet.Cells(1, 3).Value 'Get Last Row & Record Count – THIS WORKED intLastRow = oXLSheet.Cells.Find("*", searchorder:=xlByRows, SearchDirection:=xlPrevious).Row intRecordCount = intLastRow - 1 'Get SUM for Parameter Column strSUMRange = strSUMColumn & "2:" & strSUMColumn & intLastRow 'This works once and then returns ZERO each time I run the SUB again????? varSumExcelColumns = Excel.WorksheetFunction.Sum(oXLSheet.Range(strSUMRange)) msgbox "SUM From Excel Worksheet: " & varSumExcelColumns 'This works Once and then causes ERROR each time the SUB is subsequently run 'varSumExcelColumns = Excel.WorksheetFunction.Sum(Range("M:M")) 'This throws ERRORS when used more than once??? 'msgbox "SUM From Excel Worksheet: " & varSumExcelColumns ' This also works only Once and then throws ERRORS?????? 'varSumExcelColumns = Excel.WorksheetFunction.Sum(Columns(13)) 'msgbox "SUM From Excel Worksheet: " & varSumExcelColumns 'Get Last Column – THIS WORKS intLastCol = oXLSheet.Cells.Find("*", searchorder:=xlByColumns, SearchDirection:=xlPrevious).Column intNextCol = intLastCol + 1 'Add Label and Fixed Value Data to New Column oXLSheet.Cells(1, 13).Value = strAddColumnLabel 'This WORKS oXLSheet.Cells(1, intNextCol).Value = strAddColumnLabel 'THIS DOES NOT WORK ' This throws an ERROR 'oXLSheet.Range(Cells(2, intLastCol), Cells(intLastRow, intLastCol)).Value = varAddColumnFixedValue ' This throws an ERROR also 'oXLSheet.Range(Cells(2, 23), Cells(17, 23)).Value = varAddColumnFixedValue 'This did NOT work oXLSheet.Range("X3:X7").Value = varAddColumnFixedValue 'Check to see if the Range did get updated strNewValue = oXLSheet.Range("x5").Value msgbox "New Value = " & strNewValue 'strNewValue is EMPTY strNewFixedValueInsteadofVARIANT = CStr(varAddColumnFixedValue) 'Does it work if I set the new value to a String instead of variant?? 'NO - this does NOT work either oXLSheet.Range("X3:X7").Value = strNewFixedValueInsteadofVARIANT 'Check to see if the Range did get updated - It did NOT strNewValue = oXLSheet.Range("x5").Value msgbox "New Value = " & strNewValue 'strNewValue is EMPTY Exit_label: oXLBook.Close SaveChanges:=True 'Save (and disconnect from) the Workbook oXLApp.Quit 'Close (and disconnect from) Excel Set oXLSheet = Nothing 'Disconnect from all Excel objects (let the user take over) Set oXLBook = Nothing Set oXLApp = Nothing fnProcessExcelDataImportFiles = -777 Exit Function HandleError: msgbox "Error during fnProcessExcelDataImportFiles - " & Err.Description Resume Exit_label End Function Public Sub TestfnProcessExcelDataImportFiles() Dim strFileNameAndPath As String Dim strUseWorksheet As String Dim intSUMColumn As Integer Dim strSUMColumn As String Dim strAddColumnLabel As String Dim varAddColumnFixedValue As Variant Dim dblSUMFromFunction As Double strFileNameAndPath = "C:\Users\xxxxxxxxxxxxx\FileNamexxx_2Mar17.xlsx" strUseWorksheet = "Sheet1" intSUMColumn = 13 strSUMColumn = "M" strAddColumnLabel = "PleaseWork" varAddColumnFixedValiue = 333.33 dblSUMFromFunction = fnProcessExcelDataImportFiles(strFileNameAndPath, strUseWorksheet, strSUMColumn, strAddColumnLabel, varAddColumnFixedValue) End Sub 

既然你有一条线是第一次,而不是第二次,下一步可以在你的代码中添加额外的反馈来检查variables正在传递的行数。

 'Get SUM for Parameter Column strSUMRange = strSUMColumn & "2:" & strSUMColumn & intLastRow 

添加这一行

 msgbox "Variable Check: oXLsheet> " & oXLSheet & "strSUMRange> " & strSUMRange 

看运行之间variables是否意外改变。

 'This works once and then returns ZERO each time I run the SUB again????? varSumExcelColumns = Excel.WorksheetFunction.Sum(oXLSheet.Range(strSUMRange)) msgbox "SUM From Excel Worksheet: " & varSumExcelColumns