+ Reply to Thread
Results 1 to 4 of 4

Checking Offset Assumptions

Hybrid View

ashleys.nl Checking Offset Assumptions 09-13-2011, 10:32 AM
ChrisOswald Re: Checking Offset... 09-13-2011, 03:56 PM
ashleys.nl Re: Checking Offset... 09-14-2011, 07:38 AM
ashleys.nl Re: Checking Offset... 09-14-2011, 10:04 AM
  1. #1
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Checking Offset Assumptions

    Hey Everyone,

    I'm trying to create a check in my Excel Macro to check if my assumptions are correct. For example in a given worksheet I want to find the row labeled "Mouse/Day". Once I've found this row I assume the column "Cat" is one column to the right. But what if it wasn't? what if the columns had been input in a wrong order. Instead of the cat column being at an offset of ( 0, 1) the dog column was there instead. Is there a creative way to check ?

    Thanks for your help everyone! You've all been great these past weeks.

    Sub ColumnMatchTest()
    
    
    Dim Cat As Range
    Dim MousePDay As Range
    
       
        Set MousePDay = Worksheets("Sheet1").Cells.Find(What:="Mouse/Day", LookIn:=xlFormulas, LookAt:=xlWhole, _
                           SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                           SearchFormat:=False)
    Worksheets("Sheet2").Cells(1, "D").Value = MousePDay.Offset(, 1).Value
            
    
    End Sub
    Last edited by ashleys.nl; 09-14-2011 at 10:05 AM.

  2. #2
    Registered User
    Join Date
    11-17-2010
    Location
    MN, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Checking Offset Assumptions

    Instead of using .offset with the Mouse/day column, why not just find the column number that "Cat" is in and use that directly? Assuming, of course, that you won't have problems with the word "cat" coming up multiple times in the spreadsheet, in which case you'll want to restrict the search range from .cells to something smaller.

    Sub ColumnMatchTest()
    
    
        Dim Cat                             As Range
        Dim ColCat                          As Long
        Dim RowCat                          As Long
    
    
        Set Cat = Worksheets("Sheet1").Cells.Find(What:="Cat", LookIn:=xlFormulas, LookAt:=xlWhole, _
                                                  SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                                                  SearchFormat:=False)
        If Not Cat Is Nothing Then
            ColCat = Cat.Column
            RowCat = Cat.Row
        Else
            MsgBox ("Can't find cat")
            Exit Sub
        End If
    
    
        'obviously, this will just put the word Cat into the sheet, but I think you get the idea.
        Worksheets("Sheet2").Cells(1, "D").Value = Worksheets("Sheet1").Cells(RowCat, ColCat).Value
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Checking Offset Assumptions

    Ahhh okay, I can certainly use something like this... I didn't know the .Column and .Row funtions existed... I'll have a go at this code later today... but it looks great! Thanks for the help

  4. #4
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Checking Offset Assumptions

    Thanks Chris!

    I've taken what you had suggested and added a way to indentify the row as well. but it works great! this is the final code I've created... in case anyone else comes accross this problem

    Thanks again Chris

    Sub ColumnMatchTest()
    
    
        Dim Cat                             As Range
        Dim ColCat                          As Long
        Dim MousePDay                       As Range
        Dim RowMousePDay                    As Long
    
    
        Set Cat = Worksheets("Sheet1").Cells.Find(What:="Cat", LookIn:=xlFormulas, LookAt:=xlWhole, _
                                                  SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                                                  SearchFormat:=False)
        If Not Cat Is Nothing Then
            ColCat = Cat.Column
    
        Else
            MsgBox ("Can't find cat")
            Exit Sub
        End If
        Set MousePDay = Worksheets("Sheet1").Cells.Find(What:="Mouse/Day", LookIn:=xlFormulas, LookAt:=xlWhole, _
                                                  SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                                                  SearchFormat:=False)
        If Not MousePDay Is Nothing Then
            RowMousePDay = MousePDay.Row
    
        Else
            MsgBox ("Can't find 'Mouse/Day'")
            Exit Sub
        End If
        'obviously, this will just put the word Cat into the sheet, but I think you get the idea.
        Worksheets("Sheet2").Cells(1, "D").Value = Worksheets("Sheet1").Cells(RowMousePDay, ColCat).Value
        
            
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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