Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hughmoris
Apr 21, 2007
Let's go to the abyss!
Is it possible to write a script that will go through a spreadsheet, and if a row has a value, hide that row? I've started a new job and am trying to teach myself python scripting at the same time. Today I was tasked with reading through a spreadsheet, and if a cell had a certain string, I wanted to hide that entire row. Tedious stuff, and I'm guessing there is a way to automate it.

I think i can fuddle my way through writing a basic script but I'm unsure how to issue a "hide row" command.

Adbot
ADBOT LOVES YOU

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Old James posted:

code:
Sub Hide_Row()
   Dim CELL As Range

   For Each CELL In Selection
      If Len(CELL.Value) > 0 Then CELL.EntireRow.Hidden = True
   Next CELL
End Sub

Thanks for this. A couple of dumb questions. What language is this, VB? How would I adjust it if the keyword I'm looking for is either "nausea" or "vomiting". The cell may have both.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I've been tackling a side project at work with Perl. I have a working solution but its a pretty shoddy hack. I'm thinking of re-writing the entire thing in Excel and VBA as a learning exercise. Outside of the taskscheduler, do these pseudo steps look possible entirely within Excel/VBA?

1. TaskScheduler to launch Workbook
2. Macro executes when Workbook is launched that:
- Copies pipe-delimited text file from network folder to local folder
- Loads that delimited file into workbook
- Parses delimited file, pulling out needed information and spreading it over 6 named worksheets
- Send each worksheet to a different specified network printer
- Bonus step: zip delimited file and move to different folder
- Close workbook and exit Excel

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

Schedule a repeating meeting in Outlook and trigger VBA off a combination of Category and Subject for your taskscheduler functionality! :v:

My work uses Lotus Notes. :suicide:

Hughmoris
Apr 21, 2007
Let's go to the abyss!
*Ignore, figured out the problem.

Hughmoris fucked around with this message at 05:04 on Aug 29, 2015

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

here, try this:

Writing Excel Macros with VBA, 2nd Edition https://www.amazon.com/dp/0596003595/ref=cm_sw_r_awd_s7s4vbSHRTS2B

Thanks for this. I'll see if I can get my hands on it.

In the meantime, I'm getting tripped up. If I run this code with worksheets("ALERTEXTRACT") selected in the background, it works fine. If I run it with Worksheets("Sheet1") selected, it gives me the runtime error '1004': Method 'Range' of Object'_worksheet' failed. Why does it toss me that error even though I'm explicitly naming what worksheets I want to read from?

Also, is this code considered "good practice" for iterating over a column of unknown length??

Visual Basic .NET code:
Sub GetPhysicianConsult()
    Dim i As Long, j As Long
    Dim wb As Workbook
    Dim rs As Worksheet, ws As Worksheet
    
    
    Set wb = Workbooks("ALERTEXTRACT.txt")   
    Set rs = wb.Worksheets("ALERTEXTRACT")   'rs is the sheet i'm reading from
    Set ws = wb.Worksheets("Sheet1")  'ws is the sheet i'm writing results to
    
    j = 1
    
    For i = 1 To rs.Range("A1", Range("A1").End(xlDown)).Rows.Count
        If rs.Cells(i, 17).Value = "PHYSICIAN CONSULT" Then
            ws.Cells(j, 1).Value = rs.Cells(i, 1)   'writes out the account number associated with the consult.
            j = j + 1
        End If
    Next i
End Sub
*Just read an article saying its better to use a filter, instead of looping. I'll read up on that technique.

Hughmoris fucked around with this message at 01:42 on Aug 30, 2015

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

Great VBA stuff...

Wow, thank you for taking the time to write that up. The read file for this project is of unknown length but typically 220k+ rows. I'll put up the final version of the script when I finish it, for critiquing. Also, that benchmarking tool looks pretty handy. I'm curious how performing these actions natively within VBA will compare to my Perl script that accomplishes the same thing.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

Your read file is pipe-delimited text, not an xls/xlsx, right? I'd expect Perl to smash the gently caress out of VBA. A regex looking for "PHYSICIAN CONSULT" after the 16th | and looking back to the beginning of the line to the first | should return exactly what you want. Cycle through that and kick it out to whatever.

Yeah, Perl flies through the file but the kicker is that I also need to print these sheets to different network printers and VBA seems to handle that like a champ.

