macros and vba
6520 TopicsLearning Macro Control Buttons
My goal is to embed two control buttons that pop up when a certain range of cells is selected. I have two columns (C:D) that have numeric entries for quantities of items. I want to be able to select a cell in this range and have two buttons appear next to active cell, one to add by an input value and one to subtract by an input value. I was able to create Form Control Buttons that can do the task but I can't figure out how to align them to the active cell and hide them if the active cell is outside of the desired range. Any suggestions? Do I need to use ActiveX buttons instead of Form Control?60Views1like1CommentInitializing TextBoxes within a UserForm
My spreadsheet contains a macro which utilizes two UserForms, each containing several TextBoxes. I want to initialize several TextBoxes in each Userform. One UserForm works and the other one does not, even though I have set the code up identically in each one. Here is what I have detected: When you step through the macro code (F8), when the first UserForm.Show statement is executed, the control skips to the first line of code for the UserForm and when the second UserForm.Show is executed, the control skips to the first TextBox on the Object. Here is a sample of the code I am using: Private Sub UserForm1() TextBox1.Value = "ABCDE" End Sub I have been writing macros like this for over 30 years, but this is the first time I have run into this situation. Any help here will be greatly appreciated. Al60Views0likes3CommentsTrying to insert Pivot Table
I recorded a new macro, using the tab View Macros-Record Macro, which resulted in the following VBA code: Sub CreatePivotTable() Sheets("Year Data").Select Range("A3:F24").Select ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Year Data!R3C1:R24C6", Version:=xlPivotTableVersion14).CreatePivotTable _ TableDestination:="Yearly Summary!R3C1", TableName:="PivotTable3", _ DefaultVersion:=xlPivotTableVersion14 Sheets("Yearly Summary").Select Cells(3, 1).Select End Sub The recording did exactly as I wanted, taking the data from the "Year Data" WS and adding the new pivot table to the "Yearly Summary" WS. As a test of the macro, I deleted the initial pivot table that was created on the "Yearly Summary" WS. The problem I have now, when I attempt to run this macro again it constantly fails on the 4th line of the macro with Run-time error '5': Invalid procedure call or argument. ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Year Data!R3C1:R24C6", Version:=xlPivotTableVersion14).CreatePivotTable _ TableDestination:="Yearly Summary!R3C1", TableName:="PivotTable3", _ DefaultVersion:=xlPivotTableVersion14 I cannot figure out why it works when I am recording the macro but fails when I try to run it alone. Help is greatly appreciated.Solved101Views0likes2CommentsDate of Last Modified for a Cell
Good day, all. I am inquiring if there is an excel function that would give me a date (and time) that an individual cell was last modified. For example: cell A2 has an entry of $2.55 and I'd like cell A3 to read "12/5/22 at 10am". a week later I change the $2.55 to $3.00....I'd like the A3 cell to read "12/12/22 at 10:30am". I've seen some VBA solutions but wondering if there is a simple excel formula to accomplish this?277KViews4likes82CommentsHow to get all possible permutation in VBA
Hi, We all know Benjamin Franklins Square. How can I Get VBA to export a spreadsheet with all possible permutations for numbers 1-9. Filter out duplicates of the same number Filter out only combinations that the sum matches the criteria below First 3 digits = 15 Second 3 digits = 15 Third 3 digits = 15 1st, 4th, 7th = 15 2nd, 5th, 8th = 15 3rd, 6th, 9th = 15 1st, 5th, 9th = 15 3rd, 5th, 7th = 15 I hope you are following what I'm trying to do. Thanks in advance, Uncle BearSolved174Views0likes5CommentsTrouble Setting and Clearing Filters
Hello all. I'm on Day #2 of my VBA experience. I'm working on a spreadsheet in which I'm using a series of command buttons to filter the content my data. I have the following three subroutines - each associated with an individual command button. (ok ... I lied, there's about 20 buttons - each of which sets a different filter - but I can demonstrate my issue with just these three) When "Button A" is selected - it executes the following code: Sub xJitFilter1() Rows("14:14").Select Selection.AutoFilter ActiveSheet.Range("JitComp1").AutoFilter Field:=13, Criteria1:="1" End Sub When "Button B" is selected - it executes the following code: Sub xJitFilter2() Rows("14:14").Select Selection.AutoFilter ActiveSheet.Range("JitComp2").AutoFilter Field:=14, Criteria1:="1" End Sub When "Button C" is selected - it executes the following code: Sub ClearAndReturn() Rows("14:14").Select If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData End If Selection.AutoFilter Range("xJitLanding").Select End Sub It works just like I want it to - IF I select the buttons in a specific order. For example: Button A --> Button C --> Button B Works perfectly However, IF I select buttons in a different order (i.e.,) Button B --> Button C Crash and burn: Run Time Error 1004: Autofilter Method of Range Class failed If I selection Button C (i.e., ClearAndReturn) when first opening the spreadsheet - prior to selecting one of the "Set Filter" buttons (i.e., Button A or Button B) - it's crash and burn with the same Run Time Error. Bottom LIne: IF I select any of my 20+ "Set Filter" buttons - and followed by the "ClearAndReturn" sub ... I can bounce between filters all day long. The "ClearAndReturn" sub without having selected a "Set Filter" button or try to go from one "Set Filter" button to another ... instand fail. I assume I'm failing to reset something in the environment ... but darned if I know what! Can anybody point me in the right direction? Thanks - SpaceNorman68Views0likes1CommentAdd VBA Code to an Option button dynamically
Hello Excelers, I hope you are doing well. I am back for some more help and here is what I am trying to accomplish: I have some code that would add x Option Buttons to a User form. x is Unknown but usually less than 15 but maybe more at times. I need to assign a macro that would return the newly created Option Button's Caption. In a Module I have: Dim objOptBtn as MSForms.OptionButton In a For i = 1 to x loop I have: Set objOptBtn = uFrmChoice.controls.add("Forms.OptionButton.1","objOptBtn" & i,true) With objOptBtn .Caption = i .snglTextWidth = Len(objOptBtn.caption) .width = snglTextWidth + 18 .Left = 10 .Top = TopLevel + 10 .Width = 400 .Height = 18 End With I remember there was an .OnAction "MacroNameYouWantToRunWhenOptionButtonIsClicked" But I do not see it in the Intelisense???? How can I assign code to run once the user clicks on a Option Button on the UserForm and return the Caption of that Option button? I am trying to ask the user to choose ONE option from a set of unknown options.73Views0likes1CommentHow does Excel calculate named formulas and materialized named ranges?
I have been creating utility formulas with AFE to make my lambdas more readable and maintainable, for example: ManualAllocRng =OFFSET(ManualAllocCorner,1,0,URows,EntityCount); How does Excel calculate these results? Are they calculated every time I use them, or is there an internal type of materialization that happens once and then is re-referenced on each usage? In particular, I am wondering: (1) If I have a lambda that references my ManualAllocRng twice, does it matter if I write it as thing = LAMBDA(x,LET( rng, ManualAllocRng, a, something(rng), b, otherthing(rng), res, combine(a,b,x), res )); or as thing = LAMBDA(x,LET( a, something(ManualAllocRng), b, otherthing(ManualAllocRng), res, combine(a,b,x), res )); Does version 2 calculate the result twice? Does version 1? (2) Should I instead be materializing the value in a Calcs sheet, naming the corner ManualAllocResMaterialized, and using ManualAllocResMaterialized# throughout the workbook instead of referencing ManualAllocRes? Does every reference to the named formula calculate its result again? Does every reference to a spill recalculate the result, or does it "look" at the spilled range that was already calculated? On a related note, I have found a circumstance where sheet-based spilled result =Ledger.FilterMatch produces a different result from VBA Dim B as Variant B = Evaluate("=Ledger.FilterMatch") This seems like a serious bug. So I am considering materializing all my AFE-based named formulas onto a Calcs sheet just to avoid this bug, even if there is no performance issue to consider in the questions I asked earlier.220Views0likes6CommentsCan I control what is displayed in a FileDialog
Hello Excelers, Hope you are all doing well. Is there an option to limit the files displayed when you use something like: With Application.FileDialog(msoFileDialogFilePciker) .InitialFileName = "Book1" .InitialView = msoFileDialogViewDetails .Filters.Add "Excel Files, *.xls*" .Title = "Choose a File" (A way to limit files with a date range of say 7 days ago till yesterday .Show End With I want to limit the files displayed by a date of 7 days ago to yesterday. I know I can use the DateLastModified of a file but the question is how to incorporate this with the msoFileDialogFilePciker. Thanks in Advance! GiGiSolved126Views0likes2CommentsVBA code to allow dropdown box multiple selection
Hi all, Is there an option to allow multiple selections of a dropdown box within excel? the dropdown would be in column V (v2 downwards, and the list is in tab “Entities” from a2 down to a6. the codes I have tried from online either didn’t allow me to remove a selected item, or when I did remove it and try to select others I ended up with “floating” commas. Ie it went from “apple, pear, banana” to “,pear ,” and I couldn’t remove the commas. thanksSolved103Views1like1Comment