Migration macro VBA en Google Sheet
J'ai une macro en vba Excel qui fonctionne très bien et je souhaiterais faire une migration vers google Sheets afin quelle fonctionne correctement
ci dessous la macro vba Excel
Option Explicit
--------------------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
If Target.Count > 1 Then Exit Sub
If Not Application.Intersect(Target, Range("N:N")) Is Nothing Then
If Not WorksheetFunction.IsNumber(Target.Offset(-2, -13)) Then
MsgBox "Tu n'as pas inscrit 'RET' dans une cellule grise !"
Exit Sub
Else
If Target <> "RET" Then
MsgBox "Le texte à taper ici afin que le code fonctionne est 'RET' "
Exit Sub
End If
End If
Application.ScreenUpdating = False
Workbooks.Open Filename:=ThisWorkbook.Path & "\RETRAIT.xlsm"
ThisWorkbook.Activate
With Workbooks("RETRAIT.xlsm")
On Error Resume Next
i = Application.WorksheetFunction.Match(Target.Offset(-2, -13), .Sheets("RETRAIT").Range("A:A"), 0)
If i = 0 Then
MsgBox "Le numéro correspondant à ce libellé n'a pas été trouvé dans le fichier 'RETRAIT'"
Workbooks("RETRAIT.xlsm").Close
Exit Sub
Else
Range("A" & Target.Row - 3 & ":X" & Target.Row).Copy .Sheets("RETRAIT").Range("A" & i - 1)
Range("B" & Target.Row - 3 & ":X" & Target.Row).ClearContents
Range("W" & Target.Row - 3) = " Rbst acpt "
Range("X" & Target.Row - 3) = " Acpt "
Range("W" & Target.Row) = " Net "
Range("B" & Target.Row) = "Date ouv : "
End If
.Save
.Close
End With
End If
End SubMerci de votre aide d'avance.
Bonjour,
N'hésitez pas à utiliser les outils IA (chatGPT, Gemini, copilot…) pour ce genre de "traduction", ils se débrouillent souvent très bien ! En plus votre macro est assez simple donc ce devrait etre très "direct" comme traduction.
Si ça ne marche pas vous pouvez poster ici le code fourni, en général les corrections à effectuer sont rapides.
Bonne journée.
Bonjour,
Avant de faire le poste dans le forum, j'ai fait les recherches, mais la traduction avec les outils IA , ne fonctionne pas correctement.
Voilà pourquoi j'ai fait le poste.
Merci .
Bonjour,
Personnellement j'obtiens le code ci-dessous, a priori fonctionnel. Cependant, il faut adapter votre macro sur certains points essentiels : la couleur de fond de cellule exacte, en format HEX, ainsi que (c'est le point de différence de code majeur) le classeur lié :
en VBA vous référez au classeur via son nom (donc déjà ouvert j'imagine), en AppScript vous devez utiliser l'ID du classeur pour y référer.
Le mode de fonctionnement est indiqué dans la doc ici :
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app?hl=fr#openbyidid
Si vous n'avez pas fait ces ajustements, la macro ne pourra pas fonctionner.
j'ai indiqué dans le code ci dessous en commentaires (après //) en MAJUSCULES en français, où doivent avoir lieu ces modifications.
function onEdit(e) {
// Get the edited range
const range = e.range;
// Check if a single cell is edited and it's in column N
if (range.getNumColumns() !== 1 || range.getColumn() !== 14) return;
// Check if a value is entered and it's not "RET"
if (!range.getValue() || range.getValue() !== "RET") {
SpreadsheetApp.getUi().alert('Please enter "RET" in the cell to proceed.');
return;
}
const sheet = e.range.getSheet();
const targetRow = range.getRow();
// Check if "RET" is entered in a gray cell (assuming conditional formatting)
const isGrayCell = sheet.getRange(targetRow - 2, 1).getBackground() === '#cccccc'; // COULEUR A AJUSTER/CORRIGER
if (!isGrayCell) {
SpreadsheetApp.getUi().alert("You haven't entered 'RET' in a gray cell!");
return;
}
const targetValue = sheet.getRange(targetRow - 2, 13).getValue(); // Get value from -13 column offset
// Check if targetValue is a number, indicating a valid reference
if (!isNaN(targetValue)) {
const sourceSheet = SpreadsheetApp.openById('YOUR_RETRAIT_SPREADSHEET_ID').getSheetByName('RETRAIT'); // REMPLACER ICI L'ID DU CLASSEUR
const matchIndex = sourceSheet.getRange('A:A').find(targetValue);
if (!matchIndex) {
SpreadsheetApp.getUi().alert("The corresponding number wasn't found in the 'RETRAIT' sheet.");
return;
}
const sourceRange = sourceSheet.getRange(matchIndex.getRow() - 1, 1, 10, targetRow - targetRow + 3); // Copy relevant columns (adjust as needed)
const destinationRange = sheet.getRange(targetRow - 3, 1, 10, targetRow - targetRow + 3); // Paste to destination
sourceRange.copyTo(destinationRange);
// Clear specific cells
sheet.getRange(targetRow - 3, 2, 1, targetRow - targetRow + 3).clearContent();
// Set specific cell values
sheet.getRange(targetRow - 3, 22).setValue(' Rbst acpt ');
sheet.getRange(targetRow - 3, 24).setValue(' Acpt ');
sheet.getRange(targetRow, 2).setValue(' Net ');
sheet.getRange(targetRow, 1).setValue('Date ouv : '); // Adjust formatting if needed
} else {
SpreadsheetApp.getUi().alert("The value in the cell (-13 column offset) is not a number.");
}
}