Bonjour,
J'ai créé il y a plusieurs années, un script VBA que je souhaiterais passer sous PYTHON pour des soucis d'ergonomie et de praticité.
Je débute en Python et je bloque sur une partie.
Le but du code est d'homogénéiser des valeurs dans deux colonnes.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 Option Explicit Sub Copie() Dim i As Integer, j As Integer, k As Integer, m As Integer, Feuille_X As String, Feuille_Y As String, Ligne_vide As Integer Dim Ligne_du_haut As Integer, Ligne_du_bas As Integer, Nombre_de_lignes As Integer, Nombre_de_colonnes As Byte Application.ScreenUpdating = False With ActiveSheet Range("A2:Z" & Rows.Count).ClearContents Range("A2:Z" & Rows.Count).Interior.Pattern = xlNone If ActiveSheet.Name = "Feuil3" Then Feuille_X = "Feuil1" Feuille_Y = "Feuil2" Else Feuille_X = "Feuil2" Feuille_Y = "Feuil1" End If Nombre_de_colonnes = Cells.Find("*", , , , xlByColumns, xlPrevious).Column Sheets(Feuille_X).Range("A2:Z" & Sheets(Feuille_X).Range("A" & Rows.Count).End(xlUp).Row).Copy .Range("A2") Ligne_vide = .Range("A" & Rows.Count).End(xlUp).Row + 1 Sheets(Feuille_Y).Range("A2:a" & Sheets(Feuille_Y).Range("A" & Rows.Count).End(xlUp).Row).Copy .Range("A" & Ligne_vide) With .Range(.Cells(Ligne_vide, 1), .Cells(.Range("A" & Rows.Count).End(xlUp).Row, Nombre_de_colonnes)).Interior .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.8 End With For i = .Range("A" & Rows.Count).End(xlUp).Row To Ligne_vide Step -1 On Error Resume Next j = Application.WorksheetFunction.Match(.Range("A" & i), .Range("A2:A" & Ligne_vide - 1), 0) If j > 0 Then .Rows(i).Delete j = 0 Next i .Range("A1:Z" & Rows.Count).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes .Range("B2").Activate Retour: Do Until ActiveCell.Offset(1, 0) = "" ActiveCell.Offset(1, 0).Activate Loop Ligne_du_haut = ActiveCell.Row ActiveCell.Offset(1, 0).Activate Do Until ActiveCell.Offset <> "" If ActiveCell.Offset(1, -1) = "" Then Exit Sub ActiveCell.Offset(1, 0).Activate Loop Ligne_du_bas = ActiveCell.Row For k = 2 To Nombre_de_colonnes For m = Ligne_du_haut + 1 To Ligne_du_bas - 1 Cells(m, k) = Round(Cells(Ligne_du_haut, k) + ((Cells(Ligne_du_bas, k) - Cells(Ligne_du_haut, k)) / (Cells(Ligne_du_bas, 1) - Cells(Ligne_du_haut, 1))) * (Cells(m, 1) - Cells(Ligne_du_haut, 1)), 3) Next m Next k Range("B" & Ligne_du_bas).Activate GoTo Retour End With ' ActiveSheet End Sub
Code Python :
J'ai voulu reprendre l'idées des 'ActiveSheet' ou encore 'ActiveCell' que j'utilisais sous VBA, mais je ne suis pas certain que ce soit la bonne méthode…Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 import pandas as pd from pandas import DataFrame, Series import matplotlib.pyplot as plt import numpy as np from vbclasses import * from vbconstants import * import math from vb2py.vbfunctions import * from vb2py.vbdebug import * import csv c = csv.writer(open("COPIE.csv", "wb")) def homogeneisation(): i = int() j = int() k = int() m = int() Feuille_X = str() Feuille_Y = str() Ligne_vide = str() Ligne_du_haut = str() Ligne_du_bas = str() Nombre_de_lignes = str() Nombre_de_colonnes = bytes() with_variable0 = ActiveSheet Range('A2:Z' + Rows.Count).ClearContents() Range['A2:Z' + Rows.Count].Interior.Pattern = xlNone if ActiveSheet.Name == 'Feuil3': Feuille_X = 'Feuil1' Feuille_Y = 'Feuil2' else: Feuille_X = 'Feuil2' Feuille_Y = 'Feuil1' Nombre_de_colonnes = Cells.Find('*', VBGetMissingArgument(Cells.Find.Column, 1), VBGetMissingArgument(Cells.Find.Column, 2), VBGetMissingArgument(Cells.Find.Column, 3), xlByColumns, xlPrevious).Column Sheets(Feuille_X).Range('A2:Z' + Sheets(Feuille_X).Range('A' + Rows.Count).End(xlUp).Row).Copy(with_variable0.Range('A2')) Ligne_vide = with_variable0.Range('A' + Rows.Count).End(xlUp).Row + 1 Sheets(Feuille_Y).Range('A2:a' + Sheets(Feuille_Y).Range('A' + Rows.Count).End(xlUp).Row).Copy(with_variable0.Range('A' + Ligne_vide)) with_variable1 = with_variable0.Range(with_variable0.Cells(Ligne_vide, 1), with_variable0.Cells(with_variable0.Range('A' + Rows.Count).End(xlUp).Row, Nombre_de_colonnes)).Interior with_variable1.ThemeColor = xlThemeColorAccent2 with_variable1.TintAndShade = 0.8 for i in vbForRange(with_variable0.Range('A' + Rows.Count).End(xlUp).Row, Ligne_vide, - 1): # VB2PY (UntranslatedCode) On Error Resume Next j = Application.WorksheetFunction.Match(with_variable0.Range('A' + i), with_variable0.Range('A2:A' + Ligne_vide - 1), 0) if j > 0: with_variable0.Rows(i).Delete() j = 0 with_variable0.Range('A1:Z' + Rows.Count).Sort(Key1=Range('A1'), Order1=xlAscending, Header=xlYes) with_variable0.Range('B2').Activate() while not (ActiveCell.Offset(1, 0) == ''): ActiveCell.Offset(1, 0).Activate() Ligne_du_haut = ActiveCell.Row ActiveCell.Offset(1, 0).Activate() while not (ActiveCell.Offset != ''): if ActiveCell.Offset(1, - 1) == '': return ActiveCell.Offset(1, 0).Activate() Ligne_du_bas = ActiveCell.Row for k in vbForRange(2, Nombre_de_colonnes): for m in vbForRange(Ligne_du_haut + 1, Ligne_du_bas - 1): Cells[m, k] = Round(Cells(Ligne_du_haut, k) + ( ( Cells(Ligne_du_bas, k) - Cells(Ligne_du_haut, k) ) / ( Cells(Ligne_du_bas, 1) - Cells(Ligne_du_haut, 1) ) ) * ( Cells(m, 1) - Cells(Ligne_du_haut, 1) ), 3) Range('B' + Ligne_du_bas).Activate() GoTo(Retour) # VB2PY (UntranslatedCode) Option Explicit
Est-ce que vous auriez des pistes ou des idées pour me guider ?
Merci d'avance.