VB Daten aus Excel Datei1 in Excel Datei2 kein VBA code

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von Akki.

    VB Daten aus Excel Datei1 in Excel Datei2 kein VBA code

    Hallo VB FREUNDE,

    habe wahrscheinlich eine simple Frage, kann sie nicht lösen sitze auf dem Schlauch.


    Bei der Kopie zwischen zwei Excel Dateien tritt ein Fehler(Fehler@Zeile 44) zur Laufzeit auf.
    Ich habe habe lange versucht aber keine Ahnung womit es zusammen hängt.
    Meine Vermutung ein Logikfehler

    Schaut mal bitte drüber:


    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub CopyExcelToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyExcelToExcel.Click
    3. Dim oExcel As Object
    4. Dim oBook As Object
    5. Dim oSheet As Object
    6. Dim FileFormat As Object
    7. FileFormat = 56
    8. 'Start a new workbook in Excel.
    9. oExcel = CreateObject("Excel.Application")
    10. oBook = oExcel.Workbooks.Add
    11. Dim FilePath As String = "C:\real"
    12. 'Add data to cells of the first worksheet in the new workbook.
    13. oSheet = oBook.Worksheets(1)
    14. oSheet.Range("A1").Value = "Counter"
    15. oSheet.Range("B1").Value = "PVI"
    16. oSheet.Range("C1").Value = "BuildData"
    17. oSheet.Range("A2").Value = "1"
    18. oSheet.Range("B2").Value = "4654654UR"
    19. 'Save the Workbook and quit Excel.
    20. oBook.SaveAs(FilePath, FileFormat)
    21. oExcel.Quit()
    22. oExcel = Nothing
    23. Dim Projekt As New Object
    24. Dim objExcel As New Object
    25. Dim puffer As String
    26. Projekt = CreateObject("excel.application")
    27. 'Projekt.Application.Workbooks.Open("C:\Tabelle2.xls")
    28. objExcel = Projekt.Application.Workbooks.Open("C:\Tabelle2.xls")
    29. 'objExcel = Projekt.Worksheets(1)
    30. puffer = objExcel.Range("A2").Value '<<<<<<<<<<<<<<< Hier taucht der Fehler: Der öffentliche Member Range für den Typ Workbook wurde nicht gefunden
    31. oSheet.Range("A3").Value = puffer
    32. Projekt.Visible = False
    33. Projekt.Quit()
    34. End Sub
    35. End Class


    Vielen Dank für eure Hilfe!!!
    Range is kein Member von Workbook sondern von Worksheet!
    Ergo kannste nich ein Range vom Workbook sondern nur einen Range aufm Tabellenblatte ansprechen!
    Btw....
    Option Strict ON!
    hätteste den Unsinn direkt bemerkt!
    Wozu den ganzen Kram in Objecte definieren wenn du direkt die richtigen Tyen nutzen kannst?

    VB.NET-Quellcode

    1. Dim xlApp as new Excel.Apllication
    2. Dim xlWorkbook as Excel.Workbook = xlApp.Workbooks.Opne("DeinBookhalt")
    hey vielen Dank!

    Eine Frage habe ich noch wenn ich das ganze mit den richtigen Typen machen, wie du es vorgeschlagen hast.

    Wie kann ich dann ein neue Excel Datei erstellen und mit Spalten füllen bsp
    diesen Teil

    VB.NET-Quellcode

    1. oBook = oExcel.Workbooks.Add
    2. Dim FilePath As String = "C:\real"
    3. 'Add data to cells of the first worksheet in the new workbook.
    4. oSheet = oBook.Worksheets(1)
    5. oSheet.Range("A1").Value = "Counter"
    6. oSheet.Range("B1").Value = "PVI"
    7. oSheet.Range("C1").Value = "BuildData"
    8. 'oSheet.Range("A1:B1").Font.Bold = True
    9. oSheet.Range("A2").Value = "1"
    10. oSheet.Range("B2").Value = "4654654UR"


    mit richtigen Typen übersetzen. Wie würde es dann aussehen??

    Danke schön!
    Danke dir!

    Ich habe das auf Worksheet umgeändert. Jedoch bekomme ich jetzt ein neuen Fehler Ausnahme von HRESULT: 0x800A01A8

    VB.NET-Quellcode

    1. Dim Projekt As New Object
    2. Dim objExcel As New Object
    3. 'Dim WB As Workbook
    4. 'Dim Ziel As Range
    5. Dim puffer As String
    6. Projekt = CreateObject("excel.application")
    7. Projekt.Application.Workbooks.Open("C:\Tabelle2.xls")
    8. objExcel = Projekt.Worksheets(1)
    9. puffer = objExcel.Range("A2").Value
    10. oSheet.Range("A3").Value = puffer '<<<< Hier taucht dieser Fehler auf Ausnahme von HRESULT: 0x800A01A8


    Weißt du woran das liegen könnte mehr wird auch nicht angezeigt

    Danke FlohFuchs
    Tu dir einen gefallen... schreib mal ganz oben Option Strict ON hin...

    und dann arbeite mit den Typen.... objExcel is halt nunmal immer noch kein woksheet...
    schau dir mal folgendes an, und am besten verstehen ;)
    Imports Microsoft.Office.Interop
    Public Class Form1

    VB.NET-Quellcode

    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2. Dim xlApp As New Excel.Application
    3. Dim xlWorkbookUrsprung As Excel.Workbook = xlApp.Workbooks.Open(IO.Path.Combine(
    4. My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    5. "MappeUrsprung.xlsx"))
    6. Dim xlWorkbookZiel As Excel.Workbook = xlApp.Workbooks.Open(IO.Path.Combine(
    7. My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    8. "MappeZiel.xlsx"))
    9. Dim xlWorksheetUrsprung As Excel.Worksheet = CType(xlWorkbookUrsprung.Worksheets(0), Excel.Worksheet)
    10. Dim xlWorksheetZiel As Excel.Worksheet = CType(xlWorkbookZiel.Worksheets("Tabelle3"), Excel.Worksheet)
    11. xlWorksheetZiel.Range("A22").Value = xlWorksheetUrsprung.Range("B4").Value
    12. End Sub
    Bilder
    • OptionStrict1.gif

      258,07 kB, 1.242×797, 147 mal angesehen
    • OptionStrict2.gif

      271,49 kB, 1.068×794, 165 mal angesehen
    Danke schön. :)

    Ich habe es versucht anzu passen bekomme jedoch immernoch Fehler. ?( :(
    Ich habe deinen Rat befolgt und habe "Option Strict On" aktiviert.

    bekomme drei Fehler Zeile 15, 33

    Ich habe meine datei angehängt.

    Wenn du zeit und Lust hast kannst ja mal schauen woran es liegt.

    Vielen Dank für deine Mühe und Arbeit!

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports Microsoft.Office.Interop
    3. Public Class Form1
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5. 'Dim m_xlDateiname As String
    6. 'Dim m_xlApp As Excel.Application
    7. 'Dim xlMappe As Excel.Workbook
    8. 'Dim xlBlatt As Excel.Worksheet
    9. 'Dim xlZelle As Excel.Range
    10. 'Dim FileFormat As Object
    11. 'FileFormat = 56
    12. 'Dim FilePath As String = "C:\real"
    13. Dim xlApp As New Excel.Application '<<<<<<<<<<<<<<<<<<<<<New kann nicht auf eine Schnittstelle verwendet werden
    14. 'Dim xlWorkbookUrsprung As Excel.Workbook = xlApp.Workbooks.Open(IO.Path.Combine(
    15. 'My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    16. '"MappeUrsprung.xlsx"))
    17. Dim xlWorkbookUrsprung As Excel.Workbook = xlApp.Workbooks.Open("C:\Tabelle2.xls")
    18. Dim xlWorkbookZiel As Excel.Workbook = xlApp.Workbooks.Open("C:\Tabelle.xls")
    19. 'Dim xlWorkbookZiel As Excel.Workbook = xlApp.Workbooks.Open(IO.Path.Combine(
    20. 'My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    21. '"MappeZiel.xlsx"))
    22. Dim xlWorksheetUrsprung As Excel.Worksheet = CType(xlWorkbookUrsprung.Worksheets(0), Excel.Worksheet)
    23. Dim xlWorksheetZiel As Excel.Worksheet = CType(xlWorkbookZiel.Worksheets("Tabelle"), Excel.Worksheet)
    24. xlWorksheetZiel.Range("A2").Value = xlWorksheetUrsprung.Range("A1").Value '<<<<<<<<Option Strict On lässt spätes Binden nicht zu
    25. 'xlWorkbook.Close()
    26. xlApp.Quit()
    27. 'xlZelle = Nothing
    28. 'xlBlatt = Nothing
    29. 'xlMappe = Nothing
    30. 'xlApp = Nothing
    31. End Sub
    32. End Class
    Dateien
    • Excel.zip

      (169,87 kB, 119 mal heruntergeladen, zuletzt: )
    Probier mal das. Bei mir wirft wenigstens jetzt Intelli keine Fehler. ACHTUNG: habe Imports verändert.

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports iExcel = Microsoft.Office.Interop.Excel
    3. Public Class Form1
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5. 'Dim m_xlDateiname As String
    6. 'Dim m_xlApp As Excel.Application
    7. 'Dim xlMappe As Excel.Workbook
    8. 'Dim xlBlatt As Excel.Worksheet
    9. 'Dim xlZelle As Excel.Range
    10. 'Dim FileFormat As Object
    11. 'FileFormat = 56
    12. 'Dim FilePath As String = "C:\real"
    13. Dim xlApp As New iExcel.Application
    14. 'Dim xlWorkbookUrsprung As Excel.Workbook = xlApp.Workbooks.Open(IO.Path.Combine(
    15. 'My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    16. '"MappeUrsprung.xlsx"))
    17. Dim xlWorkbookUrsprung As iExcel.Workbook = xlApp.Workbooks.Open("C:\Tabelle2.xls")
    18. Dim xlWorkbookZiel As iExcel.Workbook = xlApp.Workbooks.Open("C:\Tabelle.xls")
    19. 'Dim xlWorkbookZiel As Excel.Workbook = xlApp.Workbooks.Open(IO.Path.Combine(
    20. 'My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    21. '"MappeZiel.xlsx"))
    22. Dim xlWorksheetUrsprung As iExcel.Worksheet
    23. xlWorksheetUrsprung = CType(xlWorkbookUrsprung.Worksheets(0), iExcel.Worksheet)
    24. Dim xlWorksheetZiel As iExcel.Worksheet =
    25. CType(CType(xlWorkbookZiel.Worksheets("Tabelle"), Excel.Worksheet),
    26. iExcel.Worksheet)
    27. xlWorksheetZiel.Range("A2").Value = xlWorksheetUrsprung.Range("A1").Value
    28. 'xlWorkbook.Close()
    29. xlApp.Quit()
    30. 'xlZelle = Nothing
    31. 'xlBlatt = Nothing
    32. 'xlMappe = Nothing
    33. 'xlApp = Nothing
    34. End Sub
    35. End Class