|
3#
楼主 |
发表于 2006-9-1 00:15:52
|
只看该作者
' *********************************************************************************************
' 函数说明:添加一张新的工作表
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' (2)workbookIdentifier:属于ExcelApp的工作薄名称;
' (2)sheetName:要插入的工作表名称;
' 返回结果:
' (1)成功:工作表对象worksheet
' (1)失败:Nothing
' 调用方法:
' InsertNewWorksheet(ExcelApp, workbookIdentifier, "new sheet")
' *********************************************************************************************
Function InsertNewWorksheet(ExcelApp, workbookIdentifier, sheetName)
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
'如果指定的工作薄不存在,将在当前激活状态的工作表中添加工作表
If workbookIdentifier = "" Then
Set workbook = ExcelApp.ActiveWorkbook
Else
On Error Resume Next
Err = 0
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Err <> 0 Then
Set InsertNewWorksheet = Nothing
Err = 0
Exit Function
End If
On Error GoTo 0
End If
sheetCount = workbook.Sheets.Count '获取工作薄中工作表的数量
workbook.Sheets.Add , sheetCount '添加工作表
Set worksheet = workbook.Sheets(sheetCount + 1) '初始化worksheet为新添加的工作表对象
'设置新添加的工作表名称
If sheetName <> "" Then
worksheet.Name = sheetName
End If
Set InsertNewWorksheet = worksheet
End Function
' *********************************************************************************************
' 函数说明:修改工作表的名称;
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' (2)workbookIdentifier:属于ExcelApp的工作薄名称;
' (3)worksheetIdentifier:属于workbookIdentifier工作薄的工作表名称;
' (4)sheetName:修改后的工作表名称;
' 返回结果:
' (1)修改成功,返回字符串:OK
' (2)修改失败,返回字符串:Bad Worksheet Identifier
' 调用方法:
' set ret = RenameWorksheet(ExcelApp, "Book1", "Sheet1", "Sheet Name")
' *********************************************************************************************
Function RenameWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier, sheetName)
Dim workbook
Dim worksheet
On Error Resume Next
Err = 0
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Err <> 0 Then
RenameWorksheet = "Bad Workbook Identifier"
Err = 0
Exit Function
End If
Set worksheet = workbook.Sheets(worksheetIdentifier)
If Err <> 0 Then
RenameWorksheet = "Bad Worksheet Identifier"
Err = 0
Exit Function
End If
worksheet.Name = sheetName
RenameWorksheet = "OK"
End Function
' *********************************************************************************************
' 函数说明:删除工作表;
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' (2)workbookIdentifier:属于ExcelApp的工作薄名称;
' (3)worksheetIdentifier:属于workbookIdentifier工作薄的工作表名称;
' 返回结果:
' (1)删除成功,返回字符串:OK
' (2)删除失败,返回字符串:Bad Worksheet Identifier
' 调用方法:
' set ret = RemoveWorksheet(ExcelApp, "Book1", "Sheet1")
' *********************************************************************************************
Function RemoveWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier)
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
On Error Resume Next
Err = 0
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Err <> 0 Then
RemoveWorksheet = "Bad Workbook Identifier"
Exit Function
End If
Set worksheet = workbook.Sheets(worksheetIdentifier)
If Err <> 0 Then
RemoveWorksheet = "Bad Worksheet Identifier"
Exit Function
End If
worksheet.Delete
RemoveWorksheet = "OK"
End Function
' *********************************************************************************************
' 函数说明:添加新的工作薄
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' 返回结果:
' (1)成功:工作表对象NewWorkbook
' (1)失败:Nothing
' 调用方法:
' set NewWorkbook = CreateNewWorkbook(ExcelApp)
' *********************************************************************************************
Function CreateNewWorkbook(ExcelApp)
Set NewWorkbook = ExcelApp.Workbooks.Add()
Set CreateNewWorkbook = NewWorkbook
End Function
' *********************************************************************************************
' 函数说明:打开工作薄
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' (2)path:要打开的工作薄路径;
' 返回结果:
' (1)成功:工作表对象NewWorkbook
' (1)失败:Nothing
' 调用方法:
' set NewWorkbook = CreateNewWorkbook(ExcelApp)
' *********************************************************************************************
Function OpenWorkbook(ExcelApp, path)
On Error Resume Next
Set NewWorkbook = ExcelApp.Workbooks.Open(path)
Set OpenWorkbook = NewWorkbook
On Error GoTo 0
End Function
' *********************************************************************************************
' 函数说明:将工作薄设置为当前工作状态
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' (2)workbookIdentifier:要设置为当前工作状态的工作薄名称;
' 返回结果:无返回值;
' 调用方法:
' ActivateWorkbook(ExcelApp, workbook1)
' *********************************************************************************************
Sub ActivateWorkbook(ExcelApp, workbookIdentifier)
On Error Resume Next
ExcelApp.Workbooks(workbookIdentifier).Activate
On Error GoTo 0
End Sub
' *********************************************************************************************
' 函数说明:关闭Excel工作薄;
' 参数说明:
' (1)ExcelApp:Excel应用程序名称;
' (2)workbookIdentifier:
' 调用方法:
' CloseWorkbook(ExcelApp, workbookIdentifier)
' *********************************************************************************************
Sub CloseWorkbook(ExcelApp, workbookIdentifier)
On Error Resume Next
ExcelApp.Workbooks(workbookIdentifier).Close
On Error GoTo 0
End Sub
' *********************************************************************************************
' 函数说明:判断两个工作表对应单元格内容是否相等
' 参数说明:
' (1)sheet1:工作表1的名称;
' (2)sheet2:工作表2的名称;
' (3)startColumn:开始比较的行序号;
' (4)numberOfColumns:要比较的行数;
' (5)startRow:开始比较的列序号;
' (6)numberOfRows:要比较的列数;
' (7)trimed:是否先除去字符串开始的空格和尾部空格后再进行比较,true或flase;
' 返回结果:
' (1)两工作表对应单元格内容相等:true
' (2)两工作表对应单元格内容不相等:flase
' 调用方法:
' ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, False)
' *********************************************************************************************
Function CompareSheets(sheet1, sheet2, startColumn, numberOfColumns, startRow, numberOfRows, trimed)
Dim returnVal 'As Boolean
returnVal = True
'判断两个工作表是否都存在,任何一个不存在停止判断,返回flase
If sheet1 Is Nothing Or sheet2 Is Nothing Then
CompareSheets = False
Exit Function
End If
'循环判断两个工作表单元格的值是否相等
For r = startRow to (startRow + (numberOfRows - 1))
For c = startColumn to (startColumn + (numberOfColumns - 1))
Value1 = sheet1.Cells(r, c)
Value2 = sheet2.Cells(r, c)
'如果trimed为true,去除单元格内容前面和尾部空格
If trimed Then
Value1 = Trim(Value1)
Value2 = Trim(Value2)
End If
'如果单元格内容不一致,函数返回flase
If Value1 <> Value2 Then
Dim cell 'As Excel.Range
'修改sheet2工作表中对应单元格值
sheet2.Cells(r, c) = "Compare conflict - Value was '" & Value2 & "', Expected value is '" & Value1 & "'."
'初始化cell为sheet2中r:c单元格对象
Set cell = sheet2.Cells(r, c) '
'将sheet2工作表中对应单元格的颜色设置为红色
cell.Font.Color = vbRed
returnVal = False
End If
Next
Next
CompareSheets = returnVal
End Function |
|