+ Reply to Thread
Results 1 to 30 of 30

finding works with column range but not row range

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    finding works with column range but not row range

    Good Afternoon Folks,

    I am trying to play with "find" and "mergearea" in order to better understand vba.

    The code i wrote worked for finding a string in a range of merged columns.....but the code is failing in a range of merged rows.
    if you see the problem, please hint, but do not give me the answer. I am more likely to remember the solution if i struggle. At
    the moment, I have no clue where the struggle should be.

    Not quite. I did see that Rng was not created when i looked at it in the "locals" window. I checked the spelling of the term in the string.
    Also the Range appears logical to me. i am defining the range as column "C" and including rows 5 to 100.

    THIS CODE WORKS
    Please Login or Register  to view this content.
    THIS CODE FAILS
    Please Login or Register  to view this content.
    Thank you,
    bil
    Attached Files Attached Files

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    Please Login or Register  to view this content.
    Last edited by sintek; 03-25-2023 at 02:13 PM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: finding works with column range but not row range

    "Rng.MergeArea.Address" The joys of merging.

    Used to use it a lot. Makes things look pretty. Still looks pretty but now without headaches because of merging.

    https://edu.gcfglobal.org/en/excel-t...rging-cells/1/
    https://theexcelclub.com/stop-do-not...hy-with-fixes/
    https://www.perfectxl.com/excel-tool...t-merge-cells/
    Experience trumps academics every day of the week and twice on Sunday.

  4. #4
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    Good Afternoon, Jolivanes

    i hear your suggestion to avoid the use of merging.

    Many people have expressed this concern. The solution "center across selection" is perfect if the original intention was to maintain an aesthetic appeal.

    I am not using merged cells for aesthetics. I am using merged cells to implicitly define a range of cells.

    The attached worksheet is one of 34. each worksheet has the same structure; two primary sections.

    one whose width is defined by row 1 , columns 5-9 (the rows will change from worksheet to worksheet)
    other whose width is defined by row 1, columns 10-144 (the number of columns will change from worksheet to worksheet)

    so to determine width of section one, my code would have looked for the merged cells with the string "WallComponentOptions" and extracts the address of that range
    to determine width of section two, my code would have looked for the merged cells with the string, "wallMeasurements" and extracts the address of that range

    to determine the length of both sections, my code would have looked for the merged cells in column 4 with the string "Wall Boundary" and extracts the address of that range

    regardless of how i write this code, i must know the width and length of the regions. The merged coding approach implicitly declares the region. I thought my approach looked simple.
    But, I am beginning to realize the approach forces the person inputting information to make sure they extend the merged regions on both axes to fit the data. I might be heading for trouble that way.

    An alternative approach, which avoids merged cells, might be to look for the last cell in column 9 and knowing data always does not start until row 4, i can determine the length of the data set of each sheet without imposing the need to make sure the merged cells in column 4 match the data.

    similarily i might look for the last cell in row 6 that contains the string "z". knowing the second section always starts at column 10, I can determines the width of the data set without imposing the need to make sure the merged cells in row 1

    so....i guess you have convinced me to shift off of merged cells (at least for the time being and knowing there is an alternative approach).

    BUT...NOBODY has explained why my original code posting fails in column mode, vs row mode. It would be nice to understand why it is failing.

    Thank you for helping me.
    bil
    Attached Files Attached Files
    Last edited by whburling; 03-25-2023 at 06:13 PM. Reason: remove one of two files

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: finding works with column range but not row range


    Hi,

    Range.Find always well works according to how it is coded so if it not works as expected : your bad !
    As obviously what is searched does very not exist in the range where Find operates !
    So just correct at least the range or maybe you did not qualify the correct worksheet where to search …
    And to write a smart code check at least the result like demonstrated in the VBA help sample of Range.Find method.
    Do not forget its parameters, maybe one is necessary.
    Like you can check with the Macro Recorder once it works manually …

    Another obvious point : no need to merge cells « to define a range » but just name a range and use this name within your code.
    Last edited by Marc L; 03-25-2023 at 07:16 PM.

  6. #6
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    Sintek,

    I looked up Find. i changed your suggestion to the following:
    Please Login or Register  to view this content.
    I used "xlvalues" as i thought it would give me a more accurate response than xlformula
    i am mising some of the options....there are nine in all.

    i checked that the range was working....(it is) but the code returns an error, "Runtime error '448' Named argument not found"
    I am assuming the error means it could not find the string?
    Attached Files Attached Files

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: finding works with column range but not row range

    There is no LookAfter parameter.

    Syntax
    expression.Find (What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  8. #8
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    Hello Marc !
    Thank you for responding.

    I am a novice. so please be patient

    I read your response. lets discuss your first two lines

    the code needs to define a range in which the find will operate. the code defines the range as C3:C100. By looking at the worksheet on can see i have selected the correct column and a range of rows that encapsulates the rows of interest.

    The string is "WallBoundary" and it is spelled the same way both in the code and in the worksheet. The only difference is that the worksheet string has been rotated ccw 90 degrees. I assumed that
    was not relevant as the string is probably just a variable as is the rotation.

    There is only one worksheet.

    i will write a code check (although i am not sure i know what that means) after i get this crucial find step working.

    There are 9 parameters for Find. I rewrote the code to make clear what parameters I am using.
    Please Login or Register  to view this content.
    The above code differs from Sintek's suggestion as when i read the explanation for xlValues and xlFormula it seemed a string search would succeed more accurately with xlValues
    I was not sure how to account for the 5 parameters I did not use. i tried a series of commas but i got compile errors.

    i am going to take your and other people's suggestions and not merge. originally i was trying to avoid hard coding the range as i have 30+ worksheets to read and the data shifts.
    Hence i chose to use an implicit action on the part of the data input person (his or her using merged cells. but i realized that i was creating an opportunity for error by asking the
    inputter to correlate merged cells with data cells. So now i will look for bounding data regions within the worksheet by actually looking at data and not using merged cells and avoiding
    putting added responsibility on the person inputting the data.

    Thank you for responding....
    bil
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: finding works with column range but not row range


    You wrote :
    « the code needs to define a range in which the find will operate. the code defines the range as C3:C100. By looking at the worksheet […] »
    No, to test if Range.Find is not coded by foot that is superfluous to 'define' a range.
    You stated for C3:C100 but your code used another range !
    So what is the exact range where to search and in which worksheet ? As coding can't be guessing so that needs to be accurate at least !
    By the way an alternative to Find method is the Excel basics worksheet function MATCH …

  10. #10
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    Sintek,

    I used your suggested code with a modification and it worked for searching along a row. but when i tried the code for a search in a single column over multiple rows, it fails.
    I checked for all the obvious errors. Nothing i could see.

    WORKED - Search was along a single row bounded by columns. Different string than the code that fails
    Please Login or Register  to view this content.
    FAILED - Search was along a single column bounded by rows.....Different string than the code that works
    Please Login or Register  to view this content.
    So i am wondering if the fact the string is rotated ccw 90 might be a source of error?

    I am at a loss.

    ALSO....i could not get rid of the parameter, AFter. I tried to put in a comma to suggest that parameter was not being used, but the compiler complained.
    Attached Files Attached Files
    Last edited by whburling; 03-25-2023 at 08:20 PM.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    According to your last attachment and VBA help an Excel basics VBA demonstration as a beginner starter :

    PHP Code: 
    Sub Demo4Noob1()
           
    Dim Rf As Range
           Set Rf 
    Sheet1.UsedRange.Columns(3).Find("WallBoundary", , xlValues1)
        If 
    Not Rf Is Nothing Then MsgBox "Found in cell " Rf.AddressSet Rf Nothing
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Or this ...


    According to your last attachment an alternative as a very beginner starter :

    PHP Code: 
    Sub Demo4Noob2()
        
    Dim V
            V 
    Application.Match("WallBoundary"Sheet1.UsedRange.Columns(3), 0)
            If 
    IsNumeric(VThen MsgBox "Found in cell C" V
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: finding works with column range but not row range


    By the way according to your last attachment post #2 code works too on my side so what is the bad on yours ?!

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: finding works with column range but not row range

    Thanks for the rep.

  15. #15
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    @ whburling...
    You don't need all the parameters...See here
    And in response to all above addressed to me...See MarcL post 13...
    For this...
    The above code differs from Sintek's suggestion as when i read the explanation for xlValues and xlFormula it seemed a string search would succeed more accurately with xlValues
    When a cell has text only, this text is considered a formula AND a value
    just happens with text alignment other than horizontal, formula parameter works...
    Last edited by sintek; 03-26-2023 at 04:48 AM.

  16. #16
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    TMS
    O apologize for not thanking you earlier. Your acute observation really made a difference in successful compilation. I apologize for not
    being more attentive.

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: finding works with column range but not row range

    I apologize for not thanking you earlier.
    No worries. I was surprised that there was no comment, as far as I could see, in the thread but assumed it was lost in the moment. The +rep was sufficient recognition of my input, thanks.

  18. #18
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    Good Morning Marc!!!

    For the second time, you have brought to my attention that I have not clearly defined the range. I apologize for presenting a code snippet that did not contain all the data needed to help me. The code i uploaded in support of my original question should reveal that I thought i did define exactly where the search would take place. I copied and pasted the code and placed it below so you can see I
    am defining where the search is to take place.

    How did you know that the range i specified C5:C100 was not the range being used by the code !!!! I looked for a way to conclude that result with data. The best i could do was to look at the "locals" window and see that Rng was "nothing". That answer could mean that the range was not appropriate (C5:C100) or the string was not appropriate. but i could not find a definitive way to isolate what the problem is. Oh. (thinking). maybe break up the code line leaving off the .find. let me try that. Test revealed that the column was 3 and the row was 5 which is exactly the cell (top left) that represents the merged region. So.....the range is correct. now i will put back the .find and this time use "ZZ" without any text rotation....to see if my original string is somehow causing a fault. Test with "ZZ" also failed. so the string is not at fault. Maybe i should spell out all the parameters of find. will do that in a few minutes.


    Please Login or Register  to view this content.

  19. #19
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    I suggest...Get rid of merged cells and then upload a sample file representing your actual file and explain what it is you are actually wanting to achieve...

  20. #20
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    SinTek!!!

    Yes !!! yes !!! i saw how text IS considered a formula AND a value when i looked up "find". But when i looked for the option
    (in writing the code), the only option i had was EITHER formula OR value. i could not find an option of both !!!! Soooo when i
    looked for examples, the formula applied to "Apple" created "App" and the value apple to "Apple" created "Apple". Hence I chose
    value instead of formula. Perhaps i misunderstood the example ! now... let me go explore your "here" to understand why i don't need all the parameters. In looking at the find explanation, all but "what" are optional. but the definition of find does not seem to
    convey what one does if one does not include an option. I interpreted your line of code with ", ," to mean a placeholder for an option.
    But there are NINE options....so i expected more ",," placeholders in your line of code. very confusing (not your code and help but the
    microsoft support of the definition, Find.

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: finding works with column range but not row range


    Quote Originally Posted by whburling View Post
    Test with "ZZ" also failed.
    As posts #2, 11 & 12 still work on our side so try to update your code like these posts or at least follow post #19.
    IMO not sure it's only a merged cells issue but more a logic concern …
    Last edited by Marc L; 03-26-2023 at 10:33 AM.

  22. #22
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    Hi Sintek

    I have uploaded the sample file I have been using. Many times. At least 5 times on this one question.
    my focus, here, is to understand a failure. You and the other helpers have already convinced me to
    look at methods of identifying a region without using merged cells. So when i write my code for that
    task, i will chose another approach.

    BUT merged cells is not the issue here. I am not using Find successfully. It is not apparent to me why
    i am failing...and it is apparent that i am not using appropriate troubleshooting tools that i suspect
    exist in VBA but am ignorant about.

    The sample file is simple, on purpose. it keeps the conversation on only a few lines of code and prevents the shift of focus
    to alternate ways to achieve a goal.

    The focus remains on why does The code work on a horizontal search and fail on a vertical search.

    why is the question. That is the name of my post , in fact.

    I apologize if i sound snippy. I am actually very grateful that you guys are taking valuable time to
    help me. All of you have helped me considerably. You all made me think.

    But none of us have resolved the question i posted. Despite code corrections, the code continually fails to work.

    I think i will post another worksheet and its code to even more dramatize the Find failure. the code will have nothing
    but two lines of code and a few declarations and creations.

    It is clear that I do not know how to troubleshoot in a way that offers data to support one way of thinking or another.
    Once i get past compile errors, i should be able to use locals and immediate windows to observe what is happening
    within one line of code. But other than deconstructing (breaking up a line of ocde into simpler pieces), i am not very
    effective.

  23. #23
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    But none of us have resolved the question i posted. Despite code corrections, the code continually fails to work.
    This thread was solved in Post 2 already...Or are we missing something...

  24. #24
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: Try this !

    Marc,
    I do like your suggestion. I will try it and see what happens. in the meantime what is wrong with my code? why does it fail in the vertical find and yet succeed on the horizontal.
    what trouble shooting techniques can i use that will give me data that will help me isolate where the problem lies?

    BY THE WAY....in your code just above (Sub Demo4Noob1()) you use a place holder, ", ," I am very confused. there are NINE possible parameters for Find. does ", ," suggest a placeholder
    for more than one parameter? also how does "find" know "xlvalues" is for the parameter, "Lookat:=" I see....mmmm it is unique only for "LookAt". ok

  25. #25
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    This works for all your files...99% of the time you don't need any other parameters...
    Please Login or Register  to view this content.

  26. #26
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    thank you, Sintek!
    What I am surmising is that the comma separated by a space and then another comma is a placeholder for the default settings of the other parameters. Only one set of ", ," are needed regardless of the number of
    default parameters one is electing to use.

  27. #27
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    Not specifying an After parameter...
    Please Login or Register  to view this content.

  28. #28
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: finding works with column range but not row range

    ok...Sintek,

    I FOUND THE PROBLEM !!!!!!

    Find demanded that I put in the SearchOrder parameter for the column search. The default must be xlRows and that is why the "Find" worked for the find in rows code line
    But when applied to the column the default SearchOrder parameter was row and so it could find NOTHING given the range i had specified !!!!!

    Thank you for hanging in there with me.

    Now...to write code that will do what i want without the merged areas. will post in a new topic when i get it written.
    Thank you again.

    bil

  29. #29
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: Try this !

    Marc,
    Your code with .Columns works !!!

    in fact, your suggestion got me to consider the reason why my "find" failed in column mode.

    find uses ALL its parameters. If a coder does not use any of the parameters, find uses its default parameters.
    it turns out that find, by default, searches in a row mode despite the range declared within Find.
    to fix my code, i had to use the SearchOrder:=xlColumns parameter for the search for a cell in a column.

  30. #30
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: finding works with column range but not row range

    This
    Please Login or Register  to view this content.
    is same as
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Finding column header and value of first column based on maximum value of range
    By dpalmer76 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-05-2021, 10:04 AM
  2. [SOLVED] Macro for replacing strings in multiple columns only works for the first column in range
    By MaartenRo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-15-2021, 01:41 AM
  3. Finding MIN in a column with dynamic range
    By pavlakoos in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-21-2020, 04:32 PM
  4. Finding a sum from a column for a time range
    By mmbosman in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 08-15-2013, 04:14 PM
  5. [SOLVED] VBA Macro for Range().End(xlUp).Row works for middle of column but oddly, not top/bottom
    By justinbelkin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-17-2013, 03:13 PM
  6. finding last used column in range
    By inky in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-07-2008, 06:29 AM
  7. Finding a range from a column using VBA.
    By Mike Echo in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-03-2005, 04:07 AM

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