Here is where I ended up last night after staying up way too late writing code. This is meat of it:
Visual Basic .NET code:
Sub ConsultFilter()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

'declare variables and populate list of units
    Dim wb As Workbook, rb As Workbook
    Dim ws As Worksheet, rs As Worksheet
    Dim i As Integer, LastRow As Long
    Dim Unit(1 To 4) As String
       
    Unit(1) = "Surgery"
    Unit(2) = "Medical"
    Unit(3) = "Oncology
    Unit(4) = "ICU"
    
    'set variables and find out how many rows there are
    Set rb = Workbooks("ALERTEXTRACT.txt")
    Set rs = rb.Worksheets("ALERTEXTRACT")
    Set wb = Workbooks("ConsultReport.xlsm")
    
    LastRow = rs.Cells(rs.Rows.Count, "A").End(xlUp).Row
    
    'Loop through each Unit, and if it find any physician consults for that Unit, it pastes it to those rows to that Units worksheet
    For i = 1 To 4
        With rs
            With .Range("A1:Z1")
                .AutoFilter
                .AutoFilter Field:=4, Criteria1:=Unit(i)
                .AutoFilter Field:=17, Criteria1:="PHYSICIAN CONSULT"
            End With
		'this Range line is ugly and eats up the most processing time it seems.  It copies the filtered data and pastes it to the Units worksheet
            .Range("A1:A" & LastRow & ",D1:D" & LastRow & ",E1:E" & LastRow & ",G1:G" & LastRow & ",J1:J" & LastRow & ",V1:V" & LastRow & "").SpecialCells(xlCellTypeVisible).Copy _
                Destination:=wb.Worksheets(Unit(i)).Range("A1")
        End With
        wb.Worksheets(Unit(i)).Columns.AutoFit
    Next i

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Cast_No_Shadow posted:

Out of interest have you tried loading into and looping through an array and setting the value of each cell individually? Never tried it at what i assume is (200k x 4) 800k lines but for an order of magnitude lower i believe it can be quicker. Turn off calculations and screen updating.

I haven't tried that method yet but I'm going to. Once I have a full working version using my current method, I'm going start over and try different techniques to improve the speed. Right now it takes about 12 seconds to run, but the starting file is roughly (200k x 15) cells.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I'm a bit stumped on a problem.

How do I execute a macro after a user clicks the Refresh All button? I have a fully functioning macro intended but I'd like to have it key off the action of Refresh All, instead of the user having to manually launch the macro. My googling is not turning up any examples I can get working.

*Disregard. I got it working. Here's some info I used incase anyone needs to reference it in the future:
https://excelandaccess.wordpress.com/2014/01/18/create-beforeafter-query-update-events/

Hughmoris fucked around with this message at 01:43 on Sep 16, 2015

Hughmoris
Apr 21, 2007
Let's go to the abyss!
For you VBA wizards out there, do you use other languages for problems that don't necessarily need a spreadsheet? For instance, I need to loop through a directory of folders and pull out csv files to combine into a master CSV, then do some parsing. Typically I'd use Python but it seems VBA can accomplish just about anything with all the reference libraries available to it. The feeling I get from reading various forums is that a lot of people use VBA as the last option.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

ShimaTetsuo posted:

Pretty sure the libraries you're talking about are not specific to VBA and could be used from most any language (COM libraries and whatnot; the world of "native" VBA libraries is basically non-existant afaik). I wouldn't recommend anyone use VBA for anything unless you are already using it for related things (I have a massive legacy VBA code base to maintain and extend). If this is just a simple task that you're going to do once and throw away, it doesn't really matter what you use.

I think for tiny tasks like combining/filtering text files most normal people use unix command line tools like awk or whatever.

fosborb posted:

VBA is best used to slightly extend the functionality of Office products. If normal, out of the box use of Excel or Word or Access, etc isn't 95% of your solution, chances are you have better options out there.

The problem is that you can do a lot more with VBA than you should and it's very easy to wind up with Excel being nothing more than a lovely front end to a ubiquitous, "easy" script engine.

Thanks, that's about what I expected. I've accomplished a few neat macros with VBA a little while ago out of necessity and was just curious if I want to fall further down that rabbit hole. I don't think I do.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
This is more of a VBA question than Excel but I'm thinking this thread if my best hope for VBA experts.

