Selecton.Rows.AutoFit does not work - why? - vba

I have a problem with a piece of VBA code:
Sub Macro3()
Sheets("Output").Select
Rows("5:160").Select
Selection.Rows.AutoFit
End Sub
What - from my point of view it should do - is applying Autosize to Rows 5:160 within the Sheet "Output". I added the code to the sheet "Output" so whenever I open it it should be properly resized automatically. However, nothing happens. But if I manually select the rows and press CRTL+C+H+O+A to auto size the rows it works properly.
Does anyone has a guess where the mistake lies? Would be a huge help!
Thanks upfront!

whenever I open it it should be properly resized automatically.
For this you have to place the code in the ThisWorkbook code area and you have to use the Workbook_Open() event
See this
Private Sub Workbook_Open()
Sheets("Output").Rows("5:160").Rows.AutoFit
End Sub
SNAPSHOT

Related

Automatic execution of Macro based on cell change

I'm trying to run a macro every, time the selection of a dropdown menu in a cell changes. I've tried to follow this solution: automatically execute an Excel macro on a cell change
by entering the following code:
Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then Call Testsub
End Sub
Public Sub Testsub()
MsgBox "Testmessage", , "Testbox"
End Sub
Cell A1 contains the Dropdown menu that should trigger the macro "Testsub". However, nothing happens when I change the selection. I'd be very grateful for any idea, what might cause the problem.
The comment of user11138753 nailed it. I edited the code at the wrong place.
Thank you very much!

how do I change the position of my image with coordinate ranges in vba

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.

Why will my macro only work when I run it from the macro dialog box?

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

VBA routines not tied to objects in Powerpoint

I'm sorry for the newbie question, but I simply can't get this to work.
I've attached plenty of callbacks to powerpoint objects so that I can change things about them when they are clicked.
Private Sub CommandButton1_Click()
ActivePresentation.Slides(1).Shapes("Grey Box").ZOrder msoSendToBack
ActivePresentation.Slides(1).Shapes("Suggest Box").ZOrder msoSendToBack
CommandButton1.Visible = False
End Sub
This Works just fine. Breakpoints activate, code works.
I want to have some code called when the slide changes. I'd also like to have some code called when the presentation starts. There is plenty of advice on this topic, and I can't get any of it to work - probably because of the same mistake or an assumption I am making.
I have copied the following code into Module 1:
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If Wn.View.CurrentShowPosition = 3 Then
'Perform Updates for slide #3
Shapes("TextBox 51").ZOrder msoBringToFront
End If
End Sub
Sub OnSlideShowPageChange()
Dim i As Integer
i = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
If i <> 1 Then Exit Sub
MsgBox "Insert your code here"
End Sub
I've riddled this with breakpoints..... code is never called.
I have two questions:
How can I get either of these routines to be called when a slide changes?
Is there a good list of these automatic events somewhere?
Thanks,
Grommit
Finally figured this out..... thought I'd answer my own question in case anyone else runs into this.
I had 2 powerpoint presentations open at once. I WAS adding the code to the correct one, because when I closed both and reopened the one with the code, it worked immediately.
Not sure what on earth was going on, but it works now.

How can I run a Macro when the value in a specific cell changes?

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.