+ Reply to Thread
Results 1 to 4 of 4

Select active cell and return value

  1. #1
    Registered User
    Join Date
    05-10-2006
    Posts
    59

    Select active cell and return value

    Hi

    I would like the user to be able to select any cell they wish and then click on a button to active my macro.

    The problem I am having is getting the value of the selected cell e.g. D4

    All I can get is the value 4.

    I would then like to add this to a range so that I can use a loop to interigate the data.

    For example:

    If the user selected the cell D4 and the data went all the way down to D100 my range would be D4:D100

    Here is my code (I had help from a previous post to get this far!)

    Sub SearchString()

    'Sets the cells in range from top to bottom
    MyRange = "D4:D100"

    'Declares Count as an Integer
    Count = 0

    'Declares CountNull as an Integer
    CountNull = 0

    For Each myCell In Range(MyRange)

    'If two or more cells have a Null value in sequence then the loop ends
    'Amend the value to 'x' for greater Null cells
    If CountNull = 2 Then
    Exit For
    End If

    'Checks if cell has a Null value
    If myCell < 1 Then
    'Allows one cell to be Null
    CountNull = CountNull + 1
    'Skips a line
    GoTo NextLoop
    Else
    'Resets value so that the count is correct
    CountNull = 0
    End If

    'searches for the occurance of tlp or TLP in the string
    If InStr(myCell, "tlp") > 0 Or InStr(myCell, "TLP") > 0 Then
    myCell.Offset(0, -2) = "Yes"
    'Adds one to the count
    Count = Count + 1

    Else
    'if neither is found then No is returned
    myCell.Offset(0, -2) = "No"

    End If

    'searches for the occurance of tnt or TNT in the string
    If InStr(myCell, "tnt") > 0 Or InStr(myCell, "TNT") > 0 Then
    myCell.Offset(0, -3) = "Yes"
    'Adds one to the count
    Count = Count + 1
    Else
    'if neither is found then No is returned
    myCell.Offset(0, -3) = "No"
    End If

    'Checks the value of Count, IF 2 then both strings have been found

    If Count = 2 Then
    'Count is two so BOTH strings have been found
    myCell.Offset(0, -1) = "Yes"
    Else
    'Value of Count is either zero or one
    myCell.Offset(0, -1) = "No"
    End If

    'Re-sets the value of count ready for Next Loop
    Count = 0

    NextLoop:
    Next myCell

    End Sub

    I hope I have explained this clearly enough!

    Just being able to work out the actual cell value would be great e.g. D4

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    hi,

    is this what you want?

    rng = ActiveCell.Address

    dave

  3. #3
    Registered User
    Join Date
    05-10-2006
    Posts
    59
    That's it !!!!

    I was using a variable as an integer and not a string.

    .Address works a treat.

    Thank you sweep !!

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444
    you're welcome - glad it works

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1