I'm trying to create and run a pretty simple macro in Excel 2016, but I keep getting the following error message "Compile Error: Invalid Outside Procedure.
All the macro is trying to do is when I click a button on one worksheet, the macro moves to another worksheet, and amends a filter on a particular column (and removes the zero's from the column).
I've recorded the macro to do this, but when I assign it to a button and try to run it, the error keeps coming up.
The code shown in the VBA tab is as follows:
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Can someone help as it's driving me nuts!
Thank you
Half code (or incomplete code) is like half-truth, difficult to understand or judge. So, if I understand your question here's the (probable) solution.
1. On the button click method, write the sheet change code (e.g. Sheets("books").Select)
2. Once the intended sheet is selected, select the column you want to apply filter on by simply recording the macro.
3. Then do the rest of the tasks.
These are simple steps. If you still don't succeed then please revert.
It could be just as easy as deleting the first 'End Sub' in your code.
w1n5rx seems to be correct with completing your code to perfom what you're describing.
But maybe other options to try:
Maybe try the active controlx button <--- or however its labeled.
or try
Static Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Related
So I've set up this spreadsheet at work - I end up having to do a lot of fiddly Excel tasks, though I barely know any VBA - and I want a table to automatically filter itself after the worksheet has been edited. The problem is that the column which is being filtered is full of formulas, which change in response to edits made by the user, and the filter gets applied before the column has finished calculating, producing erratic results. So really what I want is to apply AutoFilter after the worksheet has been calculated, rather than after it has been edited.
The macro I have been using is:
Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveWorkbook.Worksheets("Library").ListObjects("Library")
.AutoFilter.ApplyFilter
End With
End Sub
Naively changing the activating event to Worksheet_Calculate() doesn't work - it appears to be repeatedly applying the filter over and over again.
I'm sure this is a relatively simple one and I just don't know what it is I need to do. Can you guys advise?
(PS first time posting here - hope I have done everything right!)
Never mind, have solved it myself. For anyone else with the problem, I just set calculation to manual and then replaced my code with:
Private Sub Worksheet_Change(ByVal Target As Range)
Calculate
ActiveWorkbook.Worksheets("Library").ListObjects("Library").AutoFilter.ApplyFilter
End Sub
I'm trying to program a Chess-Game in VBA. I'd like to change the position of the figures with mousemove. I can already move them but It would be great if I could release the figure and the figure so jumped into the middle of the field.
I have no Idea how do solve the Problem
Thanks in advance
PS: Sorry for my bad english
Best case scenario - use the macro recorder. Copy the figure and paste it somewhere.
See the code. Then make sure that it works, only in the given range of the chessboard. Then make an event like this:
Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A1:H8")) Is Nothing Then Exit Sub
Debug.Print Target.Address
End Sub
And try to build the recorded code in Target.Address. The event is for the right click of the mouse. It should be put in the Worksheet part of the VBEditor.
I am trying to create a macro for Word 2016 that will automatically edit a document based on a list of about 250 search terms that I used the "find and replace" function to change. When I record the macro from start to finish and attempt to run, I receive a message saying "Procedure is too long". I have looked through answers on here and understand that you can clean up some code and/or create sub sections within the code to troubleshoot. However, I am not sure what I am doing and the code is very long.
Could someone help me please?
portion of code is included below:
Portion of code
Break your macro into steps. The easiest way is (use a copy of your document for testing, so you can always put the original text back if you make a mistake!):
Record a short macro. It doesn't matter what it does. Name it something like DoAllReplacements.
Record a macro that does your first block of replacements. Name it something like DoReplacement1.
View the macros, and edit the DoReplacement1 macro. Copy all of the text from the end of the comment block to the line just before End Sub.
Put the edit caret (cursor) at the end of the DoReplacement1 End Sub and hit enter twice. Enter the following text:
Sub DoReplacement2()
End Sub
Paste the code from the clipboard between the Sub and End Sub lines you just added. Edit the macro code to do your next block of replacements.
Repeat the above until you have all of your replacements coded, using as many new DoReplacementX routines as needed.
Go back to the very first macro you recorded (DoAllReplacements), and remove all of the code between the Sub DoAllReplacements() and End Sub. Add new calls to the individual DoReplacementX macros you wrote.
Sub DoAllReplacements()
DoReplacement1
DoReplacement2
DoReplacement3
' Etc. until you've added them all
End Sub
Exit the macros code window. Use View Macros, select your DoAllReplacements macro and execute it.
I have two macros that I want to work when I push a button. I inserted the code needed in the button sub and made it so that it is not private. Then I opened the macro dialog box and ran the macro and it worked as expected. But, when I try to press the button, it comes up with an error that says "the requested member of the collection does not exist."
The code shouldn't really matter in this case since it is working correctly when I run it manually but here it is:
Sub CommandButton4_Click()
Selection.Tables(1).Select
Selection.Copy
Selection.PasteAndFormat (wdPasteDefault)
ActiveWindow.ActivePane.VerticalPercentScrolled = 24
'Some other Code here
End Sub
When I perform a debug, it says that the problem is in this line: Selection.Tables(1).Select
Then in my other instance I have this code:
Sub CommandButton3_Click()
Selection.SelectRow
Selection.Copy
Selection.InsertRowsBelow 1
Selection.Paste
End Sub
The code error appears to be in this line:
Selection.SelectRow
It says
The SelectRow method or property is not available because some or all of the object does not refer to a table.
I got the code by using the Macro Recorder.
If you have any ideas as to why this is happening, I would appreciate your help.
This code
Selection.Tables(1)
works only when the Selection (the cursor) is inside a table.
When you click the button, the button gets the focus, and the Selection is moved there. If the button is not inside a table, the code will fail.
It does work if you move the button inside the table, but that would probably look silly.
Replace your
Selection.SelectRow
with
'r being which ever row you want to Select
Selection.Table(1).Row(r).Select
You can bind your macro to a key combination. This won't change any focus you've set, and thus should solve your problem.
How to do this differs among different versions of Word, but the recent versions are explained here:
http://wordribbon.tips.net/T008058_Assigning_a_Macro_to_a_Shortcut_Key.html
Let me preface by saying that I am very new to VB...
I am trying to run a macro whenever the value in a certain cell changes. I've read up on how to do this, but can't seem to get it to work. I have entered the following code into the private module of the worksheet object:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
Application.Run _
"'Amex Payments_Experiment.xlsm'!SelectCells"
End If
End Sub
C5 is the cell I am trying to monitor for change.
"SelectCells" is the macro I want to run.
"Amex Payments_Experiment.xlsm" is the name of the file.
When I change the value in C5 nothing happens. Some help would be great. Thanks!
UPDATE:
Cyberkiwi - No, that is not exactly how I did it, but when I follow you're instructions I do find the code where you say it should be. To get to the private module of the worksheet object I right clicked the sheet tab at the bottom, selected "view code", then selected "worksheet" from the dropdown in the top center of the page.
User587834 - Yes. Macro's are enabled.
Any other suggestions?
if you use Excel2007 be sure that macros are enabled, by default Excel 2007 deactivate macro execution for new workbook.
for that try to execute any other macro to be sure that macros are enabled.
I have entered the following code into the private module of the worksheet object:
How exactly have you done that? As below?
Alt-F11 to switch to code view
On the left, double-click on the target sheet
On the right, do you see the code you entered? if not, proceed to next step
Paste the code block
This code works Ok for me
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
MsgBox "hello"
End If
End Sub
Maybe the problem is with your Application.Run:
- select the line containing the intersect and click F9 to switch on Debug, then try changing a cell to see if you get to the sub.
If you never reach that line then you have not got the code in the worksheet clkass module, or you have switched off events, or macros are disabled or ...
Check the Application.EnableEvents property, and set it to True if required.