Consulting

Results 1 to 5 of 5

Thread: VBA cannot activate cell after first loop

  1. #1
    VBAX Newbie
    Joined
    Sep 2009
    Posts
    2
    Location

    VBA cannot activate cell after first loop

    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:

    [vba]Sheets("Persium").Range("J" & celle_nummer).Activate[/vba]

    Along with a 1004 runtime error message - does anyone know how to solve this?

    The rest of the coding looks as follows:

    [vba]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
    [/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It might be because anotehr sheet is active.

    Without re-cutting all the code, furst try

    [vba]

    Sheets("Persium").Acivate
    Range("J" & celle_nummer).Activate
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Sep 2009
    Posts
    2
    Location
    Thx it worked!
    - do you know why the original coding didnt work? - should be better/similar coding?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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

    [vba]

    Sheets("Persium").Range("J" & celle_nummer).Activate
    [/vba]

    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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

    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •