I am really new to VBA. SO this might seem a silly question for experts.
after researching the internet on how to put dynamic range for filters in excel considering a situation when your list might grow or shrink I stumbled on couple of ways. One of them is as the following.
ActiveSheet.Range("A1").AutoFilter Field:=46, Criteria1:="1"
I know the you can put two arguments to specify ranges and the second one is optional. In this particular I am just putting the first cell where my data starts. My questions are below:
does the code automatically select the active range based on the first specified cell?
Had my data started from "C5" would i still have to chose the argument as "A1"?
I am filtering out something from column 46. what does it matter where I specify my first cell? for example if I had chosen "C5" instead of "A1" how it would have affected my macro?
Let's say, I want to filter out something from column 2 ie, Field 2. But the argument for range I choose as "D5". How it would affect my macro code?
AutoFilter: A Small Study
Well, I was curious, too. So I studied it a little bit and created a
workbook where you can input the parameters of some of the arguments
of the AutoFilter method and just by clicking on a command button
apply the changes. The next click removes the filter.
The workbook also contains the results of the behavior when changing
by row. The same thing could be done by column.
Workbook
Download
(Dropbox)
The following image displays the initial state of the only worksheet in the workbook.
This I was most curious about: what would happen if I would filter in C8. There's a surprise.
There still remains a ton of unanswered questions, but this could get you started to investigate deeper.
Related
I've found several sources that appear to give me a solution to my need, but each one has come up short. I think my solution is in using VBA UNION; however, I am a complete VBA noob, so I feel like I shooting in the dark. Here is my need.
I have a worksheet with multiple tabs.
Each tab has the same headers for the first 10 columns.
I'd like a sheet that is a summary of the first 10 columns of all other sheets combined.
I thought if I made each sheet a table and named each, I could create a range of ranges and then just call that combined range on the summary tab. My thought is there a solution somewhere with Union here, but I don't know enough to know if that's right or not.
I need basically what this solution is, but rather than it running on a run command and doing a copy/paste, the result just needs to dynamically update. https://danwagner.co/how-to-combine-data-from-multiple-sheets-into-a-single-sheet/
As a Google Sheets user, this is super simple, but I have to use Excel for this. I feel like there must be a simple solution that I am just completely missing. In Sheets I'd have just done ={range1;range2;range3;etc} and I'd have had my output, if that helps someone get what I need.
Any help here is very much appreciated.
Part of what is great about VBA is that it is an event-driven language, so you can set your code to run every time a cell is changed, workbook is saved, etc. instead of having to press a button. I recommend reading up to section 3.1 of Excel VBA Events - An Easy Guide where it explains how you can use:
Private Sub Worksheet_Change(ByVal Target as Range)
CombineDataFromAllSheets
End Sub
I am looking to do two things with one macro:
1) Break external links.
2) Change formulas parts pointing to other sheets to values.
I can find macros to break external links, there are a few methods to do that, but I have a great issue with the second point.
For example - if I have such formula in my active worksheet:
"=K6*34+Sheet1!A1"
I would like it to be replaced by this:
"=K6*34+25"
So, Sheet1!A1 reference should be replaced with its underlying value.
I cannot provide any sort of half-decently reliable code. I cannot find anything on the web either, as everything relates to breaking links and things on a workbook level, not a worksheet.
Why do I need this? I want to "break" one of the worksheets in the workbook and send it via mail. I can do every part of the macro except reliably finding and replacing references with values.
Ok, so you should define a load of names for cells that 'export' a value off a sheet. Then when you want to breakaway then you loop through the names and replace with a constant.
So this way you won't need to parse the formulae which (as has been pointed out) would be very difficult.
I have started to build a couple of workbooks that I would like to communicate with each other. My goal is to take entries from column1 # midnight,6am,noon, 6pm, and 11pm in workbook1 and 'copy/paste' them to row1 with the same labels in workbook 2. My main question is do I need VBA for this? My research leads me to believe that I do. However, I know that if I am doing this between spreadsheets in the same workbook I don't. I just use code such as ='Sht1'!A1 on spreadsheet 2 and it will copy and paste that entry from spreadsheet 1, so is there a similar way to do so with workbooks? Thanks for any input!
I figured it out. INDEX function was correct, at least in this instance. The correct formula was not far off because I had my row and column messed up, so the answer was:
=INDEX('[May 2018.xlsm]1st'!$A$13:$D13,1,3)
To break it down for people that do not want to use VBA and struggle with coding like me :)
INDEX() is my function
'[May 2018.xlsm]1st'! is the name of the workbook and sheet that I want to reference and ! means that the workbook is macro-enabled
$A$13 is the beginning of my range (A,13)
$D$13 is the end of my range (D,13)
So my range is 1 row and 4 columns (Think of it like a table), yours could be whatever you want (2x4 or 6x6 or whatever size you need)
1 is the row in the range that I want to reference
3 is the column in the range that I want reference
Be sure to close the function with )
I turned out making this more complicated than it should have been, because I had merged cells and I was technically referencing the right row, but not the right column. I was using (1,2) as my row and column which was referencing my first cell.
Hope this helps.
EDIT: I found later that this only works for referencing static data, so if you use it for a template or another spreadsheet that gets updates, this is not the answer for that situation. I will update again if I figure it out.
EDIT 2: For this to work with a updating spreadsheet, do the following.
=('[June''18 7Q10.xlsm]1st'!$C$13:$D$13)
Its actually much simpler to perform than I originally made it out, but going back to my beginner programming classes I wanted to make sure I understand why it worked not only how to perform it.
The process via LINK is:
Open both workbooks (Source workbook = 1 and Updated = 2)
Find the cell you want to have the data 'Pasted' TO in (2) and edit it with =(.
Now go to (1) and click the cell the that you want to 'Copy' data FROM. This will generate a reference point for you.
Now go back to (2) and finish the function out by closing the argument with ).
Finally, simultaneously press Shift, Control, and Enter and the spreadsheets/workbooks will link, likely leaving you with your result or a 0.0 until data is updated.
So here is what I want to do in VBA:
Find the value in E10, copy the row it is in, paste the row somewhere else (doesn't matter as long as I can find it) then finding the next row with the value E10 and doing the same thing, etc, until I reach the end of the spreadsheet
I'm really new to VBA and macros in general, so any help would be appreciated!
Generally speaking you'll find that asking questions without explaining "what have you tried" tends to be frowned upon in these parts.
I suggest that you take a look at this somewhat similar question that I answered just earlier today:
Copy & Paste row of data in Excel 2003 to different sheet by Email search
The principal differences between that one and your need are:
Instead of an InputBox, you'll use the value in E10 to run the .Find method; and
You need to create a loop. You still run the .Find method as shown in my example but you do it in a loop until you've looped through every instance of the value in E10 in the sheet. (Get the address of the first range that you find, and keep running the .Find method until you get back to that address.) Look up the While statement and the For statement in VBA help for examples on how to create a loop.
To get the value of an individual cell one way is just to create a reference to the cell range, then query the .Value property. For example a fully qualified reference could be:
Thisworkbook.Worksheets("Sheet1").Range("E10").value
There are other syntaxes which will yield the same result, but you can learn about those in time. Recording your actions using the Macro Recorder is a good way to learn about properties and methods. The problem is that in yet another part of the Excel 2010 interface that I detest, Microsoft has decided to hide the thing. To get to the Macro editor, go to File -> Options -> Customise Ribbon and turn on the Developer tab.
Give all of that a try and if you get stuck with something specific, post again with the details.
I'm trying to find a way to from a Cell get the data from a cell in the Sheet that lies to the Left (down in the tray) of the current Sheet.
I know how to call to other sheets via
=Sheet1!A1
But now I need something best explained with
=Sheet[-1]!A1
Any ideas?
Using the tab order as a fundamental part of your calculations is a complicated and risky approach to Excel calculations. Excel offers many alternatives which you'd be better off using:
A simplified version of belisarius's suggestion is: =INDIRECT(A1 & "!A2") where cell A1 has the name of your datasource sheet and A2 has the name of your target cell in your datasource sheet. If you know the name of your sheet of interest (or can look it up in some way), use this method.
If you need to do this often, you might want to export the data into an actual database (i.e. MS Access). Then you can make standard SQL queries and import the results into your Excel file.
If you absolutely want to go the VBA route, then you'd have to write some code that:
3a. Grabs all the names of the active workbook and stores them in an array.
3b. Identifies the index number of the currently active workbook in that array. Subtract 1 from that index to get the sheet to the left.
3c. Gets the cell value from that sheet.
You can also get freaky with Named Ranges. In Excel 2003, go to Insert->Name->Define, add a new Named Range and you can use that name in your calculations instead of referring to the cell by row and column.
Edit
The whole Idea with this one, is that
you have the Sheets arranged, and are
able to move them around, and that
shall change the calculations. – Gnutt
1 hour ago
Please, please, don't do that. For starters, this isn't a standard method of interaction with a spreadsheet. Your end-users will likely be confused and may not even ask for clarification.
You'll want to explore the idea of data validation:
Using Data->Validation, make a drop-down menu listing all the sheets in the workbook (if the names of all the sheets are static, you can just hardcode them, otherwise, you'll need some VBA to pull them).
Then the user just picks the sheet of their choice and indirect() will automatically update everything.
Alternatively, you can also check out Tools->Scenarios. I don't know anybody who uses this feature, but you might be a good candidate for it. Basically, it lets you see the results of calculations using different datasets (i.e. "scenarios") so the user can go back and forth between them.
Using either of the 2 methods above, there's a good chance you can avoid VBA entirely, thus saving users that annoying warning message when they open your file.
=INDIRECT("Sheet"&TEXT(VALUE(MID(CELL("filename",A8),FIND("]",CELL("filename",A8))+1,256))-1,"#")&"!A1")
Caveats:
Your workbook must be saved previously
A8 may be replaced by a reference to ANY non-error cell
I know it's not seen here as good practice, but I want to do something similar. And it does replicate database functionality to an extent but I don't have the time or support to build one from scratch when there's something already half in place.
The reason I want to be able to do this is to create a summary table that links to all the worksheets in the workbook, and automatically extends if you insert a new worksheet. This is to manage a large sales / reporting spreadsheet with lots of different business units that all have the same structure (ie use the same worksheet format to report the same outcomes for different people. There is a high turnover. I want to have several summary sheets reporting different aspects of the source sheets. This is very time consuming to manage if recreating all of the tables each time.
You should be able to use the row() as an index marker to define the information that you want using something like REPLACE, OFFSET or INDEX but you can't as they only refer to 2D arrays.
Whereas Excel treats 3-D references as arrays for statistical functions it does not seem to do the same for reference functions. You might have SUM(sheetX:sheetY!A1) and be able to add a sheet in between, there is not (eg) a INDEX(sheetX:sheetY!A1,n) function. I've tried experimenting using these 2D functions as part of array formulas, and defining the 3D reference as an array or a named range... well it was worth a go :).
So I believe it's a valid action. I also believe there must be a way to do it, but for now I'm falling back on a UDF that has the risk of errors caused by calculation issues, or manipulating a Workbook_SheetChange function or similar. Or creating a single master sheet to control all the others which is populated by using a subroutine based on an array of all workbooks.
these functions work well for me. They get the worksheet index (the parent of
the range you call them with), add or subtract from that index, and then create
a range from that (relative) sheet and the address passed in.
Function relativeSheet(r As Range, iRelative As Integer)
Application.Volatile
relativeSheet = Sheets(r.Cells(1, 1).Parent.Index + iRelative).Range(r.Address)
End Function
Function prevSheet(r As Range)
prevSheet = relativeSheet(r, -1)
End Function
Function nextSheet(r As Range)
nextSheet = relativeSheet(r, 1)
End Function