For work, I'm part of a 25-person committee that makes decisions on certain topics. What I would like to do is create a questionnaire or form of some sort that can reside in an email and be sent out, and the recipients can answer the 5 or so questions with a YES/NO/MAYBE. I'd then need a way to log/track the answers from all the recipients. I have Outlook and Excel 2013 to work with. Any ideas on a simple way to go about this? It looks like there is a simple "Poll" type email that Outlook can do but I think its only good for 1 question. I'm thinking I'll need to create some sort of form with VBA?

Hughmoris fucked around with this message at 03:39 on Sep 17, 2016

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

Just use Survey Monkey. Literally the entirely of Corporate America uses it for bullshit one off polls.

Yeah, Survey Monkey would definitely be the easiest way to go about it. I'd like to try my hand at it as a learning exercise. Am I on the right track, thinking I can build a VBA userform inside an outlook email and the recipients somehow submit their answers back to me?

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

If you're on a committee of 25 it sounds like you're in a larger company. Check to see if you have Microsoft InfoPath. That + SharePoint could probably solve your problem. Link.

Otherwise, I think you're SOL. Here's a breakdown of html form functionality by email client.

mystes posted:

Even if this is possible, you definitely should not do this. You should not be emailing random VBA code to people to execute. Also, you would still need a way to receive the answers, and sending an email without user intervention or something like that would be extra terrible.

If you really wanted to do this yourself manually you would have to run an HTTP server to host an HTML form which is not something you can do in VBA.

Do what fosborb said and try infopath or use something off the shelf like survey monkey.

Thanks for the ideas. After thinking about it for a few days, ya'll are right. It isn't worth taking on the headache, and I'll see if others want to use other means.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Being a VBA novice, I about had a fit today trying to get regex to work for me.

I have a column of cells, each with a format like:

quote:

---------------------------------------
asjdfia
250
ajdfja
W123456789123
ajsdif
mzcv
hsafa
285
madmf
W111222333444
afmdfa
--------------------------------------

It's always a volume (250), a line of garbage, then the unit number (W123456789123). I want to extract the volume and its associated unit number, in pairs. Ideally, I'd end up with:
code:
| 250 | W123456789123 |
| 285 | W111222333444 |
...
I've been able to successfully do it in Python with re.findall() but I'm trying to write this as a macro for a coworker.

Any advice on how to go about this? I was following this example on stackoverflow, example #4 of the top response.

http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops

Hughmoris fucked around with this message at 01:34 on Dec 16, 2016

Hughmoris
Apr 21, 2007
Let's go to the abyss!

mystes posted:

If the data is in exactly that format, surely you don't need a regex at all, you can just iterate over the cells (or rather the value array of the range, which is faster)?

The volume and unit number always have one line of text separating them, but there is a random amount of data before and after them. The volume is always 3 digits, and the unit number is W + 12 digits.

Is there an easy solution that I'm missing?

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Convicted Bibliophile posted:

I have a small task I need to accomplish in an existing excel spreadsheet but I'm a total newbie when it comes to vba/macros in Excel. Instead of posting it here (I'm not sure how long it would take and I don't want to waste people's time), does SA have a place where you can pay people to do small things in Excel for you?

You might have some luck here: "Request a tiny custom app"

http://forums.somethingawful.com/showthread.php?threadid=2415898&userid=0&perpage=40&pagenumber=1

Hughmoris fucked around with this message at 21:57 on Dec 26, 2016

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Phraggah posted:

Not sure if you were able to figure this out. First I'd define two patterns. This assumes what you're looking for will always be in its own cell...

Thanks for this. This small project has taught me quite a few things: more VBA, slicers, Power Query, and the fact that Excel has a built in data form that you can use to query data. I thought I was going to have to create a VBA userform so that others could easily search the parsed data.

Hughmoris
Apr 21, 2007
Let's go to the abyss!


Can someone tell me if this can be accomplished in Excel? I'm looking at a datasheet that has medication administration times. Each order has an allowed frequency that dictates how frequent a medication can be given. If it's administered sooner than its allowed frequency than an alert should fire and notify the user.

In the table above, med_id ->1111 was given for the first time at 0100. Since it has a frequency of "every 6 hours" then it should not be given again before 0700. However, it was given again at 0645 so the alert should fire. I want to look at every administration in the sheet and add a Y/N on whether or not that administration should have fired an alert.

