Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


VBA to VB.net

Posted on 2007-08-02
Medium Priority
Last Modified: 2011-04-14

I have following code which is giving me 72 errors in VB.net? I am trying to convert from VBA

        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "CSV Files (*.csv)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If (openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim xlTmp As Microsoft.Office.Interop.Excel.Application
            Dim ExWorkBook As Microsoft.Office.Interop.Excel.Workbook
            Dim ActiveSheet As Microsoft.Office.Interop.Excel.Workbook
            Dim xlValues As Microsoft.Office.Interop.Excel.Workbook
            Dim ExWorksheet As Microsoft.Office.Interop.Excel.Workbook
            Dim Range As Microsoft.Office.Interop.Excel.Range
            Dim xlUp As Microsoft.Office.Interop.Excel.Workbook
            Dim ActiveCell As Microsoft.Office.Interop.Excel.Workbook
            'Dim ExWorksheet As Microsoft.Office.Interop.Excel.Workbook

            xlTmp = CreateObject("Excel.Application")
            ExWorkBook = xlTmp.Workbooks.Open(openFileDialog1.FileName)
            MsgBox("Do you want to continue with this file?", vbOKOnly, "Yesl")
            xlTmp.Cells(5, 2).Formula = "BULSW"
            ActiveSheet.Cells(4, 3).Copy()
            ActiveSheet.Cells(4, 5).PasteSpecial(Paste:=xlValues)
            ActiveSheet.Cells(4, 3).Clear()
            ActiveSheet.Cells(5, 3).Copy()
            ActiveSheet.Cells(5, 5).PasteSpecial(Paste:=xlValues)
            ActiveSheet.Cells(5, 3).Clear()
            ActiveSheet.Cells(6, 3).Copy()
            ActiveSheet.Cells(6, 5).PasteSpecial(Paste:=xlValues)
            ActiveSheet.Cells(6, 3).Clear()
            ActiveSheet.Cells(6, 4).Copy()
            ActiveSheet.Cells(6, 6).PasteSpecial()
            ActiveSheet.Cells(6, 4).Clear()

            Dim Lst As Long
            Dim rng As Microsoft.Office.Interop.Excel.Range

            ' the last cell in col i that has a value

            'save the rowNo
            Lst = ActiveCell.Row

            'Define the range
            rng = Range("i8:i" & Lst)

            'Go to cell that should contain the sum
            ActiveCell.Offset(1, 0).Select()

            'Calculate the sum
            ActiveCell.Formula = "=SUM(" & rng.Address & ")"

            ActiveSheet.Range("a" & ActiveSheet.Rows.Count) _
            .End(xlUp).Offset(1, 0).Value = "Total Hours"

            ExWorkBook.SaveAs("c:\" & Format(Now, "mmddyy"))


            MsgBox("Modification Completed", vbOKOnly, "Hello")

            ExWorksheet = Nothing
            ExWorkBook = Nothing
            xlTmp = Nothing

            TextBox1.Text = "No File is selected browse for the file to modify"

        End If

Can you tell me what are the mistakes here?

Question by:CalmSoul
  • 2
LVL 13

Expert Comment

ID: 19618975
main mistake is you cannot use ActiveSheet here
Set ExWorkSheet = ExWorkbook.Sheets(1)
ExWorkSheet.Cells(i,1) = "whatever you want"

Author Comment

ID: 19619023
What about "Paste:=xlValues"?
is that ok?
LVL 13

Accepted Solution

hiteshgoldeneye earned 2000 total points
ID: 19619077

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question