Forums > Off-Topic Discussion > Any Visual Basic people here? Need MS Excel macro

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

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

Photographer

Lohkee

Posts: 14028

Maricopa, Arizona, US

Looknsee Photography wrote:
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.

And here I thought you claimed to have a masters . . . . . . . My bad. Carry on!

Apr 07 15 02:41 pm Link

Photographer

Jay Edwards

Posts: 18616

Fort Lauderdale, Florida, US

Lohkee wrote:

And here I thought you claimed to have a masters . . . . . . . My bad. Carry on!

ouch

Apr 07 15 02:58 pm Link

Photographer

Gregory Thelen

Posts: 145

Concord, California, US

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

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

Lohkee wrote:

And here I thought you claimed to have a masters . . . . . . . My bad. Carry on!

Got anything relevant to contribute to the thread?

Apr 08 15 09:02 am Link

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

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

Photographer

RW Steele

Posts: 119

Raleigh, North Carolina, US

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

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

RW Steele wrote:
If you're able to find and put focus on the cell then try this:
Application. Goto ActiveCell.EntireRow,True

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

Photographer

Llobet Photography

Posts: 4915

Fort Lauderdale, Florida, US

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

Photographer

LightDreams

Posts: 4440

Vancouver, British Columbia, Canada

Looknsee Photography wrote:
I challenge Lohkee to provide an answer written in Icelandic (or any other language he doesn't know).

[EDIT] Edited to convert my answer to Icelandic (which I don't know!) [/EDIT]

Auðvitað, VBA lausn í íslensku er algerlega eins.

Ég veit, ég veit, að þú vilt bara einhvern til að skrifa kóðann fyrir þig!

Apr 09 15 09:27 am Link

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

LightDreams wrote:
[EDIT] Edited to convert my answer to Icelandic (which I don't know!) [/EDIT]

Auðvitað, VBA lausn í íslensku er algerlega eins.

Ég veit, ég veit, að þú vilt bara einhvern til að skrifa kóðann fyrir þig!

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.

I concede that Icelandic is not the same as Visual Basic.  I do have deep knowledge of computer science and have spent many years designing & maintaining all aspect of an operating system, including user interface (my favorite), spooler, memory manager, process manager, device drivers (wrote the first driver for a laser printer), and many more.  I'm conversant is a handful of programming languages, but Visual Basic isn't one of them.

One difference between Icelandic & Visual Basic -- with Icelandic, you can use Google Translate -- what's the Google Translate version the converts programming languages?

So sue me -- I have a specific question -- how to use a value stored in a cell to highlight a cell & move it to the top of the unfrozen cells in an Excel spreadsheet.  If you have knowledge, please share.  If you want to throw around unconstructive jabs, I suggest you CAM.

Apr 09 15 11:26 am Link

Photographer

LightDreams

Posts: 4440

Vancouver, British Columbia, Canada

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

Photographer

LightDreams

Posts: 4440

Vancouver, British Columbia, Canada

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

Photographer

Jirrupin

Posts: 1755

Canberra, Australian Capital Territory, Australia

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

Photographer

scrymettet

Posts: 33239

Quebec, Quebec, Canada

Looknsee Photography wrote:
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.

do while lenIcell(row,ligne)) > 0
   i =   InStr( [start], string, substring, [compare] )
  if i > 0
    activecell =
end if
loop

or something

Apr 12 15 08:23 am Link

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

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

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

Looknsee Photography wrote:
...  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 well, but there are a couple of things that I need to tweak:

1)  I've hard-coded the size (number of rows) in the "database", in this case "671".  But I'm often adding rows to the "database", so eventually this won't work.  I've found that by changing the "671" to (say) "2000" has no adverse effect on the functionality of the macro.  The macro doesn't need to change at all -- if necessary, I'd have to update the MATCH and VLOOKUP formulas.  But at 2000, that'll be a long time away.

2)  The "Rows(Counter).Select" line selects the whole row, which is what I wanted.  But sometimes, I scroll horizontally, the look at the columns that fall off to the right, sometimes outside of the Excel window.  When the "Rows" line executes, the window is scrolled all the way to the left (kinda resetting the horizontal scroll, if any).  Anyone with any ideas how to preserve the horizontal scrolling with a select the entire row?


I worried about using a search criteria off the end of the database:  for example, if the last entry was for "Zoo", what would happen if I searched for "ZZZ"?  No worries, as written, it'll just highlight the last row, the one for "Zoo".  As a programmer, I was always making what I call "Off By One" errors like that, so I developed the habit of testing all the boundaries.

Apr 14 15 11:47 am Link

Photographer

LightDreams

Posts: 4440

Vancouver, British Columbia, Canada

Looknsee Photography wrote:
Anyone with any ideas how to preserve the horizontal scrolling with a select the entire row?

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:

- Possibly defining the worksheet scroll area. (<worksheetname>.ScrollArea).   Try a Google search for "excel macro freeze scroll".

- The other theoretical possibility is to programically restore your previous scrolling position after Excel changes it.  See:
http://www.automateexcel.com/2004/08/25 … _scroll_h/

For what it's worth!

Apr 14 15 12:16 pm Link

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

LightDreams 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:

- Possibly defining the worksheet scroll area. (<worksheetname>.ScrollArea).   Try a Google search for "excel macro freeze scroll".

- The other theoretical possibility is to programically restore your previous scrolling position after Excel changes it.  See:
http://www.automateexcel.com/2004/08/25 … _scroll_h/

For what it's worth!

Those are tasty hints, but I think I have to ruminate a bit more.

The problem is that when I execute the "find row" macro, I don't know how far I've scrolled horizontally.  Your hints tell me how to scroll to a specific row and/or column, but I don't (yet) know how to record the currently scrolled position before I select the appropriate row.  I'll keep looking -- it's an interesting problem, especially since I don't know VB.

Apr 14 15 12:56 pm Link

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

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

Photographer

Looknsee Photography

Posts: 26342

Portland, Oregon, US

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