UMUM
09-21-2009, 04:40 AM
I am having trouble running a loop the second time.
It goes through the first time, but the second time I get a runtime error on the following coding:
Sheets("Persium").Range("J" & celle_nummer).Activate
Along with a 1004 runtime error message - does anyone know how to solve this?
The rest of the coding looks as follows:
Sub Find_løndata_pers()
'
' Find_løndata_pers Makro
'
'
Dim Cpr As String
Dim Filled_cells As Integer
Dim i As Integer
Dim celle_nummer As Integer
Dim soegning As Range
' Dim definerer hvilken slags variabel
Application.ScreenUpdating = False
' Gør makroen hurtigere. "False" screenupdating skal ikke foretages
Sheets("Persium").Activate
Filled_cells = ActiveSheet.UsedRange.Rows.Count - 3
' Vi definerer Filled_cells som værende i det aktive sheet "Persium"
' i den brugte range (antal rækker som er udfyldt) minus de 3 første
' rækker
celle_nummer = 4
' start cellen er 4
i = 0
Do
' "gør dette:"
Sheets("Persium").Range("J" & celle_nummer).Activate
Cpr = Left(ActiveCell, 6) & "-" & Right(ActiveCell, 4)
' Vi definerer Cpr som værende den sidst brugte ".Activate" der tælles 6 pladser fra venstre
' og tilføjer - og4 pladser fra højre
Sheets("ØSLDV").Activate
Set soegning = Cells.Find(What:=Cpr, After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False)
' Set soegning betyder, at søgningen skal afprøves, og droppes hvis der ikke findes noget
If Not soegning Is Nothing Then ' Hvis søgningen ikke fejler, gør sådan...
Cells.Find(What:=Cpr, After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Copy Destination:=Sheets("Lønsammensætning").Range("E" & celle_nummer)
' Active.offset vi kan ændre hvilken celle
' der skal aktiveres (o rækker, 1 kolonne imod højre) til venstre er -1
Sheets("Lønsammensætning").Activate
Sheets("Lønsammensætning").Range("E" & celle_nummer).Value = _
Sheets("Lønsammensætning").Range("E" & celle_nummer).Value / _
Sheets("Faktor").Range("B1").Value * 12
End If ' slut på ovenstående betingelse
celle_nummer = celle_nummer + 1
' vi lægger 1 til i løkken (efter do)
i = i + 1
' i er tæller for hvor mange gange vi har kørt løkken
Loop Until i = 30 'Filled_cells
' løkken fortsætter indtil der ikke er flere fyldte rækker
Application.ScreenUpdating = True
End Sub
It goes through the first time, but the second time I get a runtime error on the following coding:
Sheets("Persium").Range("J" & celle_nummer).Activate
Along with a 1004 runtime error message - does anyone know how to solve this?
The rest of the coding looks as follows:
Sub Find_løndata_pers()
'
' Find_løndata_pers Makro
'
'
Dim Cpr As String
Dim Filled_cells As Integer
Dim i As Integer
Dim celle_nummer As Integer
Dim soegning As Range
' Dim definerer hvilken slags variabel
Application.ScreenUpdating = False
' Gør makroen hurtigere. "False" screenupdating skal ikke foretages
Sheets("Persium").Activate
Filled_cells = ActiveSheet.UsedRange.Rows.Count - 3
' Vi definerer Filled_cells som værende i det aktive sheet "Persium"
' i den brugte range (antal rækker som er udfyldt) minus de 3 første
' rækker
celle_nummer = 4
' start cellen er 4
i = 0
Do
' "gør dette:"
Sheets("Persium").Range("J" & celle_nummer).Activate
Cpr = Left(ActiveCell, 6) & "-" & Right(ActiveCell, 4)
' Vi definerer Cpr som værende den sidst brugte ".Activate" der tælles 6 pladser fra venstre
' og tilføjer - og4 pladser fra højre
Sheets("ØSLDV").Activate
Set soegning = Cells.Find(What:=Cpr, After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False)
' Set soegning betyder, at søgningen skal afprøves, og droppes hvis der ikke findes noget
If Not soegning Is Nothing Then ' Hvis søgningen ikke fejler, gør sådan...
Cells.Find(What:=Cpr, After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Copy Destination:=Sheets("Lønsammensætning").Range("E" & celle_nummer)
' Active.offset vi kan ændre hvilken celle
' der skal aktiveres (o rækker, 1 kolonne imod højre) til venstre er -1
Sheets("Lønsammensætning").Activate
Sheets("Lønsammensætning").Range("E" & celle_nummer).Value = _
Sheets("Lønsammensætning").Range("E" & celle_nummer).Value / _
Sheets("Faktor").Range("B1").Value * 12
End If ' slut på ovenstående betingelse
celle_nummer = celle_nummer + 1
' vi lægger 1 til i løkken (efter do)
i = i + 1
' i er tæller for hvor mange gange vi har kørt løkken
Loop Until i = 30 'Filled_cells
' løkken fortsætter indtil der ikke er flere fyldte rækker
Application.ScreenUpdating = True
End Sub