I'm creating a button with code
Set CreateButton = ActiveSheet.Buttons.Add _
(C.Left, C.Top, C.Width, C.Height)
where C is Cell object. Button appears not in cell but little upper.
CreateButton.Top is 107.25. And when I try to assign CreateButton.Top to itself, value is 114.75. What is going on?
It appears to be related to the margins. I changed it around and got the button to move its relative position by changing the top margin. Like you it only happened when adding the button while in page layout mode. In normal mode everything worked regardless of the margin.
You may be able to compensate with that information.
Related
Hope somebody can help me with this Excel/VBA issue.
I have recently started replacing my active X controls with shape controls hoping for less issues and better performance, however I've stumbled across a strange issue when trying to accept user input from a shape textbox by clicking on a shape button.
I've setup a simple worksheet to demonstrate the problem, it simply has the following shapes;
Shape textbox used for input, I have locked the shape and unlocked the text.
Shape button with text for the button name, I have locked the shaped and locked the text.
When the button is clicked and the macro runs it copies the text from the textbox to cell A1
Note: I have protected the sheet except for cell A1
I want the user to input some text into the textbox then immediately click on the button to accept the input, the button will run a macro which will do something with the text.
I've discovered the macro only works when the outside area of the button is clicked, it won't work when the user clicks near the text which is embedded on the button and takes up most of the center area of that button. Also when I hover over the button the mouse pointer changes to a move pointer where text isn't present and for the area with the text it changes to a text pointer.
If I click out of the input text box first into a worksheet cell before clicking the button then it works as expected and the mouse pointer shows the expected link pointer.
My test example
Below is the code assigned for the button macro;
Sub Test()
Range("A1") = Sheet1.Shapes("TextBox 1").TextFrame.Characters.Text
End Sub
Any help would be greatly appreciated
I have a very wide excel spreadsheet. It's formatted in such a way that there is specific data on each screen page. I have the first column frozen so I can place hyperlink buttons vertically. These buttons are hyperlinked to each of the formatted pages in the worksheet.
When I click one of the hyperlink buttons I would like the worksheet to scroll so the target cell in the UPPER LEFT corner of the screen.
I have googled and found this VBA code to place in the "thisworkbook" section of the VBA editor.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveWindow.ScrollRow = ActiveCell.Row
ActiveWindow.ScrollColumn = ActiveCell.Column
End Sub
This code does not seem to work for me. If I'm at the far left of the screen and press the hyperlink button it scrolls to the target cell until it gets on the active screen. So basically it always ends up somewhere on the Upper right of the screen. However, if I click the hyperlink buttons the require the page to scroll to the left side of the worksheet it does work.
How can I make it so the target cell is ALWAYS in the upper left of screen after I press the hyperlink button?
Also, how do I know the above code is even running? The hyperlinks seem to be doing the same thing with or without it. I just cut and pasted the code into the VBA editor (per google) for thisworkbook. Is that it or do I need to check something so it will be activated?
Thanks in adavance.
There's a way to do that without using VBA. You can just use a normal hyperlink in the spreadsheet, and link it to "Place in This Document". Make sure you pick the correct Sheet and Type the cell reference that you want to go to (I assume this is A1). This will make sure that whenever you click that particular link, it will always go to that particular spot.
Here's how the edit hyperlink would look in an empty spreadsheet.
I have an Excel Sheet with some macros. Also, I have some navigation shapes and images on my sheet. I want users of this sheet, cannot change this shapes and images positions, can't select them and can't move them.
Is there any way to Protect some specific objects?
Regards.
I believe this is an age old question ever since Shapes were added to MS Excel and the answer to which I myself was looking for many years already.
I just found out 3 days ago, on my own, how to lock MS Excel Freeform shapes like Choropleth Map shapes from being moved around, reformatted or worst, deleted, all WITHOUT needing to lock the WorkSheet, or eventually the WorkBook.
And I think I must share my discovery with the world because everyone wants to lock their Shapes!
Steps: (I work with Excel VBA and with msoFreeform shapes mostly but I think any shape should be working and manually added shapes through Excel UI should work too and in other Office Apps as well)
1.(yourWorkSheet or )ActiveSheet.Shapes.AddChart (through VBA but MAYBE you can add chart through Excel UI and then delete the only chart)
(no need for any other parameter because we just need the chart container, "ChartObject")
2.If you don't have some already, create a shape either through Excel UI or through VBA with AddShape method or BuildFreeform on the Chart directly or on to the worksheet.
3.Copy/Paste the created shape (if created through Excel UI or through VBA on the worksheet) on to the BLANK Chart Container. (NOT drag and drop)
4.Format the ChartContainer Rectangle window as required (try "No Fill & No Outline") through VBA or Excel UI
5.There are 3 options regarding protection of a Chart
(Embedded chart here because I don't work with Chart Sheets, may be this might work with them)
source:[https://peltiertech.com/Excel/ChartsHowTo/ChartProtection.html]
But here, only the relevant 2 will be shown:
5(a)ActiveChart.ProtectFormatting = True
That protection will block any formatting changes on the Shape and the Chart via "Chart Tools Menu" or "Drawing Tools - Format Menu" or moving or resizing with mouse or deleting BUT will show the ChartContainer window upon Selection via Mouse but non-selectable via VBA
eg. yourworksheet.ChartObjects("YourChartName").Chart.ProtectFormatting=True
5(b)ActiveChart.ProtectSelection = True
That will stop the shape or the chart from being selected altogether so this is the end of story
eg. yourworksheet.ChartObjects("YourChartName").Chart.ProtectSelection=True
The best thing about this method is that the shape can still be accessible through VBA like
eg.yourworksheet.ChartObjects("YourChartName").Chart.Shapes("YourShapeName or Index").whatever
except Shape.Select which should be obvious and there is NO need to lock the Worksheet or Workbook at all.
NB:1)The interesting finding here is that the 2 protections do not replace each other (if applied one after another) but more like stacked with each other meaning if both (if you really want) were set True first and then after setting either one False, the other restriction still remains.
2)Even if selection is protected as above, the chart can still be accessible through the Selection Pane, therefore:
Application.CommandBars("Selection and Visibility").Enabled = False
and also blocking the Worksheet Export are advisable but I think these are overkill nonetheless included for completeness' sake.
Discovered and tested on MS Excel 2010 so YMMV.
Nay Lynn's answer caused my Excel to crash. Specifically with the .ProtectSelection property being enabled (using Microsoft365 Excel). . . Excel VBA's Intellisense shows the property, so it is legitimate, but everything would be fine until the chart was selected. Playing around with this idea though, I did find a great work around.
1. Place a Chart on your sheet. Make it span across the area you want to protect
(we will expand the size of this chart later).
a. Leave Fill of Chart at Default until a later step (this helps ensure the
next steps are successful).
b. Delete all the elements present for the chart (Series label, Title, Etc.)
c. Shrink the "Plot Area" box leftover to as negligible as possible.
d. CRITICAL: Right Click the chart and choose "Select Data" - use the
option to remove all the data in the boxes of this dialog, otherwise when
you select the Rectangle added below, it will show the data references as
selections in the Worksheet behind it.
2. Insert a Rectangle into the Chart. It will have the default colors.
3. Tap `Esc` to clear the selection. If you select the shape and try to move it
around, it will take precedence over the Chart itself (this is how we'll trick
Excel later).
a. Confirm this does not allow the shape to be pulled beyond the border of the
Chart.
4. Set Fill of Chart to be "No Fill"
5. Expand the Rectangle's size to match the size of the chart.
6. Set Fill of Rectangle to be a color with 100% Transparency (NOT the same as "No
Fill" - this is critical)
a. You should be able to see all of your shapes, etc, but not touch them if you
click where the rectangle was, and trying to click and drag will also fail since the
Rectangle takes selection precedence and is also bound by the chart.
7. Set the fill of the Rectangle back to a color with 0 transparency (we need to
find the edge of the shape)
8. Select the Rectangle's border and it should then select the Chart instead (you
should see the chart Format option appear).
9. Expand the Chart's border to the ends of the Excel Sheet (ensure the top left
corner sits in the top left corner of the sheet by dragging it). You can also set
the height/width to an absurd number that users will get fed up with trying to find
if you do not wish to expand it across the entire boundaries (or if there's a hard
limitation - I did not try to expand across the entire sheet, but went to "BO400"
with no issue.
a. Expand the Rectangle to fit the Chart once again if it did not expand
automatically (in my case it did, but I cannot guarantee this behavior)
b. If you have any Buttons that DO need to be clicked, place this chart
at the bottom, place all the buttons/shapes etc. that need interaction to
the top, then bring the chart up a level until all the items needing
protection are hidden.
c. Change the Transparency back to 100%
10. After you have the Chart expanded properly, you will need to use a bit of VBA
to ensure the Chart's Formatting is protected as Nay Lynn mentions. Get the
Chart's name by selecting the border, and institute some VBA Code that gets
toggled based on your needs (you might want to include an unprotect sub
as well just in case).
Example:
Sub Protect_Sheet_With_Chart ()
dim sht as Worksheet
dim chrt as Chart
Set sht = ActiveSheet
'You can use a sheet by name for the above as well - make your code robust.
Set chrt = sht.ChartObjects("ChartNameFoundFromStep10").Chart
chrt.ProtectFormatting = True
End Sub
Sub UnProtect_Sheet_With_Chart ()
dim sht as Worksheet
dim chrt as Chart
Set sht = ActiveSheet
'You can use a sheet by name for the above as well - make your code robust.
Set chrt = sht.ChartObjects("ChartNameFoundFromStep10").Chart
chrt.ProtectFormatting = False
End Sub
11. After you protect the Chart, selecting it and deleting will not
actually delete it, NOR the rectangle, so it can't be removed!
12. Protect your code somehow and you'll be set!
I'm creating a VBA UserForm in Excel 2010. I've added a bunch of textboxes like so:
Let's say I selected the top textbox and looked at its properties. The BorderStyle is set to 0-fmBorderStyleNone. Upon changing the BorderStyle to 1-fmBorderStyleSingle and then back to 0-fmBorderStyleNone, it loses its default style, like the top textbox shown below:
This is using the object. If I were to edit the BorderStyle using VBA code, the effect is the same.
Is there a way to get back the fully default BorderStyle of the textbox after changing the BorderStyle?
It's actually property SpecialEffect that controls to sunken look. Changing BorderStyle also affects SpecialEffect
To reset, change SpecialEffect to frmSpecialEffectSunken
I have a sheet, and a button on it (Excel forms button, not ActiveX).
Depending on current selection, the button is moved/resized or made visible/unvisible (handled by Worksheet_SelectionChange event).
All this was designed in Excel 2010 and worked fine until some end users reported errors while using the workbook in Excel 2007. The issue is, that when you copy/cut a range, and then change selection CutCopyMode is reset to False (floating frame around what was copied disappears). This does not happen in Excel 2010. I found out CutCopyMode is reset when I programatically modify button properties (top, height, left, visible, backcolor are modified each cancels CutCopyMode).
Do you have any ideas how to solve this?
Following the logic here, it seems that you will need to keep track of the range on the clipboard before modifying your button, and then recopying/recutting after modifying your button to restore the original range to the clipboard.