Aller au contenu
Zebulon
  • Pas encore inscrit ?

    Pourquoi ne pas vous inscrire ? C'est simple, rapide et gratuit.
    Pour en savoir plus, lisez Les avantages de l'inscription... et la Charte de Zébulon.
    De plus, les messages que vous postez en tant qu'invité restent invisibles tant qu'un modérateur ne les a pas validés. Inscrivez-vous, ce sera un gain de temps pour tout le monde, vous, les helpeurs et les modérateurs ! :wink:

conversion d'un code vba en vb


samichac

Messages recommandés

Bonjour

 

j'ai un programme ecrit en vba sur excel. je suis en train de le reecrire en vb en reprenant les memes fonctions et subs mais en adaptant le langage du programme au visual basic.

 

lors du test de l'unes des fonction, j'ai fait face a une erreur:

la fonction:

Function nbl() As Integer  'cette fonction compte le nombre de points inseres dans le tableau'



	Dim i As Integer
	Dim j As Integer

	i = 1
	j = 0

	If sdc1.Cells(2, 2) = 0 Then nbl = 0   ' sdc est un tableau excel place sur la form



	If sdc1.Cells(2, 2) <> 0 Then
		While (sdc1.Cells(i, 1) <> 0)

			j = j + 1
			i = i + 1
		End While

		nbl = j
	End If
End Function

l'erreur rencontree est:

 

Operator '=' is not defined for type 'Range' and type 'Integer'.

 

je ne comprend pas ce qui se passe.

si quelqu'un saurait se serait sympa de m'expliquer. Il faut noter que je ne suis pas pro en programmation.

J'utilise visual basic 2005.

 

 

Merci...

Lien vers le commentaire
Partager sur d’autres sites

Bonsoir j'ai retouché le code

Function nbl() As Integer
Dim i As Integer
Dim j As Integer
i = 1
j = 0

If sdc1.Cells(2, 2) = 0 Then
nbl = 0
Else
Do While (sdc1.Cells(i, 1) <> 0) 's'execute tant que (sdc1.Cells(i, 1) <> 0) est vrai
	j = j + 1
	i = i + 1
Loop
nbl = j
End If

End Function

j'utilise la fonction do ...... loop avec l'instruction while, qui permet de créer une boucle(bien faire attention qu'elle ne soit pas infinie, car le programme s'éxecutera sans fin) et continue avec while qui vérifie si la condition est vrai

Lien vers le commentaire
Partager sur d’autres sites

Sans avoir fait des masses de VB / VBA, je pense que le message d'erreur concerne la ligne "If ..." puisque c'est la seule à utiliser l'opérateur '=' avec autre chose que des symboles explicitement définis comme étant des Integer ...

Apparemment sdc1.Cells(a, b) renvoie un Range et non un Integer ...

Quel est le type de sdc1 ?

Lien vers le commentaire
Partager sur d’autres sites

sdc1 est une spreadsheet inseree sur la form grace au composant excel spreadsheet.

 

generalement pour designer le contenu d'une case du tableau on note sdc1(cad nom du tableau).cells(ligne, colonne)

 

la phrase donnant une surbrillance est

 

If sdc1.Cells(2, 2) = 0 Then

nbl = 0

 

 

Mais c'est bon j'ai pu faire ce que je voulais sur visual basic 6. je voulais ouvrir la form ecrite en vba et la sauver sous forme de fichier .exe.

et visual basic 6 a reconnu le codage sans probleme, ce qui est etonant puisqu'en general ce sont les nouvelles versions qui lisent tout!!!

Par contre je ne dirai pas non si quelqu'un m'expliquerai qu'est ce qui se passe.

 

Et j'aurais par la meme occasion une autre question(que je vous pose tout de suite au lieu de reecrire un nouveau sujet). je tente de faire un graphe sur une form en vba.

pour cela j'ai cree un espace graphe en utilisant le composant microsoft office chart 11.0

avec un spreadsheet de excel sur la form a l'aide du composant microsoft office spreadsheets.

 

Le graphe doit prendre ses valeurs du spreadsheet.Dans l'aide , j'ai trouve un exemple de code que je vous presente:

Sub Window_OnLoad()

Dim chConstants

Spreadsheet1.ActiveSheet.Cells.Clear
Spreadsheet1.ActiveSheet.Cells(2, 1).Value = "Car"
Spreadsheet1.ActiveSheet.Cells(3, 1).Value = "Sport-Utility"
Spreadsheet1.ActiveSheet.Cells(4, 1).Value = "Truck"
Spreadsheet1.ActiveSheet.Cells(5, 1).Value = "Minivan"

Spreadsheet1.ActiveSheet.Cells(1, 2).Value = "1998"
Spreadsheet1.ActiveSheet.Cells(2, 2).Value = 0.2
Spreadsheet1.ActiveSheet.Cells(3, 2).Value = 0.06
Spreadsheet1.ActiveSheet.Cells(4, 2).Value = 0.17
Spreadsheet1.ActiveSheet.Cells(5, 2).Value = 0.13

Spreadsheet1.ActiveSheet.Cells(1, 3).Value = "1999"
Spreadsheet1.ActiveSheet.Cells(2, 3).Value = 0.38
Spreadsheet1.ActiveSheet.Cells(3, 3).Value = 0.82
Spreadsheet1.ActiveSheet.Cells(4, 3).Value = 0.28
Spreadsheet1.ActiveSheet.Cells(5, 3).Value = 0.62

