Toon posts:

[vb6] next row excel

Pagina: 1
Acties:

Verwijderd

Topicstarter
hi,
heb een probleem met excel in vb6
de bedoeling van men programmatje is dat textboxen worden ingevuld met waarden per rij
de eerste rij waarden kan ik laden met de volgende code


Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set Xlsfile = CreateObject("Excel.Application")
If fso.FileExists(path) Then
Xlsfile.Application.Workbooks.Open (path)
Else
MsgBox "De locatie:' " & path & "' bestaat niet", vbOKOnly, "Bestand locatie"
End If
frmMdb.txtBss.Text = Xlsfile.cells(1, 1).Value
Xlsfile.Quit



deze code word uitgevoerd door een commandbutton
het probleem is nu dat ik een andere commandbutton gebruik om naar de volgende rij te gaan,
zou er iemand weten hoe ik dit moet doen

heb al veel op het internet gezocht maar niets nuttigs gevonden

thx

  • Daos
  • Registratie: Oktober 2004
  • Niet online
Je kan de rij opslaan in een globale variabele (Dim buiten een Sub/Function).

Let er wel op dat je programma traag wordt als je voor elke rij je bestand weer gaat openen en sluiten.


Offtopic:
Gebruikt op GOT code tags en laat de code inspringen na een if. Zoiets dus:
Visual Basic:
1
2
3
4
5
If fso.FileExists(path) Then
    Xlsfile.Application.Workbooks.Open (path)
Else
    MsgBox "De locatie:' " & path & "' bestaat niet", vbOKOnly, "Bestand locatie"
End If

Verwijderd

Topicstarter
de code tags heb ik gevonden ;)

heb je toevallig geen voorbeeldje van wat je bedoeld?

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
private sub commandbutton1_click()
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set Xlsfile = CreateObject("Excel.Application")
If fso.FileExists(path) Then
Xlsfile.Application.Workbooks.Open (path)
Else
MsgBox "De locatie:' " & path & "' bestaat niet", vbOKOnly, "Bestand locatie"
End If
frmMdb.txtBss.Text = Xlsfile.cells(1, 1).Value
Xlsfile.Quit
end sub


de bedoeling is eigenlijk dat de x bij elke maal dat er geklikt wordt op commandbutton2, +1 gedaan word
Visual Basic:
1
2
3
private sub commandbutton2_click()
Xlsfile.cells(x, 1).Value
end sub


en hoe zou ik het kunnen oplossen dat ik mijn excel niet de hele tijd moet openen?

thx

  • Tukk
  • Registratie: Januari 2002
  • Laatst online: 21-08 16:52

Tukk

De α-man met het ẞ-brein

Naast de globale variabele kun je ook de cell selecteen, en dan het volgende nemen

Visual Basic:
1
  Set newCell  = application.selection.offset(1)

Wat de syntax van de offset functie is moet je even kijken in de help file.


edit:
Wacht even, volgens mij snap ik je probleem.

Waarom sluit je excel af, nadat je de cell hebt uigelezen?
Je kan het object toch gewoon in je geheugen houden?

[ Voor 30% gewijzigd door Tukk op 24-07-2005 12:04 ]

Q: How many geeks does it take to ruin a joke? A: You mean nerd, not geek. And not joke, but riddle. Proceed.


  • Daos
  • Registratie: Oktober 2004
  • Niet online
Zoiets?:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
Dim x As Integer

Private Sub CommandButton1_Click()
    x = 10
End Sub

Private Sub CommandButton2_Click()
    MsgBox x
    x = x + 1
End Sub

Verwijderd

Topicstarter
dit heb ik voor het moment


public variabelen zijn
Visual Basic:
1
2
3
Public xlsfile As Object
Public path As String
Public x As Integer


de eerste button waarbij excel geopend word
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Private Sub commandbutton1_Click()

  path = InputBox("Geef het pad in van het excel bestand", "Excel bestand openen")
  
  If path = "" Then
    MsgBox ("Path is niet ingevuld")
  Else
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set xlsfile = CreateObject("Excel.Application")
      If fso.FileExists(path) Then
       xlsfile.Application.Workbooks.Open (path)
      Else
       MsgBox "De locatie:' " & path & "' bestaat niet", vbOKOnly, "Bestand locatie"
      End If
      frmMdb.txtBss.Text = xlsfile.cells(1, 1).Value
  End If
  
End Sub


de 2e knop waarbij excel word afgesloten
Visual Basic:
1
2
3
4
  x = x + 1
  frmMdb.txtBss.Text = xlsfile.cells(x, 1).Value
  xlsfile.Quit
  Set xlsfile = Nothing


hierbij geeft hij runtime error "object variabele or with variabele not set" op
Visual Basic:
1
frmMdb.txtBss.Text = xlsfile.cells(x, 1).Value


wat doe ik verkeerd?

thx

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 00:45

Creepy

Tactical Espionage Splatterer

Wat heb je zelf al geprobeerd om je foutmelding op te lossen? Aan de foutmelding te zien gok ik erop dat je een variabele gebruikt die niet bestaat, of cells() geeft niet terug wat jij verwacht.

Anyway, we hebben tegenwoordig een speciaal forum voor Office applicaties e.d. namelijk: Officesuites en -software. Dus ik move je topic bij deze.

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


Verwijderd

Topicstarter
public variabelen zijn

Visual Basic:
1
2
3
Public xlsfile As Object
Public path As String
Public x As Integer

de eerste button waarbij excel geopend word

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Private Sub commandbutton1_Click()

  path = InputBox("Geef het pad in van het excel bestand", "Excel bestand openen")
  
  If path = "" Then
    MsgBox ("Path is niet ingevuld")
  Else
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set xlsfile = CreateObject("Excel.Application")
      If fso.FileExists(path) Then
       xlsfile.Application.Workbooks.Open (path)
      Else
       MsgBox "De locatie:' " & path & "' bestaat niet", vbOKOnly, "Bestand locatie"
      End If
      frmMdb.txtBss.Text = xlsfile.cells(1, 1).Value
  End If
  
End Sub

de 2e knop waarbij excel word afgesloten

Visual Basic:
1
2
3
4
  x = x + 1
  frmMdb.txtBss.Text = xlsfile.cells(x, 1).Value
  xlsfile.Quit
  Set xlsfile = Nothing


hierbij geeft hij runtime error "object variabele or with variabele not set" op
Visual Basic:
1
2
    
frmMdb.txtBss.Text = xlsfile.cells(x, 1).Value


het werkt wel als ik dit bij commandbutton2 toevoeg maar dan opend hij opnieuw een excel
Visual Basic:
1
  Set xlsfile = CreateObject("Excel.Application")


hoe kan ik dit oplossen zodat dit werkt zonder dat ik de hele tijd het excel bestand moet openen en afsluiten

thx

Verwijderd

Verwijderd schreef op zondag 24 juli 2005 @ 13:26:
hoe kan ik dit oplossen zodat dit werkt zonder dat ik de hele tijd het excel bestand moet openen en afsluiten

thx
Zoals al eerder aangegeven, waarom sluit je je excel applicatie iedere keer als je je op knop 2 klikt ? Dat is namelijk je probleem !

Verwijderd

Topicstarter
als ik dit niet doe opend hij elke keer als ik op commandbutton2 click het excel bestand

Verwijderd

Topicstarter
mensen echt bedankt voor de hulp

het is opgelost

thx
Pagina: 1