VBA to

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

I have following code which is giving me 72 errors in 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
    LVL 13

    Expert Comment

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

    Author Comment

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

    Accepted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now