Spreadsheet1.ActiveSheet.Cells(1, 4).Value = "2000"
Spreadsheet1.ActiveSheet.Cells(2, 4).Value = 0.42
Spreadsheet1.ActiveSheet.Cells(3, 4).Value = 0.12
Spreadsheet1.ActiveSheet.Cells(4, 4).Value = 0.55
Spreadsheet1.ActiveSheet.Cells(5, 4).Value = 0.25

' Clear the contents of the chart workspace. This removes
' any old charts that may already exist and leaves the chart workspace
' completely empty. One chart object is then added.
ChartSpace1.Clear
ChartSpace1.Charts.Add
Set chConstants = ChartSpace1.Constants

' Set the chart DataSource property to the spreadsheet.
' It is possible to specify multiple data sources, but this example uses only one.
ChartSpace1.DataSource = Spreadsheet1

' Add three series to the chart.
ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SeriesCollection.Add

' Connect the chart to data by specifying spreadsheet cell references
' for the different data dimensions.
' Notice that the series name is also bound to a spreadsheet cell. Changing
' the contents of the cell "B1" will also change the name that appears in the legend.
' If you don't want this behavior, set SeriesCollection(0).Caption instead of
' using the SetData method to bind the series name to the spreadsheet.

' Series one contains sales growth data for 1998.
' Bind the series name, the category names, and the values.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "B1"
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimCategories, chConstants.chDataBound, "A2:A5"
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimValues, chConstants.chDataBound, "B2:B5"

' Series two contains sales growth data for 1999.
ChartSpace1.Charts(0).SeriesCollection(1).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "C1"
ChartSpace1.Charts(0).SeriesCollection(1).SetData chConstants.chDimCategories, chConstants.chDataBound, "A2:A5"
ChartSpace1.Charts(0).SeriesCollection(1).SetData chConstants.chDimValues, chConstants.chDataBound, "C2:C5"

' Series two contains sales growth data for 2000.
ChartSpace1.Charts(0).SeriesCollection(2).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "D1"
ChartSpace1.Charts(0).SeriesCollection(2).SetData chConstants.chDimCategories, chConstants.chDataBound, "A2:A5"
ChartSpace1.Charts(0).SeriesCollection(2).SetData chConstants.chDimValues, chConstants.chDataBound, "D2:D5"

' Make the chart legend visible, format the left value axis as percentage,
' and specify that value gridlines are at 10% intervals.
ChartSpace1.Charts(0).HasLegend = True
ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionLeft).NumberFormat = "0%"
ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionLeft).MajorUnit = 0.1
End Sub


voulant adapter ce code a mon programme j'ai insere:



Sub Window_OnLoad()

Dim chConstants
Dim i As Integer



Spreadsheet1.ActiveSheet.Cells.Clear
For i = 1 To 302

Spreadsheet1.Cells(i, 1) = S1.Cells(i + 2, 4)
Spreadsheet1.Cells(i, 2) = S1.Cells(i + 2, 5)
Spreadsheet1.Cells(i, 3) = S1.Cells(i + 2, 7)
Spreadsheet1.Cells(i, 4) = S1.Cells(i + 2, 9)
Next

' Clear the contents of the chart workspace. This removes
' any old charts that may already exist and leaves the chart workspace
' completely empty. One chart object is then added.
ChartSpace1.Clear
ChartSpace1.Charts.Add
Set chConstants = ChartSpace1.Constants

' Set the chart DataSource property to the spreadsheet.
' It is possible to specify multiple data sources, but this example uses only one.
ChartSpace1.DataSource = Spreadsheet1

' Add three series to the chart.
ChartSpace1.Charts(0).SeriesCollection.Add
'ChartSpace1.Charts(1).SeriesCollection.Add
'ChartSpace1.Charts(2).SeriesCollection.Add

' Connect the chart to data by specifying spreadsheet cell references
' for the different data dimensions.
' Notice that the series name is also bound to a spreadsheet cell. Changing
' the contents of the cell "B1" will also change the name that appears in the legend.
' If you don't want this behavior, set SeriesCollection(0).Caption instead of
' using the SetData method to bind the series name to the spreadsheet.

' Series one contains sales growth data for 1998.
' Bind the series name, the category names, and the values.


ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "A1:A300"
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "B1:B300"


' Make the chart legend visible, format the left value axis as percentage,
' and specify that value gridlines are at 10% intervals.
ChartSpace1.Charts(0).HasLegend = True
ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionLeft).NumberFormat = "0%"
ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionLeft).MajorUnit = 0.1


End Sub

Private Sub CommandButton7_Click()

Call Window_OnLoad

End Sub

mais ca ne marche pas

j'ai lu quelque part que si on entrait pas le type de graphe desire on obtient par defaut un diagramme. j'ai par suite tente de changer le type de graphe chose que je ne suis pas parvenu a faire.]

 

si vous savez quoi faire....

 

Merci beaucoup

Modifié par KewlCat
Utilisation de la balise code
Lien vers le commentaire
Partager sur d’autres sites

Rejoindre la conversation

Vous pouvez publier maintenant et vous inscrire plus tard. Si vous avez un compte, connectez-vous maintenant pour publier avec votre compte.
Remarque : votre message nécessitera l’approbation d’un modérateur avant de pouvoir être visible.

Invité
Répondre à ce sujet…

×   Collé en tant que texte enrichi.   Coller en tant que texte brut à la place

  Seulement 75 émoticônes maximum sont autorisées.

×   Votre lien a été automatiquement intégré.   Afficher plutôt comme un lien

×   Votre contenu précédent a été rétabli.   Vider l’éditeur

×   Vous ne pouvez pas directement coller des images. Envoyez-les depuis votre ordinateur ou insérez-les depuis une URL.

  • En ligne récemment   0 membre est en ligne

    • Aucun utilisateur enregistré regarde cette page.
×
×
  • Créer...