PDA

View Full Version : VBA cannot activate cell after first loop



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

Bob Phillips
09-21-2009, 04:47 AM
It might be because anotehr sheet is active.

Without re-cutting all the code, furst try



Sheets("Persium").Acivate
Range("J" & celle_nummer).Activate

UMUM
09-21-2009, 04:53 AM
Thx it worked!
- do you know why the original coding didnt work? - should be better/similar coding?

Bob Phillips
09-21-2009, 05:03 AM
I didn't look too closely, but a cursory look suggested that you were activating sheets in the code, and so when you ran it a second time, some other sheet was active. This code



Sheets("Persium").Range("J" & celle_nummer).Activate


does not activate that cell if that sheet is not active, which may be contrary to what you thought. That is why I broke it down into separate statements, activate the sheet, then activate the cell.

I would write the code to avoid selecting the sheets, thus avoiding the problem.

Bob Phillips
09-21-2009, 05:11 AM
Here is a quick attempt to recut your code as I suggest.

I cannot verify its accuracy, I don't know what you are doing, or have data to test it with, but you can look see



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
Filled_cells = Worksheets("Persium").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:"
With Worksheets("Persium").Range("J" & celle_nummer)

Cpr = Left(.Value, 6) & "-" & Right(.Value, 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
End With

With Worksheets("ØSLDV")

Set soegning = .Cells.Find(What:=Cpr, _
After:=.Range("A1"), _
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...

Set soegning = .Cells.FindNext(soegning)
soegning.Offset(0, 1).Copy Destination:=Worksheets("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
With Sheets("Lønsammensætning")

.Range("E" & celle_nummer).Value = _
.Range("E" & celle_nummer).Value / _
Worksheets("Faktor").Range("B1").Value * 12
End With
End With
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