Consulting

Results 1 to 5 of 5

Thread: Selection

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

    Selection

    I have a subroutine that utilizes the following simple:

    [VBA]
    selection.value = Date
    [/VBA]

    This statement is not based on a pre-defined range but relies on the cells selected by the user. It is triggered by clicking on a command button. In other words, the user highlights the cell or cells in which the current date is to be inserted, then clicks the command button and the date is inserted.

    I've run into a problem, however, when only one cell is selected, i.e., the ActiveCell. When I click on the command button the entire spreadsheet is filled with the current date. Is there some way to restrict this to only the active cell when only the active cell is selected?

  2. #2
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Here is the full date script, in case it is needed to give context to the question.

    [VBA]
    Sub insertDate()

    Dim sRng As Range
    Dim wRng As Range
    On Error Resume Next
    Set sRng = Range("SaleDate").Offset(1, 0).Resize(gRows - hRow).SpecialCells(xlCellTypeVisible)
    Set wRng = Range("WormDate").Offset(1, 0).Resize(gRows - hRow).SpecialCells(xlCellTypeVisible)

    With Sheets("(History)")
    ''''' Do ONLY if ActiveCell is in the WormDate or SaleDate Range '''''
    If Not Intersect(ActiveCell, wRng) Is Nothing Or _
    Not Intersect(ActiveCell, sRng) Is Nothing And _
    .FilterMode = True Then
    If ActiveCell.Value = "" Then
    Selection.SpecialCells(xlCellTypeBlanks).Value = Date
    Else: Selection.Value = ""
    End If

    ''''' For all other DATE cells '''''
    ElseIf Cells(hRow, ActiveCell.Column).Value <> "KidDueDate" And _
    Right(Cells(hRow, ActiveCell.Column), 4) = "Date" Then
    If ActiveCell.Value = "" Then
    ActiveCell.Value = Date
    Else: ActiveCell.Value = ""
    End If

    End If

    End With

    End Sub
    [/VBA]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]If Selection.Count > 1 Then
    Selection.Value = Date
    Else
    ActiveCell.Value = Date
    End If[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Opv
    I've run into a problem, however, when only one cell is selected, i.e., the ActiveCell. When I click on the command button the entire spreadsheet is filled with the current date. Is there some way to restrict this to only the active cell when only the active cell is selected?
    That shouldn't happen, and doesn't in a test I just tried.
    ____________________________________________
    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
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks. The only thing I can figure that was going on is that some of the predefined ranges rely on the UsedRange. My UsedRange area kept getting distorted. I set up a function to keep the correct number of data rows and columns in tact. Whether that was the source of the problem, I don't know but either that or some other inadvertent change resolved the problem.

Posting Permissions

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