how to prevent re sizing of buttons after release of auto filter in excel 2010 using vba - vba

I have an excel sheet with some buttons (each doing different functions) i have created an auto filter macro as shown below but the problem is when i release the filter my all buttons get very small in their sizes (means they change their original size) although i selected the radio button (Do not move or size with cell) from each button's property.
Sub AutoFilter()
Range("A1:I1628").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$I$1631").AutoFilter Field:=8, Criteria1:="="
Selection.Copy
Sheets("Blank Names").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
I am badly in need of help, please assist me with this issue.plzzzzzz

My guess is that you need to set the object positioning of the button to "Don't move or size with cells" (as seen here, unfortunately I don't have enough reputation to post images yet).
If you have many different buttons in many different spreadsheets, this code should set the property for all of them (provided none of the worksheets are protected, etc.)
Sub test()
Dim ws As Worksheet, sh As Shape
For Each ws In Worksheets
For Each sh In ws.Shapes
If sh.Type = msoFormControl Then
sh.Placement = xlFreeFloating
End If
Next
Next
End Sub

I faced the same issue and couldn't find a solution anywhere in the internet. Then on manual testing, I found that the issue is with the format.
Please clear the format from the range of cells where you're trying to put autofilter
For example, I copied data from source file and pasted the as values and then I put the autofilter.

Related

Mirroring Sheet1 to Sheet2 for Interior Color only, through VBA

I have a schedule showing a lot of information. I would like to condense this onto a second sheet that displays the fill color only and none of the values.
I want that any fill color changes are automatically copied from sheet1 to sheet2.
I want the code to work with a specific cell range as they differ from both sheets, (Sheet1 is "D8:QP27) & (Sheet2 is B3:QN22) and to get it to mirror at all.
Sheet1 showing all information
Sheet2 showing fill (Interior.Color)
It looks as if you also want to copy the borders (eg the diagonal border of column I), so I would suggest you use PasteSpecial with the option xlPasteFormats. See https://learn.microsoft.com/en-us/office/vba/api/excel.range.pastespecial
With ThisWorkbook
.Worksheets("Sheet1").Range("D8:QP27").Copy
.Worksheets("Sheet2").Range("B3:QN22").PasteSpecial xlPasteFormats
End With
Update: As you are looking for a trigger to copy the format automatically. First step is to create a subroutine:
Sub copyFormat()
Application.ScreenUpdating = False
With ThisWorkbook
.Worksheets("Sheet1").Range("D8:QP27").Copy
.Worksheets("Sheet2").Range("B3:QN22").PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
Application.ScreenUpdating = True
End Sub
Now you need to find a way to call the code, eg
o call the routine from the Worksheet_SelectionChange-event (drawback: as this is rather slow, it could annoy the user)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
copyFormat
End Sub
o Place a button on the sheet to call the routine.
o Use a Timer to call the routine every n seconds (see https://learn.microsoft.com/en-us/office/vba/api/excel.application.ontime). Plenty of examples on SO and elsewhere

Highlighting active cell in Microsoft Excel 2013 when loosing focus

I commonly work with a split screen, comparing two or more Excel sheets or researching information online and then putting it in sheets. Excel nicely highlights the active cell that is clicked, however if I go into the browser or another sheet that highlighting is deactivated, making it hard to remember where you were.
One solution is to add a VBase code that I found somewhere that highlights the active cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Cells.Interior.ColorIndex = xlNone
Target.Interior.ColorIndex = 19 'Background Yellow
End Sub
The problem with this is, that, lets say a column was red before I clicked it, when it looses focus now the old red information is gone and its no color. Can we write the information before into a variable, then highlight yello and when we click another cell we give it its original color again? What would be the best solution?
Here's a simple method which is ignoring any pre-existing coloring:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rng As Range
If Not rng Is Nothing Then rng.Interior.ColorIndex = xlNone
Set rng = Target
rng.Interior.Color = vbYellow
End Sub
Using the Static declaration means that the value of rng isn't lost when the method exits, and is a useful alternative to using a Global.
When you change selection it un-shades the previous selection. However, as you note, it will also not re-apply any shading the range had before you selected it...
This version used a Conditional Formatting approach, which won't replace any existing shading: CF shading overrides any existing fill colors.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rng As Range
If Not rng Is Nothing Then rng.FormatConditions.Delete
Set rng = Target
With rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=TRUE")
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = 49407
End With
End Sub
When the CF is removed, any existing shading will be shown as before.
If you might be selecting whole columns of cells, you could consider limiting your operations to only the UsedRange on the sheet.
Try a different spreadsheet program. If Microsoft sees enough people moving away from Excel over this bug then they will fix it. I'd say it is an easy fix for the Excel development team, unless they've all left and gone to Google :-) Google Docs spreadsheet does not have this bug!

Clearing cells in excel and 6 checkboxes

I have a button in Excel with a VBA script attached to it to clear certain cells on a sheet.
Is there a cleaner code to achieve the same result?
Also for my checkboxes for some reason 1 piece of code clears all 6 boxes, is this right?
The checkboxes are activeX boxes I added through the developer view.
Sub ClearForm()
Range("I9:I10").Select
Selection.ClearContents
Range("I13:I17").Select
Selection.ClearContents
Range("H20").Select
Selection.ClearContents
Range("C5").Select
Selection.ClearContents
Range("C9:C10").Select
Selection.ClearContents
Range("C13:C18").Select
Selection.ClearContents
Dim OleObj As OLEObject
For Each OleObj In ActiveSheet.OLEObjects
If OleObj.progID = "Forms.CheckBox.1" Then
OleObj.Object = False
End If
Next OleObj
End Sub
Well for one thing you can have a string of ranges and do it all at once.
Range("I9:I10, I13:I17, H20, C5, C9:C10, C13:C18").Select
Selection.ClearContents
as for your check-boxen, if by "clear" you mean "Removes" or "Deletes" then yes it makes sense, if on the other hand all you want to do is clear a checkbox then I think this is what you need in the loop:
OleObj.Object.Value = False
To answer your first question:
Range.Select is unecessary and actually slows execution down a bit. You can directly call .ClearContents on the range object itself. I imagine you did the Range.Select bit because of macro recorder. Whilst a useful tool to learn about Excel object model, also bear in mind that it does churn out some ugly bits ;-)
As you are performing the same set of commands several times, it is also generally better practice to encapsulate the steps into their own sub to a) have a neater 'main' procedure and b) easily enable to you adapt the process, should you later wish it.
However, this second point is definitely optional if this is a minor and unlikely to change part of your code, as the actual 'step' is just a single call to Range.ClearContents. Indeed, if it is literally intended to only clear 5 cells, I would probably just keep it like you do in the sub. I show the alternative below just for reference for more complex bits in the future.
Finally, if the group of ranges you wish to clear or do things to could change, it is also considered good practice to define a set of the ranges and enumerate the set (aka "looping through") rather than specifying identical commands for each range itself. Most objects will be handled through a Collection or an array. However for ranges, a range can contain many ranges of contiguous and non-contiguous cells. Each group of contiguous cells is referenced by using the .Areas property of the range object, which returns a range of the area. I also demonstrate this below.
Note that as another answerer has shown, Range.ClearContents can operate on multiple Areas at once so looping through Areas is not necessary in this case (it is for many other operations).
For example:
Sub ClearForm()
Dim rng As Range
For Each rng in Range("I9:I10,I13:I17,...etc...").Areas
clearRange rng
Next rng
...Rest of code...
End Sub
Private Sub clearRange(rng As Range)
rng.ClearContents
End Sub
With respect to your second question: the 6 checkboxes are cleared because you loop through all activex objects on the sheet in the last part of your code and set their Value property to False, which for a checkbox unchecks it.

VBA seems to disappear after I protect a range of cells

Hi I can't seem to get my head around this problem:
After I lock a range of cells A1:U1 (and all other cells are unlocked) and I then protect the worksheet - the VBA in the worksheet doesn't appear to work anymore - is there any way around this??
Thanks,
Kieran
If the VBA changes cells that you have locked then you need to update the VBA to unlock the worksheet before running the rest of the code. Then complete the Sub by locking the worksheet.
'Add to beginning of the Sub
Sheets("worksheet_name").Unprotect Password:="password"
'Add to end of the Sub
Sheets("worksheet_name").Protect Password:="password"

Excel Macro - Take a snapshot of Particular Range

I have come around a very strange requirement in my Excel Dashboard.
I have a some data in a Range in a particular sheet. I need to take a
snapshot of it and display it as an Image in all other sheets at top.
I know I can simply copy & paste the range but that kind of Header is creating some issues while hiding columns on the sheets.
Any solution/trick for that same?
Range has a .CopyPicture method.
Or use the Camera tool:
Sub Tester()
Sheet1.Range("D5:E16").Copy
Sheet2.Activate
Sheet2.Range("A1").Select
ActiveSheet.Pictures.Paste Link:=True
Application.CutCopyMode = False
End Sub