Results 1 to 4 of 4

Thread: Writing excel file from vb

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Location
    Italy
    Posts
    6

    Question Writing excel file from vb

    Good morning,
    I've some troubles when I try to write an Excel file from visual basic: every time, the first cell is overwrite and the "counter" don't work correctly.
    I paste here some code i've used to write the document... someone can help me?
    thanks

    'find the cell where to start writing opening the existing file (and reading the number of cell where to start):

    Dim foglioExcel2 As Excel.Worksheet
    foglioExcel2 = cartExcel.Worksheets.Item(2)
    conteggio = foglioExcel2.Cells(2, 4).value
    If conteggio <> 0 Then
    contoriga = foglioExcel2.Cells(2, 4).value
    End If

    'writing data on excel file:

    foglioExcel2.Range("A2").EntireColumn.WrapText = True
    foglioExcel2.Cells(contoriga, 1).value = domande(random)
    foglioExcel2.Cells(contoriga, 2).value = UCase(scelta)
    foglioExcel2.Cells(2, 4).value = contoriga
    contogiuste = foglioExcel2.Cells(2, 5).value
    contosbagliate = foglioExcel2.Cells(2, 6).value
    foglioExcel2.Cells(2, 5).value = contogiuste + corrette
    foglioExcel2.Cells(2, 6).value = contosbagliate + sbagliate
    If correzionefile = 1 Then

    foglioExcel2.Cells(contoriga, 3).value = "CORRETTO: "
    End If
    If correzionefile = 2 Then

    foglioExcel2.Cells(contoriga, 3).value = "SBAGLIATO: " & CStr(UCase(correttes(random)))
    End If

    contoriga = contoriga + 1
    cartExcel.Save()

    Loop

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Location
    Italy
    Posts
    6

    Re: Writing excel file from vb

    If you need the full code, i paste it here:

    Imports System
    Imports System.IO
    Imports Excel = Microsoft.Office.Interop.Excel

    Module Module1

    Sub Main()

    Dim ndomande As Integer = 0
    Dim domande() As String
    Dim rispa() As String
    Dim nrispa As Integer = 0
    Dim rispb() As String
    Dim nrispb As Integer = 0
    Dim rispc() As String
    Dim nrispc As Integer = 0
    Dim rispd() As String
    Dim nrispd As Integer = 0
    Dim correttes() As String
    Dim ncorrette As Integer = 0

    'transfert data from file to arrays: domande, rispa, rispb...


    Dim appExcel As New Excel.Application
    Dim cartExcel As Excel.Workbook
    Dim foglioExcel As Excel.Worksheet

    cartExcel = appExcel.Workbooks.Open("C:\Users\fabio\Documents\Visual Studio 2012\Projects\Quizmatrice\Quizmatrice\cartella.xlsx")
    foglioExcel = cartExcel.Worksheets.Item(1)

    Dim finefile As Integer = 3057

    For ndomande = 1 To finefile
    ReDim Preserve domande(ndomande)
    domande(ndomande) = foglioExcel.Cells(ndomande, 2).value
    Next ndomande

    For nrispa = 1 To finefile
    ReDim Preserve rispa(nrispa)
    rispa(nrispa) = foglioExcel.Cells(nrispa, 3).value
    Next nrispa

    For nrispb = 1 To finefile
    ReDim Preserve rispb(nrispb)
    rispb(nrispb) = foglioExcel.Cells(nrispb, 4).value
    Next nrispb

    For nrispc = 1 To finefile
    ReDim Preserve rispc(nrispc)
    rispc(nrispc) = foglioExcel.Cells(nrispc, 5).value
    Next nrispc

    For nrispd = 1 To finefile
    ReDim Preserve rispd(nrispd)
    rispd(nrispd) = foglioExcel.Cells(nrispd, 6).value
    Next nrispd

    For ncorrette = 1 To finefile
    ReDim Preserve correttes(ncorrette)
    correttes(ncorrette) = foglioExcel.Cells(ncorrette, 7).value
    Next ncorrette




    'end transfert
    'look if the number of questions = number of answers

    If ndomande <> nrispa Or ndomande <> nrispb Or ndomande <> nrispc Or ndomande <> nrispd Or ndomande <> ncorrette Then
    Console.WriteLine("ERRORE: Il numero di domande non corrisponde al numero di risposte")
    If ndomande <> nrispa Then
    Console.WriteLine("ci sono " & CStr(nrispa) & " risposte A su " & CStr(ndomande) & " domande")
    Console.ReadKey()
    End
    End If
    If ndomande <> nrispb Then
    Console.WriteLine("ci sono " & CStr(nrispb) & " risposte B su " & CStr(ndomande) & " domande")
    Console.ReadKey()
    End
    End If
    If ndomande <> nrispc Then
    Console.WriteLine("ci sono " & CStr(nrispc) & " risposte C su " & CStr(ndomande) & " domande")
    Console.ReadKey()
    End
    End If
    If ndomande <> nrispd Then
    Console.WriteLine("ci sono " & CStr(nrispd) & " risposte D su " & CStr(ndomande) & " domande")
    Console.ReadKey()
    End
    End If
    If ndomande <> ncorrette Then
    Console.WriteLine("ci sono " & CStr(ncorrette) & " risposte su " & CStr(ndomande) & " domande")
    Console.ReadKey()
    End
    End If
    End If

    'end control



    '***************************** the program start ************************************



    Dim random As Integer = 0
    Dim scelta As String = "nullo"
    Dim discont As Integer = 0
    Dim corrette As Integer = 0
    Dim sbagliate As Integer = 0
    Dim uscitasicura As String
    Dim vrandom(1) As Integer
    Dim contrandom As Integer = 0
    Dim i As Integer = 0
    Dim index As Integer = 0

    Dim myrandom As New Random

    Dim contoriga As Integer = 2
    Dim correzionefile As Integer
    Dim contogiuste As Integer = 0
    Dim contosbagliate As Integer = 0
    Dim conteggio As Integer = 0

    Do

    correzionefile = 0

    'generate a random number
    2:

    random = myrandom.Next(ndomande)
    vrandom(contrandom) = random
    index = index + 1
    If index >= ndomande Then
    GoTo 3
    End If

    'look for the chose

    For i = 0 To contrandom - 1
    If vrandom(i) = random Then
    index = index - 1
    GoTo 2
    End If
    Next i



    contrandom = contrandom + 1
    ReDim Preserve vrandom(contrandom)



    'print on screen a random question and its possible answers

    Console.Clear()
    Console.WriteLine(" ")
    Console.WriteLine(domande(random))
    Console.WriteLine(" ")
    Console.WriteLine(rispa(random))
    Console.WriteLine(rispb(random))
    Console.WriteLine(rispc(random))
    Console.WriteLine(rispd(random))
    Console.WriteLine(" ")
    1:
    Console.WriteLine("digita la risposta corretta [per uscire, digita E]")
    scelta = Console.ReadLine
    scelta = UCase(scelta)
    Console.WriteLine(" ")


    'controllo la scelta:
    'procedura di logout

    If scelta = "E" Then
    Console.WriteLine("sei sicuro di voler uscire? [S/N]")
    uscitasicura = Console.ReadLine
    uscitasicura = UCase(uscitasicura)
    If uscitasicura = "S" Then
    3:
    Console.Clear()
    Console.WriteLine(" ")
    Console.WriteLine("I tuoi risultati:")
    Console.WriteLine("CORRETTE: " & CStr(corrette) & " SBAGLIATE: " & CStr(sbagliate))
    Console.WriteLine(" ")
    Console.WriteLine("Arrivederci!")
    Console.ReadKey()
    cartExcel.Close(False)
    appExcel.Quit()
    End
    Else
    GoTo 1
    End If
    End If



    'look if the chosen answer is the correct one

    If scelta = "A" Then
    If correttes(random) = "a" Then
    corrette = corrette + 1
    correzionefile = 1
    Console.WriteLine("CORRETTO: " & CStr(rispa(random)))
    Console.ReadKey()
    Else
    sbagliate = sbagliate + 1
    correzionefile = 2
    Console.WriteLine("SBAGLIATO: la risp corretta era la " & CStr(correttes(random)))
    Console.ReadKey()
    End If
    End If
    If scelta = "B" Then
    If correttes(random) = "b" Then
    corrette = corrette + 1
    correzionefile = 1
    Console.WriteLine("CORRETTO: " & CStr(rispb(random)))
    Console.ReadKey()
    Else
    sbagliate = sbagliate + 1
    correzionefile = 2
    Console.WriteLine("SBAGLIATO: la risp corretta era la " & CStr(correttes(random)))
    Console.ReadKey()
    End If
    End If
    If scelta = "C" Then
    If correttes(random) = "c" Then
    corrette = corrette + 1
    correzionefile = 1
    Console.WriteLine("CORRETTO: " & CStr(rispc(random)))
    Console.ReadKey()
    Else
    sbagliate = sbagliate + 1
    correzionefile = 2
    Console.WriteLine("SBAGLIATO: la risp corretta era la " & CStr(correttes(random)))
    Console.ReadKey()
    End If
    End If
    If scelta = "D" Then
    If correttes(random) = "d" Then
    corrette = corrette + 1
    correzionefile = 1
    Console.WriteLine("CORRETTO: " & CStr(rispd(random)))
    Console.ReadKey()
    Else
    sbagliate = sbagliate + 1
    correzionefile = 2
    Console.WriteLine("SBAGLIATO: la risp corretta era la " & CStr(correttes(random)))
    Console.ReadKey()
    End If
    End If

    If scelta <> "A" And scelta <> "B" And scelta <> "C" And scelta <> "D" And scelta <> "E" Then
    Console.WriteLine("Non ho capito la risposta ")
    GoTo 1
    End If

    'find the cell where to start writing opening the existing file (and reading the number of cell where to start):

    Dim foglioExcel2 As Excel.Worksheet
    foglioExcel2 = cartExcel.Worksheets.Item(2)
    conteggio = foglioExcel2.Cells(2, 4).value
    If conteggio <> 0 Then
    contoriga = foglioExcel2.Cells(2, 4).value
    End If

    'writing data on excel file:

    foglioExcel2.Range("A2").EntireColumn.WrapText = True
    foglioExcel2.Cells(contoriga, 1).value = domande(random)
    foglioExcel2.Cells(contoriga, 2).value = UCase(scelta)
    foglioExcel2.Cells(2, 4).value = contoriga
    contogiuste = foglioExcel2.Cells(2, 5).value
    contosbagliate = foglioExcel2.Cells(2, 6).value
    foglioExcel2.Cells(2, 5).value = contogiuste + corrette
    foglioExcel2.Cells(2, 6).value = contosbagliate + sbagliate
    If correzionefile = 1 Then

    foglioExcel2.Cells(contoriga, 3).value = "CORRETTO: "
    End If
    If correzionefile = 2 Then

    foglioExcel2.Cells(contoriga, 3).value = "SBAGLIATO: " & CStr(UCase(correttes(random)))
    End If

    contoriga = contoriga + 1
    cartExcel.Save()

    Loop



    End Sub

    End Module

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Writing excel file from vb

    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Location
    Italy
    Posts
    6

    Re: Writing excel file from vb

    Quote Originally Posted by si_the_geek View Post
    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum
    I'm sorry...

    Anyway I've solved my question, thank you ;-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width