Consulting

Results 1 to 6 of 6

Thread: Solved: Instructions Following a Called Script

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Instructions Following a Called Script

    [VBA]
    'CONVERT FORMULAS TO VALUES
    Sub convFormulasToValues()

    Dim lastRecRow As Integer
    Dim formulaRng As Range

    With Sheets("Checking")

    On Error GoTo closeIt

    lastRecRow = .Range("Rec").End(xlDown).Row
    Set formulaRng = .Range("K4:K" & lastRecRow).SpecialCells(xlCellTypeFormulas)

    If Right(lastRecRow, 2) = "00" And formulaRng.Count > 0 Then
    Application.ScreenUpdating = False
    formulaRng.Copy
    formulaRng.PasteSpecial (xlPasteValues)
    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    Else:
    GoTo closeIt
    End If

    End With

    closeIt:

    End Sub
    [/VBA]

    This snipped is called from another script and is doing what it is suppose to do. It is called BEFORE a simple instruction to "Select" a designated cell at the bottom of the worksheet. It works fine so long as the prescribed conditions are not met. However, if the conditions are met, something in the above script is causing the formulaRng to remain selected so that the subsequent Select instruction is apparently being ignored. Does anything jump out as being the possible culprit?

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I avoid Select when I can.

    If you want to select some other range after a paste when that sheet is not the active sheet, you need to activate it and select another cell as the selection for that sheet.

    e.g.

    [VBA]Sub Test()
    Dim r As Range, rr As Range
    Set r = Worksheets(1).Range("A11:A12")

    r.Worksheet.Activate
    r.Select

    With Worksheets(2)
    .Unprotect "ken"
    .Range("A2:A10").Locked = False
    .Protect "ken"
    End With

    With Worksheets(2).Range("A2:A10")
    .Worksheet.Activate
    .Formula = "=Row()"
    .Copy
    .PasteSpecial xlPasteValues
    Cells(.Row, .Column).Select 'Select only the first cell of range.
    End With

    r.Worksheet.Activate
    Application.CutCopyMode = False
    Cells(r.Row, r.Column).Select 'Select only the first cell of range r.
    End Sub[/VBA]

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    I thought I had avoided Select in the script posted. The Select statement referenced in my explanation is in another script, not another worksheet, so the worksheet in question is already active. That Select statement is presumed to be needed, as it tells Excel where to scroll to and leave the cursor when I click to save the worksheet.

  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Well, I somehow corrected my own problem. Thanks anyway.

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This is another way to change formulas to their values, that leaves the Clipboard un changed.

    With theRange
        .Value = .Value
    End With

  6. #6
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by mikerickson
    This is another way to change formulas to their values, that leaves the Clipboard un changed.

    With theRange
        .Value = .Value
    End With
    Very cool. Thanks.

Posting Permissions

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