Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested With Statement hierarchy

Tags:

excel

vba

I've come across this a couple of times recently and was just curious if there was an easier way to do this...

With Activeworkbook
  'Do Stuff
  With .Sheets(1)
    'More stuff Done
    '...
    'But now I need to refer to or pass the Sheet in the last With Statement
    SomeFunctionAnswer = SomeFunction Activeworkbook.Sheets(1)
  End With
  'Yet more stuff Done
End With

Does it have to be fully written out, or is there some way of notating it that makes it easier/cleaner? Perhaps there is some sort of property or method to pass itself for just this instance? What about referring to a property or method from the higher With?

SomeFunctionAnswer =  SomeFunction .Self  '???
'OR
SomeFunctionAnswer =  SomeFunction .Parent.Name  '???

Hope that makes sense...

like image 334
Rdster Avatar asked Jan 23 '17 20:01

Rdster


People also ask

What is a nested if?

A nested IF is just two more IF statements in a formula, where one IF statement appears inside the other. To illustrate, below I've extended the original pass/fail formula above to handle "incomplete" results by adding an IF function, and nesting one IF inside the other: = IF(C3 = "","Incomplete",IF(C3 >= 65,"Pass","Fail"))

What is top nested operator in SQL Server?

The top-nested operator accepts tabular data as input, and one or more aggregation clauses. The first aggregation clause (left-most) subdivides the input records into partitions, according to the unique values of some expression over those records.

Why is it important to arrange conditions in nested IF statements?

Because a nested IF formula returns a value corresponding to the first TRUE condition. Therefore, in your nested IF statements, it's very important to arrange the conditions in the right direction - high to low or low to high, depending on your formula's logic.

What does it mean to nest in Excel?

What nesting means Nesting simply means to combine formulas, one inside the other, so that one formula handles the result of another. For example, here's a formula where the TODAY function is nested inside the MONTH function:


Video Answer


1 Answers

... curious if there was an easier way to do this... Does it have to be fully written out,...?

I too have had this problem; an example is transferring data from a TYPE structure to a database RST structure, where I would like to use the dot-notation in the innermost With/EndWith statement block.

I came up with the following:

Function AddTypdataToDB(dbs As Database, typData As typAddressInfo)
    Dim rst As Recordset
    Set rst = dBase.rstOpenRecordset(dbs, "SELECT * FROM Master")
        rst.AddNew
        With typData
            rst.Fields("Amenities") = .strAmenities
            rst.Fields("BusinessName") = .strBusinessName
            rst.Fields("CountOfBathrooms") = .strCountOfBathrooms
            rst.Fields("CountOfBeds") =
            rst.Fields("CountOfGuests") =

Here, I am partway through writing the VBA program code with my least-effort mechanism to date:

  1. I typed in the first assignment (“amenities”) in full.
  2. I copied that full statement to the line below, ...
  3. ... stripped away the .strAmnenities part and then ...
  4. ... copy/pasted that skeletal line many times.
  5. From the Access MDB table definition, I copy/pasted the field names (Alt+Tab between the two windows)
  6. Finally, I typed a period after each successive equals sign, and scrolled to the appropriate field name from my TYPE structure.

This sounds complex when I write it out in English, but it is the fastest error-free way I have found to generate code manually. I have eliminated the outer WITH statement and used explicit RST code, but the bulk of that is copy/pasted.

Not only do I reduce the elapsed time to generate a block of VBA code for my AddRecord procedure, but I save time by not having to correct the myriad errors that creep in when I code in “long-hand”.

like image 132
Chris Greaves Avatar answered Nov 15 '22 17:11

Chris Greaves