Solved

Select current range of cells not whole row - Excel VBA

Posted on 2011-03-24
7
716 Views
Last Modified: 2012-05-11
Hi Again
I am so sorry for annoying, but I need to get this up today.  I got Syd's fix working and have moved to another part of the spreadsheet (bearing in mind I didn't design this).  The range to look in starts at bf7 and across to ck.

I can get this to work, but don't want it to select the whole active row this time, just a range of cells.  So the line: workstn.Rows(c.Row).Select  I don't want to be the whole row.  I want it to start at column bf and move across to ck.

I really hope someone can help and please excuse my ignorance.  This really isn't my area.

Cheers and thanks
Private Sub cmbFind_Click()
    Dim strFind As String    'what to find
    Dim FirstAddress As String
    Dim rSearch As Range  'range to search
    Set rSearch = workstn.Range("bf7", Range("bf65536").End(xlUp))
   Dim f      As Integer
    strFind = Me.unmtxt.Value    'what to look for
     With rSearch
        Set c = .Find(strFind, LookIn:=xlValues)
        If Not c Is Nothing Then    'found it
        workstn.Rows(c.Row).Select
            With Me    'load entry to form
                .proftxt.Value = c.Offset(0, 1).Value
                .typetxt.Value = c.Offset(0, 2).Value
                .descritxt.Value = c.Offset(0, 3).Value
                .detailtxt.Value = c.Offset(0, 4).Value
                .versiontxt.Value = c.Offset(0, 5).Value
                .locationtxt.Value = c.Offset(0, 6).Value
                .cmbAmend.Enabled = True     'allow amendment or
                .cmbDelete.Enabled = True    'allow record deletion
            f = 0
            End With
            FirstAddress = c.Address
            Do
                f = f + 1    'count number of matching records
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAddress
            If f > 1 Then
                Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")

                    Case vbOK
                        FindAll
                    Case vbCancel
                        'do nothing
                End Select
                Me.Height = frmMax

            End If
                   
        Else: MsgBox strFind & " doesn't seem to be on the list. You may have to use a different name"    'search failed
        End If
    End With
    If workstn.AutoFilterMode Then workstn.Range("Bf9").AutoFilter
   End Sub

Open in new window

0
Comment
Question by:mjbdbase
  • 3
  • 3
7 Comments
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
Try this

rRow = workstn.Rows(c.Row)
range((Range("BF"&rRow),Range("CK"&rRow)).select

Cheers,

Dave
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
or if you want to start in row 7, then go to the selected row of CK, then

rRow = workstn.Rows(c.Row)
range(Range("BF7",range("CK"&rRow)).select

Dave
0
 

Author Comment

by:mjbdbase
Comment Utility
Ummm is there the bracket missing?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Accepted Solution

by:
mjbdbase earned 0 total points
Comment Utility
Hope you don't mind, but I ended up "nutting" it myself.  Have posted the code below. It may be the long way of doing it, but should be ok for our needs.
In case anyone wanted clarification, the person who designed the spreadsheet put all of the info on one sheet.  I will change this one day but am time strapped with other work.  
The reason I wanted to select from the active cell which had been found, across to row CK was that I want the user to be able to delete rows and shift cells up without deleting WHOLE rows.

 c.Select
        Range(ActiveCell, ActiveCell.Offset(0, 31)).select

Thanks for the help though Dave. As always was prompt.

Cheers
Private Sub cmbFind_Click()
    Dim strFind As String    'what to find
    Dim FirstAddress As String
    Dim rSearch As Range  'range to search
    Set rSearch = workstn.Range("bf7", Range("bf65536").End(xlUp))
   Dim f      As Integer
    strFind = Me.unmtxt.Value    'what to look for
     With rSearch
        Set c = .Find(strFind, LookIn:=xlValues)
        If Not c Is Nothing Then    'found it
        c.Select
        Range(ActiveCell, ActiveCell.Offset(0, 31)).select
              With Me    'load entry to form
                .proftxt.Value = c.Offset(0, 1).Value
                .typetxt.Value = c.Offset(0, 2).Value
                .descritxt.Value = c.Offset(0, 3).Value
                .detailtxt.Value = c.Offset(0, 4).Value
                .versiontxt.Value = c.Offset(0, 5).Value
                .locationtxt.Value = c.Offset(0, 6).Value
                .cmbAmend.Enabled = True     'allow amendment or
                .cmbDelete.Enabled = True    'allow record deletion
            f = 0
            End With
            FirstAddress = c.Address
            Do
                f = f + 1    'count number of matching records
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAddress
            If f > 1 Then
                Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")

                    Case vbOK
                        FindAll
                    Case vbCancel
                        'do nothing
                End Select
                Me.Height = frmMax

            End If
                   
        Else: MsgBox strFind & " doesn't seem to be on the list. You may have to use a different name"    'search failed
        End If
    End With
    If workstn.AutoFilterMode Then workstn.Range("Bf9").AutoFilter
   End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Sorry. Just woke up. :)

What you are doing is the same as what dave suggested in post 1 :)

range((Range("BF"&rRow),Range("CK"&rRow)).select

and this

Range(ActiveCell, ActiveCell.Offset(0, 31)).select

are the same :)

Sid
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
Thanks Sid.  mjbdbase - whatever - glad you nutted it.
0
 

Author Closing Comment

by:mjbdbase
Comment Utility
Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now