PC Review


Reply
Thread Tools Rate Thread

Converting Cell Values Through Automation

 
 
=?Utf-8?B?T3JsYW56bw==?=
Guest
Posts: n/a
 
      20th Nov 2007
Hello,

I've run into a very peculiar issue when converting cell values to resolve
the "Number Stored as Text" issue. The first code segment is called by a
button on a worksheet from a code module within the workbook. The second
segment is executed within a SQL Server Integration Services (SSIS) Script
Task. The code executed within Excel works as expected. The SSIS executes,
but doesn't resolve the issue. The error persists.

Is there some difference between the code segments being overlooked? I'm
interested in your thoughts and opinions on this.

(FYI - I am aware server side automation is not recommended for several
reasons. Nonetheless, the company has chosen to utilize it. I can only
advise them of the consequences....)

====================================================
Code within Excel Module
====================================================
Public Sub test()
Dim rng As Range
Dim cell As Range

'' Update titles for each report within the workbook to reflect
'' the reporting date.
Worksheets("Total Rpt").Range("A1").Value _
= "Total Position & Risk Change for " & Now()

Worksheets("Major Change Rpt").Range("A1").Value _
= "Daily Major Position Change for " & Now()

Worksheets("Total Detail").Range("A1").Value _
= "Total Position & Risk Change - Detail for " & Now()

Worksheets("Major Change Detail").Range("A1").Value _
= "Daily Major Position Change - Detail for " & Now()