* I guess to simplify, I need to look at each TIME_GIVEN and see if the same med_id was previously administered within TIME_GIVEN - FREQUENCY

Hughmoris fucked around with this message at 05:31 on Aug 12, 2017

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

Woooo!

Assumption: all frequencies are hours, all frequency values' numbers start at position 7, and all frequency values have " hours" after the number.

1. hour_offset in frequency: =MID([@frequency],7,FIND(" ",[@frequency],7)-6)
2. next_time to give med: =[@[last_dose]]+([@[hour_offset]]/24)
3. rank of time_given by med_id: =COUNTIFS([med_id],[@[med_id]],[time_given],">"&[@[time_given]])+1
4. last_dose time by med_id: =AGGREGATE(14,6,[time_given]/([med_id]=[@[med_id]]),[@rank]+1)
5. should_alert_fire? : =IFERROR(IF([@[next_time]]>[@[time_given]],"Y","N"),"")



ALL TOGETHER
=IFERROR(IF(AGGREGATE(14,6,[time_given]/([med_id]=[@[med_id]]),COUNTIFS([med_id],[@[med_id]],[time_given],">"&[@[time_given]])+1+1)+(MID([@frequency],7,FIND(" ",[@frequency],7)-6)/24)<[@[time_given]],"N","Y"),"")

You are a wizard. This is great, thanks!

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I'm stumped coming up with an Excel solution for this problem.

Say I have a table of employee names, boss names, and the amount of money each boss gave to an employee.


What I'd like to end up with is a summary of some sort detailing who gave to each employee. The amount given doesn't need to be in the summary. The summary doesn't have to be pretty, just such that you can tell at a glance who donated to a given employee. So, something like:
code:
John | [CEO, COO]
Bill | [CEO, CTO, COO]
Sarah | [CTO]
Steve | [CEO, CTO, COO, CIO]
My first thought was a pivot table but I can't figure out how to structure my data. Any ideas?

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Fingerless Gloves posted:

Quick and dirty, set up an if statement table next to it. Maybe something like

=If(cell>0,$1:$1,"")

Then you can concatenate this together to make a single column list.

Thanks!

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

Awesome excel stuff...

Great writeup. I don't work with an excel-savvy team so its nice to see other's thought process as they work through problems.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Today was great.

I noticed that one of the members on our project team was doing repetitive work in a spreadsheet. I reached out to see if he'd be interested in learning how to automate some of his work, and he was. He showed me one of his tasks which entails running a report, combining workbooks, formatting columns, adding formulas and saving. He has 100+ reports he needs to perform this on. The same exact steps for each one.

The look on his face when I showed him how to record a macro and run it on each report was amazing. The simple macro will probably end up saving him 35+ hours of work on this project.

Even better was seeing the wheels turning in his head in realizing the potential of macros, and automation in general.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I need a little guidance on an excel stumper.

I have a workbook with a lot of worksheets, each worksheet being named after a store. I have a list of default users that are found in every worksheet. The listing of the users is the same for every worksheet. John will always be in row 2, Phil in row 3 etc...





Finally, I have a worksheet call Total that would have the list of users and the COUNT of their entries in each worksheet/store:




I have a lot of stores, and a lot of users. For this Total table, I'd like a formula that would reference each worksheet dynamically using the column header (which is the store name and appropriate worksheet name) and calculate the COUNT of each user for each store.

