I'm trying to read an Excel worksheet and do a find/replace for all of the commas "," in any of the cells in the sheet and replace them with a dash "-" I can get as far as getting the workbook opened but I'm not sure where to go from here. Is there a simple method to accomplish this?
Below is the code I have so far: - Dim exl As Microsoft.Office.Interop.Excel.Application
-
-
Dim sht As Microsoft.Office.Interop.Excel.Worksheet
-
-
exl = New Microsoft.Office.Interop.Excel.Application
-
-
exl.Workbooks.Open("C:\...\WHOLESALEpl.xls") ' Actual path omitted
10 4963
I'm trying to read an Excel worksheet and do a find/replace for all of the commas "," in any of the cells in the sheet and replace them with a dash "-" I can get as far as getting the workbook opened but I'm not sure where to go from here. Is there a simple method to accomplish this?
Below is the code I have so far: - Dim exl As Microsoft.Office.Interop.Excel.Application
-
-
Dim sht As Microsoft.Office.Interop.Excel.Worksheet
-
-
exl = New Microsoft.Office.Interop.Excel.Application
-
-
exl.Workbooks.Open("C:\...\WHOLESALEpl.xls") ' Actual path omitted
So far so good. Now, if you have doubts working with your excel's vba, just record a macro and copy-paste the code. For what you want, something like this might be of help: - Dim Obj1 as object
-
set obj1 = createobject("excel.application")
-
ob1.workbooks.open("c:\...\WHOLESALEpl.xls")
-
obj1.Cells.Replace What:=",", Replacement:="-", LookAt:=2, SearchOrder _
-
:=1, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
-
obj1.visible=true
Now, just make sure to replace the xlconstants with its numeric value, since other VB than Excel's VBA wont recognize them. That's why, even when the macro recorder wrote LookAt:=xlPart, you should use LookAt:=2
HTH
You totally ROCK! I knew there had to be a relatively easy method due to the complexity of the excel.application object. Just wasn't sure how to access it. Your sample code worked like a charm! Thanks a Million!
KK
Ok, here is what I have now: -
Dim exl As Microsoft.Office.Interop.Excel.Application
-
-
Dim sht As Microsoft.Office.Interop.Excel.Worksheet
-
-
exl = New Microsoft.Office.Interop.Excel.Application
-
-
exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")
-
-
sht = exl.Workbooks(1).ActiveSheet
-
-
exl.Cells.Replace(",", "-", 2, 1, False, False, False)
-
-
sht.SaveAs("C:\...\WHOLESALEplfixed.csv", _
-
FileFormat:="xlCSV")
-
-
exl.Application.Quit()
Ok, here is what I have now:
Glad to be of help.
Seems good, is it allright?
Kad
It works great (does the replace in all cells) until I try to do the SaveAs method. It then returns an unhandled COMException error, specifically: "Exception from HRESULT: 0x800A03EC" I have tried most of the other supposed "fixes" that I was able to find online (e.g.: Setting "Trust VB Project") in the actual Excel application. Alas, none of them were effective in alleviating the error. Any help is greatly appreciated.
KK
Actually, I did change a couple of lines of code in other foiled attempts at removing the error. The code currently looks like this: -
Try
-
-
Dim exl As New Microsoft.Office.Interop.Excel.Application
-
-
Dim xlwb As Microsoft.Office.Interop.Excel.Workbook
-
-
'Dim sht As Microsoft.Office.Interop.Excel.Worksheet
-
-
xlwb = exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")
-
-
exl.Cells.Replace(",", "-", 2, 1, False, False, False)
-
exl.DefaultSaveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV
-
exl.ActiveWorkbook.SaveAs("C:\...\WHOLESALEplfixed.csv", _
-
FileFormat:="xlCSV")
-
-
exl.Application.Quit()
-
Catch ex As Exception
-
MsgBox(ex.Message)
-
-
End Try
I finally got it to work!!! Here is the VB code: - Try
-
Dim exl As New Microsoft.Office.Interop.Excel.Application
-
-
Dim xlwb As Microsoft.Office.Interop.Excel.Workbook
-
-
xlwb = exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")
-
-
exl.Cells.Replace(",", "-", 2, 1, False, False, False)
-
exl.DefaultSaveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV
-
exl.ActiveWorkbook.SaveAs("C:\...\WHOLESALEplfixed.csv", _
-
6)
-
-
exl.Application.Quit()
-
Catch ex As Exception
-
MsgBox(ex.Message)
-
-
End Try
Note that I changed the File Format:= "xlCSV" to 6 (the value for xlCSV) did a "build" (since I am working in VS2008) instead of just a "debug". Ran it and it works like a charm!
Thanks again for your help & I hope this post can help another developer with a similar situation.
Cheers!
KK
One thing I noticed while debugging was that the EXCEL application was still running (according to Task Manager) until I stopped debugging. Is this normal behavior? Is there a way to make excel exit, to save system overhead, before the program exits completely? I thought that was handled by the Application.Quit method? Do I need to set it to Null or Nothing?
BTW & FTR - TSDN or Bytes or whatever we are calling it is the best forum on the web!!! I have posted on other forums and get the quickest, and most useful information from THIS site.
Thanks,
KK
One thing I noticed while debugging was that the EXCEL application was still running (according to Task Manager) until I stopped debugging. Is this normal behavior? Is there a way to make excel exit, to save system overhead, before the program exits completely? I thought that was handled by the Application.Quit method? Do I need to set it to Null or Nothing?
That issue has been long discussed here. Search it in the site, you might find something. I'll check it out later too.
One quick way will be setting the application visible to true, and close it normaly
BTW & FTR - TSDN or Bytes or whatever we are calling it is the best forum on the web!!! I have posted on other forums and get the quickest, and most useful information from THIS site.
Thanks,
KK
^.^
Wow, those are nice words. Thank you.
As far as killing the instance of Excel in your processes, I found this kicking around another forum: - System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oExcel)
Explanation given was this: a COM assembly has a life of its own - freestanding from your code. This line is what instructs that object to die. I tested this fix, and it seemed to work for me.
Happy coding!
- MeanDude
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Julia |
last post by:
Hi,
I am trying to use the Microsoft.Office.Interop.Excel.dll but I dont know
how. First, how do I install it? I have tried to run the register.bat but it
doesnt work. How should I do this?
...
|
by: ©pEIO |
last post by:
Example:
Dim xlsExcel As Excel.Application
Dim wkbExcel As Excel.Workbook
Dim wksExcel As Excel.Worksheet
xlsExcel = New Excel.Application
wkbExcel = xlsExcel.Workbooks.Open("C:\Test.xls")...
|
by: C# newbie with big problems |
last post by:
Ok, I've tried everything I could come up with but nothing is working.
I am creating an application which needs to post information into an
excel document. I have not yet had a chance to play with...
|
by: Cheryl |
last post by:
I am new to C# and I am trying to convert this vba code to C#:
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"),
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote,...
|
by: pradeepkumarappagari |
last post by:
by using 'Microsoft.Office.Interop.Excel.dll' can we set print settings of an excel file?
if possible tell me what and all we can do by using 'Microsoft.Office.Interop.Excel.dll'.
|
by: insanomania911 |
last post by:
I tried everything:
- the refrerence to MS OFFICE 11.0
- Installed the PIA to the Office 2003 and made sure that the files are in the assembly folder
When I add the reference I only get...
|
by: Saradhamba |
last post by:
Hi,
I have made use of the excel objects to export my data to excel. When i try debugging it throws error in the excel object being created. It throws "System.FileNotFound.Exception".
Any help...
|
by: bfrank1972 |
last post by:
I want to be able to get a list of all custom named fields in an Excel worksheet, but I am having trouble with this. In the code below, access to a field that I named "DEALCODE" works fine - I get...
|
by: romcab |
last post by:
Hi guys,
I'm trying to manipulate excel using a C#.net and my problem is that I cannot add the namespace Microsoft.Office.Interop.Excel. Does anyone know why? Do I need to download it...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |