Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Saving Changes to Excel App

Hi,

I am experimenting how to update the values of Excel application as its
values being changed in a datagrid. It does not work. Here is the code.

Hopefully, someone could spot what is wrong with it...How would you get the
columns used and rows used??

Basically, I like to get data and update the data or add new data and save
changes to the execl application. How would I do that the shortest and
fastest way. Thanks.

Using VS 2002 (C# or VB.Net) and Office 2000

***Code***
Dim ctr As Integer = dgvData.ColumnCount
Dim cell1 As String, cell2 As String, cell3 As String, cell4 As
String, cell5 As String

Dim dt As Data.DataTable
If (ds.HasChanges()) Then
dt = ds.Tables(0).GetChanges()

Dim exApp As New Excel.Application()

exApp.GetOpenFilename(txtFilePath.Text.Trim(), "Excel Files
(*.xls)|*.xls", "Practice", Nothing, Nothing)
Dim wkbook As Excel.Workbook =
exApp.Workbooks.Open(txtFilePath.Text.Trim)

wkbook.Activate()

'wkbook.Worksheets()
'exApp.ActiveWorkbook.Sheets(1).Select()

Dim sheet As Worksheet = wkbook.Sheets(1)
'Dim rng As Range = exApp.Range("A1")
'Dim r As Integer = exApp.Rows.Count
'Dim r As Int16 = sheet.Rows.Count
'Dim c As Integer = exApp.Columns.Count
'Dim c As Int16 = sheet.Columns.Count

Dim r As Int16 = 24
Dim c As Int16 = 5

Dim x As Int16, y As Int16 = 1
For x = 1 To r
'Check if values change before changing the cells in the excel app
cell1 = sheet.Cells(x, y)
If cell1 < dgvData.Rows(x).Cells(y).Value Then
sheet.Cells(x, y) = dgvData.Rows(x).Cells(y).Value
End If
cell2 = sheet.Cells(x, y + 1).ToString()
If cell2 < dgvData.Rows(x).Cells(y + 1).Value Then
sheet.Cells(x, y + 1) = dgvData.Rows(x).Cells(y + 1).Value
End If
cell3 = sheet.Cells(x, y + 2).ToString()
If cell1 < dgvData.Rows(x).Cells(y + 2).Value Then
sheet.Cells(x, y + 2) = dgvData.Rows(x).Cells(y + 2).Value
End If
cell4 = sheet.Cells(x, y + 3).ToString()
If cell1 < dgvData.Rows(x).Cells(y + 3).Value Then
sheet.Cells(x, y + 3) = dgvData.Rows(x).Cells(y + 3).Value
End If
cell5 = sheet.Cells(x, y + 4).ToString()
If cell1 < dgvData.Rows(x).Cells(y + 4).Value Then
sheet.Cells(x, y + 4) = dgvData.Rows(x).Cells(y + 4).Value
End If
Next
End If
*******

Dennis

--
MCP Year 2005, Philippines
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Saving Changes to Excel App

hi,
Is there any other shorter way to do save the data back to excel
application? This works. Thanks in advanced.

*** Code ***
'Getting Cells Rows w/Data
Dim r as Int16 = sheet.UsedRange.Rows.Count

For x = 2 To r
range1 = sheet.Range("A" & x)
range2 = sheet.Range("B" & x)
range3 = sheet.Range("C" & x)
range4 = sheet.Range("D" & x)
range5 = sheet.Range("E" & x)

t = range1.Rows.Count
cell1 = range1.Value
If cell1 < dgvData.Rows(x - 2).Cells(0).Value.ToString() Then
range1.Value = dgvData.Rows(x - 2).Cells(0).Value
End If
cell2 = range2.Value
If cell2 < dgvData.Rows(x - 2).Cells(1).Value.ToString() Then
range2.Value = dgvData.Rows(x - 2).Cells(1).Value
End If
cell3 = range3.Value
If cell3 < dgvData.Rows(x - 2).Cells(2).Value.ToString() Then
range3.Value = dgvData.Rows(x - 2).Cells(2).Value
End If
cell4 = range4.Value
If cell4 < dgvData.Rows(x - 2).Cells(3).Value.ToString() Then
range4.Value = dgvData.Rows(x - 2).Cells(3).Value
End If
cell5 = range5.Value
If cell5 < dgvData.Rows(x - 2).Cells(4).Value.ToString() Then
range5.Value = dgvData.Rows(x - 2).Cells(4).Value
End If
Next

exApp.ActiveWorkbook.Save()
exApp.ActiveWorkbook.Close(True)
******

den2005
--
MCP Year 2005, Philippines


"den 2005" wrote:

Hi,

I am experimenting how to update the values of Excel application as its
values being changed in a datagrid. It does not work. Here is the code.

Hopefully, someone could spot what is wrong with it...How would you get the
columns used and rows used??

Basically, I like to get data and update the data or add new data and save
changes to the execl application. How would I do that the shortest and
fastest way. Thanks.

Using VS 2002 (C# or VB.Net) and Office 2000

***Code***
Dim ctr As Integer = dgvData.ColumnCount
Dim cell1 As String, cell2 As String, cell3 As String, cell4 As
String, cell5 As String

Dim dt As Data.DataTable
If (ds.HasChanges()) Then
dt = ds.Tables(0).GetChanges()

Dim exApp As New Excel.Application()

exApp.GetOpenFilename(txtFilePath.Text.Trim(), "Excel Files
(*.xls)|*.xls", "Practice", Nothing, Nothing)
Dim wkbook As Excel.Workbook =
exApp.Workbooks.Open(txtFilePath.Text.Trim)

wkbook.Activate()

'wkbook.Worksheets()
'exApp.ActiveWorkbook.Sheets(1).Select()

Dim sheet As Worksheet = wkbook.Sheets(1)
'Dim rng As Range = exApp.Range("A1")
'Dim r As Integer = exApp.Rows.Count
'Dim r As Int16 = sheet.Rows.Count
'Dim c As Integer = exApp.Columns.Count
'Dim c As Int16 = sheet.Columns.Count

Dim r As Int16 = 24
Dim c As Int16 = 5

Dim x As Int16, y As Int16 = 1
For x = 1 To r
'Check if values change before changing the cells in the excel app
cell1 = sheet.Cells(x, y)
If cell1 < dgvData.Rows(x).Cells(y).Value Then
sheet.Cells(x, y) = dgvData.Rows(x).Cells(y).Value
End If
cell2 = sheet.Cells(x, y + 1).ToString()
If cell2 < dgvData.Rows(x).Cells(y + 1).Value Then
sheet.Cells(x, y + 1) = dgvData.Rows(x).Cells(y + 1).Value
End If
cell3 = sheet.Cells(x, y + 2).ToString()
If cell1 < dgvData.Rows(x).Cells(y + 2).Value Then
sheet.Cells(x, y + 2) = dgvData.Rows(x).Cells(y + 2).Value
End If
cell4 = sheet.Cells(x, y + 3).ToString()
If cell1 < dgvData.Rows(x).Cells(y + 3).Value Then
sheet.Cells(x, y + 3) = dgvData.Rows(x).Cells(y + 3).Value
End If
cell5 = sheet.Cells(x, y + 4).ToString()
If cell1 < dgvData.Rows(x).Cells(y + 4).Value Then
sheet.Cells(x, y + 4) = dgvData.Rows(x).Cells(y + 4).Value
End If
Next
End If
*******

Dennis

--
MCP Year 2005, Philippines

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automaticly saving formula's to values when saving Gunti Excel Discussion (Misc queries) 8 November 11th 08 09:34 AM
Saving Excel 2007 conditional formatting in Excel 2003 format [email protected] Excel Discussion (Misc queries) 0 March 28th 08 06:42 AM
Saving a spreadsheet without saving the Macro tedd13 Excel Programming 7 March 31st 06 02:17 PM
editing an excel 97 file within excel 2003 and saving back as exce Chris Egebrecht Excel Discussion (Misc queries) 3 March 24th 06 11:14 PM
Saving a Workbook: Forcing User to Rename before Saving Rollin_Again[_6_] Excel Programming 5 April 16th 04 02:54 PM


All times are GMT +1. The time now is 02:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"