'' Some columns may not be formatted correctly after the import resulting
'' in a number stored as text error. Scan relevant columns within the
sheet
'' and apply the appropriate conversion to cleanse the sheet of this
error.
With Worksheets("Total Rpt").Range("TotalRpt")
Set rng = Range(.Cells(2, 3), .Cells(2, 8).End(xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell
End With

With Worksheets("Total Detail").Range("TotalDetail")
Worksheets("Total Detail").Activate
Set rng = Range(.Cells(2, 5), .Cells(2, 13).End(xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell
End With

With Worksheets("Major Change Rpt").Range("MajorChangeRpt")
Worksheets("Major Change Rpt").Activate
Set rng = Range(.Cells(2, 5), .Cells(2, 7).End(xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell
End With

With Worksheets("Major Change Detail").Range("MajorChangeDetail")
Worksheets("Major Change Detail").Activate
Set rng = Range(.Cells(2, 7), .Cells(2, 9).End(xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell

Set rng = Range(.Cells(2, 3), .Cells(2, 3).End(xlDown))
For Each cell In rng.Cells
cell.Value = CStr(cell.Value)
Next cell

Set rng = Range(.Cells(2, 11), .Cells(2, 11).End(xlDown))
For Each cell In rng.Cells
cell.Value = CStr(cell.Value)
Next cell
End With

End Sub

====================================================

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==

==================================================
Code within the SSIS Script Task
====================================================
Public Sub Main()
Dim ExcelSheet As Object
Dim workbook As Object
Dim sheet As Object
Dim rng As Object
Dim cell As Object
Dim x As Byte()

Dim templateDestinationPath As String
Dim templateFileName As String
Dim destinationPath As String
Dim dataDate As Date

templateDestinationPath =
CStr(Dts.Variables("templateDestinationPath").Value)
templateFileName = CStr(Dts.Variables("templateFileName").Value)
destinationPath = CStr(Dts.Variables("destinationPath").Value)
dataDate = CDate(Dts.Variables("dataDate").Value)

Try
ExcelSheet = CreateObject("Excel.Application")
'workbook = ExcelSheet.Workbooks.Open(templateDestinationPath &
templateFileName)
workbook = ExcelSheet.Workbooks.Open(templateDestinationPath)

With ExcelSheet
'' Delete the formatting rows from the named ranges
.Worksheets("Major Change
Detail").range("MajorChangeDetail").Cells(.Range("MajorChangeDetail").Rows.count, 1).entirerow.delete()
.Worksheets("Major Change
Rpt").range("MajorChangeRpt").Cells(.Range("MajorChangeRpt").Rows.count,
1).entirerow.delete()
.Worksheets("Total
Detail").range("TotalDetail").Cells(.Range("TotalDetail").Rows.count,
1).entirerow.delete()
.Worksheets("Total
Rpt").range("TotalRpt").Cells(.Range("TotalRpt").Rows.count,
1).entirerow.delete()

'' Apply formatting to each section of the sheet.
'' Update titles for each report within the workbook to
reflect
'' the reporting date.

.Worksheets("Total Rpt").Range("G1").Value() _
= "Reporting Date: " + dataDate

.Worksheets("Major Change Rpt").Range("G1").Value() _
= "Reporting Date: " + dataDate

.Worksheets("Total Detail").Range("G1").Value() _
= "Reporting Date: " + dataDate

.Worksheets("Major Change Detail").Range("G1").Value() _
= "Reporting Date: " + dataDate


'' Some columns may not be formatted correctly after the
import resulting
'' in a number stored as text error. Scan relevant columns
within the sheet
'' and apply the appropriate conversion to cleanse the sheet
of this error.
With .Worksheets("Total Rpt").Range("TotalRpt")
rng = ExcelSheet.Range(.Cells(2, 3), .Cells(2,
8).End(ExcelSheet.xlDown))
For Each cell In rng.Cells
cell.Value = CObj(cell.Value)
Next cell
End With

With .Worksheets("Total Detail").Range("TotalDetail")
rng = ExcelSheet.Range(.Cells(2, 5), .Cells(2,
13).End(ExcelSheet.xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell
End With

With .Worksheets("Major Change Rpt").Range("MajorChangeRpt")
rng = ExcelSheet.Range(.Cells(2, 5), .Cells(2,
7).End(ExcelSheet.xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell
End With

With .Worksheets("Major Change
Detail").Range("MajorChangeDetail")
rng = ExcelSheet.Range(.Cells(2, 7), .Cells(2,
9).End(ExcelSheet.xlDown))
For Each cell In rng.Cells
cell.Value = CDbl(cell.Value)
Next cell

rng = ExcelSheet.Range(.Cells(2, 3), .Cells(2,
3).End(ExcelSheet.xlDown))
For Each cell In rng.Cells
cell.Value = CStr(cell.Value)
Next cell

rng = ExcelSheet.Range(.Cells(2, 11), .Cells(2,
11).End(ExcelSheet.xlDown))
For Each cell In rng.Cells
cell.Value = CStr(cell.Value)
Next cell
End With

'' Save the changes to the workbook
.Workbooks(1).close(True)
End With
Catch ex As Exception
Dts.Log("Error removal of dummy format rows from Excel sheet: "
& ex.Message, 0, x)
Dts.TaskResult = Dts.Results.Failure
Finally
'' ExcelSheet.Workbooks(1).Close()
ExcelSheet.Application.Quit()
ExcelSheet = Nothing
End Try

'--------------------------------------
Dts.TaskResult = Dts.Results.Success
End Sub
End Class

====================================================
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting integer values to duration values (hours & minutes) on Gridview TC Microsoft C# .NET 2 12th Jul 2007 09:30 AM
Values don't behave when converting text to values in userform =?Utf-8?B?S3JhZ2VsdW5k?= Microsoft Excel Programming 2 15th Feb 2007 08:53 PM
Converting a word document (.doc) to SNP or PDF using automation in vba microb0x Microsoft Access 2 28th Jan 2007 10:15 PM
Converting an automation script to a COM object? Trigger Microsoft Powerpoint 4 18th Feb 2004 05:54 PM
Converting Option Group number values to text values =?Utf-8?B?QnJhZE4=?= Microsoft Access Getting Started 2 18th Nov 2003 12:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 AM.