|
-
Oct 29th, 2013, 06:18 AM
#1
Thread Starter
Lively Member
-
Oct 29th, 2013, 06:19 AM
#2
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
And for any real smart arses, is it possible to use the system clock and zero everything when there is a new year, but save the data from last year?
-
Oct 29th, 2013, 09:18 AM
#3
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
This is what I would use to write values to a spreadsheet (add a reference to the Excel Object Library first):
Code:
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim writeRow As Long
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.Desktop + "\Repair_Income.xlsx")
oSheet = oBook.worksheets(1)
writeRow = oSheet.cells(oSheet.rows.count, 1).end(XlDirection.xlUp).row + 1
oSheet.range("a" & writeRow).value = txtInput.Text
oBook.close(True)
oSheet = Nothing
oBook = Nothing
oExcel = Nothing
End Sub
End Class
This will write to the next blank cell in column A, in Sheet 1, so change as required.
-
Oct 29th, 2013, 09:46 AM
#4
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
 Originally Posted by vbfbryce
This is what I would use to write values to a spreadsheet (add a reference to the Excel Object Library first):
Code:
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim writeRow As Long
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.Desktop + "\Repair_Income.xlsx")
oSheet = oBook.worksheets(1)
writeRow = oSheet.cells(oSheet.rows.count, 1).end(XlDirection.xlUp).row + 1
oSheet.range("a" & writeRow).value = txtInput.Text
oBook.close(True)
oSheet = Nothing
oBook = Nothing
oExcel = Nothing
End Sub
End Class
This will write to the next blank cell in column A, in Sheet 1, so change as required.
Thats works nicely thank you, however I still have this problem:
-
Oct 29th, 2013, 09:52 AM
#5
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
It seems that no matter what ""\Repair_Income.xls"" is set to it will always make it a xlsx file?
-
Oct 29th, 2013, 09:57 AM
#6
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
When I tried your code, it created a file that I couldn't open (corrupted somehow). When I started over and changed to Repair_Income2.xlsx, it worked for me, then I could open it.
I created the file first, with only a heading in cell A1, then I ran the code. See if that change works or no.
-
Oct 29th, 2013, 10:01 AM
#7
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
 Originally Posted by vbfbryce
When I tried your code, it created a file that I couldn't open (corrupted somehow). When I started over and changed to Repair_Income2.xlsx, it worked for me, then I could open it.
I created the file first, with only a heading in cell A1, then I ran the code. See if that change works or no.
Thats interesting, but for some reason, and I might just be being a noob, but when I try that the it still saves as Repair_income and does not change???
-
Oct 29th, 2013, 10:04 AM
#8
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
you changed this:
oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.Desktop + "\Repair_Income.xlsx")
to this:
oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.Desktop + "\Repair_Income2.xlsx")
right?
-
Oct 29th, 2013, 10:09 AM
#9
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
 Originally Posted by vbfbryce
you changed this:
oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.Desktop + "\Repair_Income.xlsx")
to this:
oBook = oExcel.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.Desktop + "\Repair_Income2.xlsx")
right?
Correct, now im getting this?
-
Oct 29th, 2013, 10:16 AM
#10
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
Okay, I think I fixed that bit
-
Oct 29th, 2013, 10:17 AM
#11
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
With that code you are not creating a new excel file but writtin to an existing one. That is why previously when using Repair_Income, at trying to open it with excel you still get same error, and now it is telling you that it does not exists.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Oct 29th, 2013, 10:29 AM
#12
Thread Starter
Lively Member
Re: HELP, Importing and exporting specific cells from Excel to Visual Studio
 Originally Posted by kaliman79912
With that code you are not creating a new excel file but writtin to an existing one. That is why previously when using Repair_Income, at trying to open it with excel you still get same error, and now it is telling you that it does not exists.
Yes thank you, I'm just a noob. That problem is all sorted now thank you, so now we need to import data from a cell into a text box?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|