ItIsHardToProgram
Technical User
Ok, I really wanted to do this by myself, and strangely I got it to work just fine, but as soon as I put the macro in a button, it has an error, I know exactly wich line it errors on, I have no clue why though...
What my macro does is it goes in a sheet that is ready for upload, it tests if it finds a property and an account (row / column).
Once this is done, it goes catch the data in the according row / column.
It then creates a sheet, if a matching sheet for the specific data sheet does not exist, and adds the data in the newly created sheet.
This works fine, although you will argue my error handling.
Once 10 000 lines have been filtered, it goes to the new sheet, sorts the data, and loops through the next data sheet and so on....
Weirdly, when I have my macro in a Module, it works fine, every behavior has I want it. But when I put my macro in a sheet, and call my macro through a button, it errors on my Cells.select... runtime error saying it failed to select the cells...
Here is my code:
"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.
What my macro does is it goes in a sheet that is ready for upload, it tests if it finds a property and an account (row / column).
Once this is done, it goes catch the data in the according row / column.
It then creates a sheet, if a matching sheet for the specific data sheet does not exist, and adds the data in the newly created sheet.
This works fine, although you will argue my error handling.
Once 10 000 lines have been filtered, it goes to the new sheet, sorts the data, and loops through the next data sheet and so on....
Weirdly, when I have my macro in a Module, it works fine, every behavior has I want it. But when I put my macro in a sheet, and call my macro through a button, it errors on my Cells.select... runtime error saying it failed to select the cells...
Here is my code:
Code:
Sub StartUploadMacro()
'
' StartUpload Macro
' Enregistré par Julien-Bono Roy le 29 décembre 2008
Dim CurrDate As String, PostMonth As String, Property As String, GLnumber As String
Dim Amount As Double
Dim iSheet As Integer, SheetTest As Integer
Dim iSheetName As String, SplitSheetName, uploadSheetName As String
Dim i As Integer, j As Integer, iTest As Integer
Dim StartUp As Boolean, SheetExist As Boolean
Dim testSheetName As String
Dim bidon
Dim iLineOffset As Integer
Dim sSplitPMname
On Error GoTo ErrorHandler
PostMonth = Worksheets(1).Cells(1, 6)
CurrDate = Worksheets(1).Cells(2, 6)
iLineOffset = 1
sSplitPMname = Split(PostMonth, "/")
For iSheet = 1 To Worksheets.Count
'Test si la feuille doit avoir un fichier d'upload ou non.
iSheetName = Worksheets(iSheet).Name
SplitSheetName = Split(iSheetName, "_")
StartUp = False
Select Case SplitSheetName(0)
Case "Rdy"
For iTest = 1 To 20
testSheetName = UCase(Worksheets(iSheet).Cells(iTest, 1).FormulaR1C1)
'Vérifie si une ligne d'upload est présente
If testSheetName = "START-UPLOADMACRO" Then
StartUp = True
Exit For
End If
Next iTest
If StartUp = True Then
For i = iTest + 1 To 10000
'Identifie une ligne associé à une propriété
Property = Worksheets(iSheet).Cells(i, 1).FormulaR1C1
'test la validité (très simple, pas très flexible) de ce qui est rentrée)
Property = UCase(Property)
If Property = "MODIFGL" Then
iTest = i
End If
If Len(Property) = 5 And IsNumeric(Property) Then
For j = 1 To 200
'Identifie une colone associé à un compte
GLnumber = Worksheets(iSheet).Cells(iTest, j).FormulaR1C1
'test la validité (très simple, pas très flexible) de ce qui est rentrée)
If Len(GLnumber) = 5 And IsNumeric(GLnumber) Then
'Prend le montant dans la ligne et colone identifiée
Amount = Worksheets(iSheet).Cells(i, j).Value
'vérification si la feuille d'upload existe
For SheetTest = 1 To Worksheets.Count
If Worksheets(SheetTest).Name = ("Up " + SplitSheetName(1) + " " + sSplitPMname(0) + "-" + sSplitPMname(1)) Then
SheetExist = True
End If
Next SheetTest
'Si la feuille existe, annule le test, sinon crée une nouvelle feuille nom selon nom de feuille
If SheetExist = True Then
Else
Worksheets(Worksheets.Count).Select
Sheets.Add
NameLenghtError:
Worksheets(Worksheets.Count - 1).Name = "Up " + SplitSheetName(1) + " " + sSplitPMname(0) + "-" + sSplitPMname(1)
End If
uploadSheetName = "Up " + SplitSheetName(1) + " " + sSplitPMname(0) + "-" + sSplitPMname(1)
With Worksheets(uploadSheetName)
.Cells(iLineOffset, 1).FormulaR1C1 = "J"
.Cells(iLineOffset, 5).FormulaR1C1 = CurrDate
.Cells(iLineOffset, 6).FormulaR1C1 = "'" & PostMonth
.Cells(iLineOffset, 9).FormulaR1C1 = Property
.Cells(iLineOffset, 10).FormulaR1C1 = Amount
.Cells(iLineOffset, 11).FormulaR1C1 = GLnumber
.Cells(iLineOffset, 14).FormulaR1C1 = 1
.Cells(iLineOffset, 15).FormulaR1C1 = SplitSheetName(1) + " " + sSplitPMname(0) + "-" + sSplitPMname(1)
End With
iLineOffset = iLineOffset + 1
End If
Next j
End If
Next i
End If
End Select
If StartUp = True Then
Sheets("Up " + SplitSheetName(1) + " " + sSplitPMname(0) + "-" + sSplitPMname(1)).Activate
Sheets("Up " + SplitSheetName(1) + " " + sSplitPMname(0) + "-" + sSplitPMname(1)).Select
[highlight]Cells.Select[/highlight]
Selection.Sort Key1:=Range("I1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End If
iLineOffset = 1
SheetExist = False
Next iSheet
Exit Sub
ErrorHandler:
If Err.Number = 1004 And SheetExist = False Then
bidon = MsgBox("Le nom de feuille qui est dénoté par Rdy_ est trop grand pour créer une nouvelle feuille d'upload. Le nom de feuille sera donc tronqué", vbYesNo, "Nom de feuille trop grand")
If bidon = vbYes Then Exit Sub
SplitSheetName(1) = Left(SplitSheetName(1), 7)
SheetExist = True
Resume NameLenghtError
End If
MsgBox Error(Err)
MsgBox Err.Number
End Sub
"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.