Forums >
Off-Topic Discussion >
Any Visual Basic people here? Need MS Excel macro
Okay -- I'm lazy. I've got a large MS Excel (2010) spreadsheet with a kinda database in it -- it has about a dozen columns and several hundred rows. The spreadsheet is sorted alphabetically on the info in the first column. I want a macro that works like this: ... I put a partial text string in a spare cell. ... I fire up a macro the reads that spare cell & scrolls the spreadsheet to that the closest match appears up at the top of the spreadsheet (right under the frozen panes that contain the labels for the columns). Any hints about how to go about doing this? I can get VLOOKUP to work to find the appropriate entry in the first column, but I don't know how to tell the spreadsheet to scroll down appropriately. All help appreciated. Apr 07 15 02:25 pm Link Looknsee Photography wrote: And here I thought you claimed to have a masters . . . . . . . My bad. Carry on! Apr 07 15 02:41 pm Link Lohkee wrote: ouch Apr 07 15 02:58 pm Link Been awhile since I played with Macros but you might try a MoveNext statement in the VBA. Does what you have work and it finds a record and stops and do you want to know how to make it continue? After re-reading your question, I am confused a little. Apr 07 15 03:22 pm Link Apr 07 15 03:58 pm Link Lohkee wrote: Got anything relevant to contribute to the thread? Apr 08 15 09:02 am Link I've gotten close. If I put the search string in cell O2, then the formula "=MATCH(O2,A4:A669)+3" will return the row number of the first row that is less than the search string (the "+3" part is to compensate for the 3 rows of "freeze pane" of column labels). I just don't know the Visual Basic syntax to scroll down the number of rows stored in O2. To illustrate my position, I challenge Lohkee to provide an answer written in Icelandic (or any other language he doesn't know). Apr 08 15 01:05 pm Link If you're able to find and put focus on the cell then try this: Application. Goto ActiveCell.EntireRow,True Apr 08 15 03:13 pm Link RW Steele wrote: Thanks, but my question is how do I tell VB to put the focus on the row (stored in cell O2), column A? Apr 09 15 08:01 am Link Range("whateverCell").Select ActiveWindow.SmallScroll Down:=5 -moves the window around a bit, try different values like Up, ToRight, ToLeft Just guessing Apr 09 15 08:16 am Link Looknsee Photography wrote: [EDIT] Edited to convert my answer to Icelandic (which I don't know!) [/EDIT] Apr 09 15 09:27 am Link LightDreams wrote: Nei, ég þarf ekki einhvern til að skrifa kóðann fyrir mig. Ég þarf bara smá hjálp með því að nota gildi geymd í klefa sem ættingja eða alger heimilisfang. Apr 09 15 11:26 am Link VBA programming code written in "icelandic" (and many other languages) is English with no translation required. Internationally they learn the same English major programming code languages that you do. Which is why I thought you were joking about it being written in icelandic or any other language that you don't speak, knowing that the code (and code language) would all be the same. As far as the Icelandic portion of your message, re: "need a little help with using the value stored in the cell as a relative or absolute address", this isn't the whole solution but it may help you along: http://excelribbon.tips.net/T010738_Con … olute.html It apparently deals with Application.ConvertFormula for distinguishing between absolute locations, relative locations, absolute columns and relative columns. Hope it helps, at least somewhat! Apr 09 15 02:34 pm Link For your later, much more specific question, not sure how helpful these Google finds are or not: VBA code for moving cell (contents) http://www.ozgrid.com/forum/showthread.php?t=33713 Dynamically select cells based on specific values http://www.vbaexpress.com/kb/getarticle.php?kb_id=20 MAY help in getting a cells value based on another variable. Not so sure about this one(?) http://stackoverflow.com/questions/1492 … a-variable Apr 09 15 03:02 pm Link its been about fifteen years for me, but I'm pretty sure you can nest your vlookup into the cell reference when you use activate or goto and do it in a single line of code Apr 12 15 02:14 am Link Looknsee Photography wrote: do while lenIcell(row,ligne)) > 0 Apr 12 15 08:23 am Link Solution Found! Had a few minutes to figure this out today, and I've got a macro that seems to work. Here's the solution I found: The Challenge: I have a large database type Excel (2010) spreadsheet with maybe a dozen columns & several hundred rows. The data in the first column (Column A) is alphabetized. I used the top 3 rows for field labels; these stay in the window via "Freeze Panes". I wanted to be able to type in a few characters in an available cell and then run a macro that will scroll to & highlight the row that best matches the search characters. I wanted to highlight the last row whose value in Column A was less than the text I had placed in the search cell. The Solution: I chose some cells in the top 3 (frozen pane) rows, so that the functionality would always be visible. For me, these cells were as follows: (I might move them around after I choose a better formatting): ... Cell O2: That's where I type in the search characters. ... Cell N2: Has the formula "=VLOOKUP(O2,A4:M671,1)" which returns the contents of the best match to the search criteria. ... Cell N3: Has the formula "=MATCH(O2,A4:A671)+3" which returns the row that best matches the search characters in Cell O2. The "+3" is needed to skip past the top 3 frozen rows. I wrote a simple macro using the information stored in Cell N3. It is as follows: Sub Find_Row() Dim Counter As Integer Counter = Range("N3").Value Rows(Counter).Select End Sub It works this way: ... I create a variable, called "Counter", ... I copy the value in Cell N3 into Counter, ... I use index addressing to select the row indicated by Counter (or Cell N3). Not shown here -- I assigned the macro to a button, which I placed next to the search criteria cell. Thanks for the sincere attempts to help. I suppose I could have gotten this to work with a DO WHILE loop, but as a computer scientist, that just seems wasteful & inefficient to me. And to the doubters, I managed to figure this out without any help and without knowing Visual Basic syntax. Thanks again. Apr 13 15 01:21 pm Link Looknsee Photography wrote: It works well, but there are a couple of things that I need to tweak: Apr 14 15 11:47 am Link Looknsee Photography wrote: Found on Google so I can't personally vouch for them, but there appears to be at least 2 possible approaches that might be useful: Apr 14 15 12:16 pm Link LightDreams wrote: Those are tasty hints, but I think I have to ruminate a bit more. Apr 14 15 12:56 pm Link Refined Solution: I have created some macros that reacts slightly differently -- I'll use them for a while to see if I prefer one over another, but basically, I can search for and find an appropriate cell, range, row, or column anywhere in the "non-frozen" area of the spreadsheet. Thus, if "myrow" and "mycol" are integers that contain the row & column of the target cell, I can use... Cells( myrow, mycol ).Select To select that cell. But that cell can be pretty much anywhere in the "unfrozen" area -- usually, it winds up in the middle. If I want to scroll the unfrozen area so that the selected cells is in the upper left corner (just within the "unfrozen area"), I need to execute the following Visual Basic command: Application.Goto Selection, True To be honest, this is mostly an incantation to the VB gods, but the "Application" actually refers to Excel itself, and commands in this form allow a VB program to access Excel functionality & formulas, and the "Goto Selection, True" is what moves the selected cells to the first position outside the frozen panes areas. This VB command works regardless of the shape of the selection. I learned a lot with these programming challenges -- I wouldn't consider myself expert yet, but I have made some useful macros for my business spreadsheets. Apr 26 15 09:44 am Link My computer scientist hackles rise a little bit by this. If you use absolute addressing (as in "B5"), it's column first followed by row, But if you use indexed addressing (as in "Cells( myrow, mycol ).Select "), it's row first, followed by column. Go figure. Apr 26 15 09:47 am Link |