I was poking around using COUNT(INDIRECT(....) but I couldn't get the relative cell range to work. John should be B2:D2, Phil is B3:D3 and so on.

I hope that makes sense. Any help is appreciated.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

kumba posted:

The idea is you're counting the number of non-blank cells in columns B:D for each user, yeah? In that case, in your Total sheet in cell B2 (or wherever it is that John & Amazon intersect) you can use this:

=COUNT(INDIRECT(B$1&"!B2:D2"))

If you want it to be a bit more dynamic, you can set up a tab of 'Range Lookups' like so:



And then use this formula instead:

=COUNT(INDIRECT(B$1&"!"&VLOOKUP($A2,'Range Lookups'!$A:$B,2,0)))

Be warned though that INDIRECT is a volatile excel function and, given enough data in your spreadsheet, will cause massive performance issues due to having to recalculate every cell any time any cell is changed. There's probably a better way to do this using MATCH and INDEX.

Thanks!

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Anyone have guidance on how to use Excel as a front-end for SQL Server? Or should I even want to do that?

I recently was given database access and have been using SSMS to write basic queries. The database has hundreds (if not thousands) of tables. What I'm currently doing is running the query is SSMS and exporting to CSV, then opening that CSV in Excel and putting together simple charts etc...

Is there a more streamlined way to approach this?

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fosborb posted:

You should be able to make a data connection in Excel that has your queries, then feed that to a table.

If you're looking for reporting and have a SharePoint environment, SSRS would be a better option, though that works best with a Visual Studio licence as well. $$$

Tableau works well as a standalone report authoring tool, or web based delivery, or even with a SharePoint wrapper. But that's $$$$$$$.

Thanks for the ideas. I might have a Tableau license available to me, so I'll read up on that.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Does anyone fool with Power Query and Power Pivot? If so, what are your typical use cases?

I'm trying to figure out if it's worth the effort of a deep dive and learning.

Hughmoris fucked around with this message at 04:54 on Feb 23, 2018

Hughmoris
Apr 21, 2007
Let's go to the abyss!

kloa posted:

I used to work with some analysts that got into it, and by proxy I sat in with them just to learn for fun.

The only reason they got into it is because one of the guys had a hard time learning SQL/DWH/SSAS, but he knew Excel well. He dove into Power stuff to be able to actually do complicated calculations that he couldn't figure out how to do in SQL :shrug:

Speaking of SSAS, do you know of any recommended learning resources? I was just recently given sql/ssrs/dwh access and am trying to futz my way through but I really don't have a great learning path identified.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Sri.Theo posted:

Thanks! I might do that as I can’t get the pivot table to show what I want. If I drag Deadline into the ‘Row’ box and removing months and quarters it won’t let me put ‘status’ in the column box to show it the way I want.

I admit I don’t really understand pivot tables, sometimes I click the button and it does what I want so I leave it at that!

If you do a decent amount of work in Excel then it would pay to become more familiar with pivot tables. This guy has a Excel series that covers all sorts of topics but here is one specific to pivot tables:

https://www.youtube.com/watch?v=e-yuYNgsHAk

Hughmoris
Apr 21, 2007
Let's go to the abyss!
This is tangentially related to Excel and the O365 ecosytstem, not sure of a better thread to put this in.

Has anyone used Microsoft Flow / Power Automate? I just discovered it, and am currently a solution looking for a problem. I'm looking at my daily/weekly tasks and seeing how I can use it to automate some Excel -> Outlook -> OneNote flows.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
What do you mean Excel doesn't have unlimited rows?

https://arstechnica.com/tech-policy/2020/10/excel-glitch-may-have-caused-uk-to-underreport-covid-19-cases-by-15841/

Possibly 16k UK covid cases went untracked due to exceeding spreadsheet row limits.

Hughmoris fucked around with this message at 20:00 on Oct 5, 2020

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Has anyone poked about Office Scripts much? I just found that my O365 subscription for work includes it. Been reading a few articles on it but I haven't really found a use case yet.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Harminoff posted:

Is anyone watching Excel Esports on ESPN?

https://www.youtube.com/watch?v=x1RVNGDSdw4

I had no clue that was a real thing. Pretty cool.

Also, there is a pro streamer scene around Excel: https://www.youtube.com/watch?v=xubbVvKbUfY

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Strong Sauce posted:

https://twitter.com/benlcollins/status/1562546876024778753

lambdas, xlookup, makearray, scan. no more using weird arrayformulas to try and apply a function to each row/cell!!!

Everyone in the scene knows that XLOOKUP breaks the meta:

https://www.youtube.com/watch?v=ICp2-EUKQAI

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Any M-code gurus out there using it daily? If so, your thoughts? I've recently started exploring Power Query inside Power BI, seems like a fun little language.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Is there any uglier language than DAX? Survey says: no.

Adbot
ADBOT LOVES YOU

Hughmoris
Apr 21, 2007
Let's go to the abyss!

DRINK ME posted:

I’ve just been using DAX for a couple of weeks in power bi and it’s ain’t fun.

I started with calculated columns trying to do things, but I ended up with a single measure that does the currency conversion and displays the right segments using selecteditem() values.
It works but it was a hell of a journey to get there.

I have zero affiliation with these guys but I've found their free "Intro to Dax" course to be pretty approachable:

https://www.sqlbi.com/training/

I always found an online DAX repl which I've been poking about with.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply