I have a text box which takes user input which is referenced by a number of custom functions in VBA. I wanted the functions to recalculate everytime the input in the text box is changed. The text box input represents a scale in the format 1:24 which is used in the functions and converted to a decimal fraction.
The code works well initially but then stopped working when I reopened it. If i add a message box after the calculate. I had the same issue with using SelectionChange for the worksheet. Thoughts? Here is the code that I used.
Also I am using a formatted table which is what is not updating.
Private Sub ScaleBox_Change()
Worksheets(ActiveSheet.Name).Calculate
End Sub
Your event handler ScaleBox_Change does not make any changes to the worksheet, so any calculation will be working on the old data.
To fix this, place the result from the ScaleBox and place it in the worksheet in the appropriate place (and in the right format for what you are trying to do). Then do the calculate routine.
Try something like:
Private Sub ScaleBox_Change()
Applicatin.CalculateFull '<<< edit
End Sub
If that doesn't do it then it would help to post your formulas.
Related
I am trying to automate a repetitive manual task for another department. They have a word template several pages long. They have to enter data several times each time they use it. Many of these are repetitive "fields". I thought about setting up custom form fields. But having them go through the navigation and know the field names to update them is not optimal. I found one solution to create a bookmark for each data input, and create links to that bookmark where it needs to be repeated - better but not optimal. I was hoping to create a userform where they can enter all the data and have it populate bookmarks, custom fields, to whatever.
I found a video that does this, but when I try the code, it does not update the bookmark. i created userform with Textbox1, and a CommandButton, and a bookmark in the document called MWDate. The code I am trying to get to work is:
Private Sub CommandButton1_Click()
Dim MWDate As Range
Set MWDate = ActiveDocument.Bookmarks("MWDate").Range
MWDate.Text = Me.TextBox1.Value
Me.Repaint
UserForm1.Hide
End Sub
This is a code example I grabbed and modified from the tutorial video. I don't know what the "Me." is. It does not populate the bookmark, which tells me it is not getting to the repaint line. Nor does it ever get to the hide line.
Anyone have a fix? Or a better way? Thanks for any help you can give.
(SW versions: Excel 2010 - Microsoft Date and Time Picker Control 6.0 (SP4))
In the sheet "sheet1" I have an ActiveX Date Picker control calendar named "foobarCalendar".
Next to it I put a button and in its "Assign Macro..." I want to specify a macro with as argument the date of this calendar "foobarCalendar".
In VBA I can get this date with "sheet1.foobarCalendar", however I'm unable to find any working way to put this as argument in the "Assign Macro..." window.
Is there a working syntax or it's simply not possible?
(another problem I encounter is that my macro name doesn't appear on the list, but I discovered that it's because Excel only display the macroes that don't get arguments in this list, although I can manually write them and use them successfully)
I tried for example 'macroName sheet1.foobarCalendar' but it's not working.
I also tried many other syntax, including those that work for passing named ranges and including the "evaluate" variants, without success. Is it possible?
In VBA I can get this date with "sheet1.foobarCalendar", however I'm unable to find any working way to put this as argument in the "Assign Macro..." window.
You don't. Sheet1.foobarCalendar is already in scope: have the macro use it.
Public Sub MyMacro()
Dim workingDate As Date
workingDate = Sheet1.foobarCalendar
'...
End Sub
That said, there is a way to invoke a parameterized macro, but as far as I could get things to work, it doesn't work with a variable argument, which appears to be required to be a hard-coded value - the syntax requires enclosing the "macro name" with single quotes, adding a space before the argument, like this:
'Test "test"'
Correctly invokes this macro:
Public Sub Test(ByVal value As String)
MsgBox value
End Sub
But, if a macro needs to use data that exists on the worksheet at the time of invocation, there's no need to parameterize it. And if a macro needs to prompt the user for a date, a path, or anything, ...then it can absolutely do that.
Note that the date picker control will likely not work properly on a 64-bit host: Consider using another way to prompt the user for a date, that doesn't involve 32-bit ActiveX tech.
I'm currently creating a workbook which has an input tab, all of it's data flowing through into later tabs. I want to prevent a user from moving onto any other tabs until all of the relavent information is filled in on the input sheet.
I'm currently trying to use the workbook_sheetactivate event but have spent a lot of time going between this and worksheet_change event, neither of which I can get working properly. Any help would be greatly appreciated.
If you don't want to use forms then I would suggest adding code similar to the following to each sheet:
Private Sub Worksheet_Activate()
If Worksheets("Sheet1").Range("A1").Value = "" Then
Worksheets("Sheet1").Select
End If
End Sub
Obviously you will need to change the sheet names, range and value but I'm sure you get the idea.
In a new excel spreadsheet, I have inserted a new module and entered a public sub. When trying to test, the sub does not appear in the macro list unless I remove the parameter.
This behaviour is bizarre and I cannot find any reference to it as an issue, everything I have read declares that subroutines (or functions, tried that too) can have parameters.
Public Sub RetrieveSIR() <-- Can be found
Public Sub RetrieveSIR( SIRNumber as Integer) <-- Cannot be found
It is driving me around the bend trying to work this out. If anyone can help, it would be much appreciated.
Subs with parameters won't show up in the macro list for the same reason you can't simply run a sub with parameters from the vba editor screen. They can only be called via code so the parameter required can be input.
Edit:
If for whatever reason you really need your macro to be in that macro list, you should make that parameter a variable in your macro and use an inputbox to specify it. That way when the user clicks the macro, they'll be prompted for the input and then the macro can run accordingly.
With regards to functions, you can have a function with parameters and use it as a formula in excel but as far as I'm aware they won't show up in the macro list either.
I know that I can write the following code in a sheets' VBA-object to run code on a worksheet change.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Is there anything similar that I can write to run code whenever I filter a certain ListObject?
Only in some cases. Say we include a new column in the table that we fill with the value 1. Elsewhere we insert an
=SUBTOTAL()
formula to sum that column. As the filter is operated the number of visible rows will vary. The SUBTOTAL() function will re-calculate.
At this point a Calculate Event macro would catch the re-calculation!
Try this...
I added a ListBox Form Control to my Excel sheet..
Next, I assign a macro to this object by Right Clicking and choosing "Assign Macro". (you may need to be in "Design Mode" to make this happen -- check the Developer Ribbon)
By default - the Macro Name is populated with a change event macro name. Click "New" to create the macro.
Your macro is added to a Module. Hope this helps!