Hiding the data in rows -Excel VBA - vba

I have three rows.I want to hide the data in those rows and display those rows in different color.I tried searching but only found Entirerow.hidden,which hides the row number as well. Is it possible to only hide the data in the rows and display it using some other color?

The below code will change the background color to Yellow for rows 1 to 3.
Sub ChangeBackColorForSpecificRows()
Rows("1:3").Interior.Color = 65535
End Sub
Change the Rows and Interior Color to suit your requirement.
Edit:-
formupahidden set to true not working neither formatting it to locked
and hidden ,is hiding the content of formula bar – Sunaina
Copy the below code and do right click on sheet tab and select view code and paste it.
Close the VBA window (Alt+Q to close VBA window) and return to that sheet and check.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Rows("1:3")) Is Nothing Then
If Application.DisplayFormulaBar Then Application.DisplayFormulaBar = False
Else
If Not Application.DisplayFormulaBar Then Application.DisplayFormulaBar = True
End If
End Sub

Related

Reset Button Excel VBA - Value as Number not Text

I have a spreadsheet and I want to reset some cells back to how they begin.
My Reset Button works using the following code.....
Sub Reset_Cells()
'Updateby Extendoffice 20161008
Range("D4", "D8").Value = "£0.00"
Range("D11").Value = "£0.00"
End Sub
It works great but it inserts the £0.00 as text which effects a dependant cell with an IF statement (see below)
=IF(D4=0,0,IF(D4<=300,35,IF(D4<=500,50,IF(D4<=1000,70,IF(D4<=1500,80,IF(D4<=3000,115,IF(D4<=5000,205,IF(D4<=10000,455,IF(D4<=200000,ROUND(D4/100,2)*5,IF(D4>200000,10000))))))))))
When clicked the vlaues 'look' how they should but the dependant cell shows £10,000 (the highest IF option) instead of £0.00.
The resetted (is that even a word?!) cells have the green triangle in the corner which tell me the cell has a Number Stored as Text.
How can I fix this?
Thanks
How about:
Sub Reset_Cells()
With Range("D11,D4,D8")
.Value = 0
.NumberFormat = "£#,##0.00"
End With
End Sub

Excel displays duplicate calendar picker control (Macros VBA)

I've created a macros-enabled Excel file where, when clicking into a specific cell, a pop-up calendar appears. It is an ActiveX control (Microsoft Date and Time Picker Control 6.0 (SP4)). From time to time the calendar control appears next to the clicked cell and is unclickable and a duplicate control appears in the left top corner of the worksheet and this one is clickable.
I would like it to appear just next to the clicked cell. What could be wrong?
Sub Worksheet_SelectionChange(ByVal Target As Range)
With Sheet1.startPicker
If Not Intersect(Target, Range("E2:E10000")) Is Nothing Then
.Visible = True
.Top = Target.Top
.Right = Target.Offset(0, 1).Right
Else
.Visible = False
End If
End With
End Sub
Private Sub startPicker_Change()
ActiveCell.Value = Me.startPicker.Value
End Sub
When I click on the date picker control in Design Mode, then the name is set to startpicker and in the formula bar it says "=EMBED("MSComCtl2.DTPicker.1";"")"
Just now I tried changing the Checkbox (in the Calendar Properties) to True. Then when I went back, the duplicate one was gone. Changed it back to False and it didn't return.

It is possible to trap AutoFilter as an Event?

I am trying to show the filter columns to users in the report. Excel gives a different icon but for large no. columns it will be good to color the columns in another color like blue.
I found code at Is there a way to see which filters are active in Excel, other than just the funnel icons?
It works for me, but how do start this code without any button
SheetChange and selection change do not work.
code
Sub test()
Call markFilter(ActiveSheet)
End Sub
Sub markFilter(wks As Worksheet)
Dim lFilCol As Long
With wks
If .AutoFilterMode Then
For lFilCol = 1 To .AutoFilter.Filters.Count
'/ If filter is applied then mark the header as bold and font color as red
If .AutoFilter.Filters(lFilCol).On Then
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Color = vbRed
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Bold = True
Else
'/ No Filter. Column header font normal and black.
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Color = vbBlack
.AutoFilter.Range.Columns(lFilCol).Cells(1, 1).Font.Bold = False
End If
Next
Else
'/ No Filter at all. Column header font normal and black.
.UsedRange.Rows(1).Font.Color = vbBlack
.UsedRange.Rows(1).Font.Bold = False
End If
End With
End Sub
I will use the same example I used in the answer that you mentioned in your post. I answered that. :)
There is no filter change event in excel. One work-around that I would use is trapping the calculate method of the worksheet or better the workbook.
So, in the worksheet with filter add a dummy formula like this: =SUBTOTAL(3,Sheet2!$A$1:$A$100) this counts only visible cells. But its up to you. Feel free to use any formula that responds to filter change.
After that, go to workbook's code and add this :
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call markFilter(Sh)
MsgBox "Filter changed"
End Sub
Boom. Now you are trapping the filter change events and it will update the filtered columns by firing the vba code.
Note markFilter is coming from the answer that you mentioned.
The key points from my article Trapping a change to a filtered list with VBA
A "dummy" WorkSheet is added with a single SUBTOTAL formula in A1 pointing back to the range being filtered on the main sheet.
A Worksheet_Calculate() Event is added to the "dummy" WorkSheet, this Event fires when the SUBTOTAL formula updates when the filter is changed.
The next two steps are only needed if it is desired to run the Workbook Calculation as Manual
Add a Workbook_Open Event to set the EnableCalculation property of all sheets other than "Dummy" to False.
Run the Workbook in Calculation mode

(Excel VBA) If Cell Value equals "" Then Show/Hide Images

I am working on a Excel Spreadsheet that when a dropdown box value is selected an image will pop up, and if another value is selected it will hide the current image and pop up the image related to the selection. I have found a few methods that are just too time consuming using just the sheet and positioning of the image using coordinates; that's not exactly the route I would like to go.I have done a quite a bit of research before using StackOverflow, and nothing seemed to work thus far. Below is what I am trying to achieve. I am trying to keep all the images within the spreadsheet which adds another level of challenge, but I believe there is a way to do this because excel assigns the image a number when inserted EX. Picture 9.
Sub Main()
If Range(G11).Value = "anything" Then
Picture1 show
Picture2 hide
End If
End Sub
Any Help is greatly appreciated. Thanks
Rather than hiding/moving/reducing the size of the unwanted pic, why not simply delete it?
Logic:
Save all your images in a temp sheet. When ever a relevant picture is supposed to be shown, get it from the temp sheet and delete the previous.
Here is an example.
Sub Sample()
Select Case Range("G11").Value
Case "Picture 1": ShowPicture ("Picture 1")
Case "Picture 2": ShowPicture ("Picture 2")
Case "Picture 3": ShowPicture ("Picture 3")
Case "Picture 4": ShowPicture ("Picture 4")
End Select
End Sub
Sub ShowPicture(picname As String)
'~~> The reason why I am using OERN is because it is much simpler
'~~> than looping all shapes and then deleting them. There could be
'~~> charts, command buttons and other shapes. I will have to write
'~~> extra validation code so that those shapes are not deleted.
On Error Resume Next
Sheets("Sheet1").Shapes("Picture 1").Delete
Sheets("Sheet1").Shapes("Picture 2").Delete
Sheets("Sheet1").Shapes("Picture 3").Delete
Sheets("Sheet1").Shapes("Picture 4").Delete
On Error GoTo 0
Sheets("Temp").Shapes(picname).Copy
'<~~ Alternative to the below line. You may re-position the image
'<~~ after you paste as per your requirement
Sheets("Sheet1").Range("G15").Select
Sheets("Sheet1").Paste
End Sub
Snapshot of temp sheet
Here is a solution using the Visible property of the object.
I used this to show a picture based on a value in a field.
The field had a formula that resulted in either "good" or "bad".
If its value was "good", I wanted to show one picture; for "bad", another picture should show; and they should never show at the same time.
The field needed to update its value whenever a user refreshed a pivot table, so I put the code in that method of the worksheet where the pivot table and picture were to appear.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'hide both pictures by loopng through all the shapes on the sheet
Dim s As Shape
For Each s In ActiveSheet.Shapes
'hide the shape if it is a picture, leave other shapes on the page visible.
If s.Type = msoPicture Then s.Visible = msoFalse
Next
Dim judgement As String
'The field whose value tells what picture to use is a one-cell named range called "judgement"
judgement = Range("judgement")
'you need to know which picture is which.
If judgement = "Good" Then ActiveSheet.Shapes("Picture 8").Visible = True
If judgement = "Bad" Then ActiveSheet.Shapes("Picture 1").Visible = True
End Sub
Sub hidePicture(myImage)
ActiveSheet.Shapes.Range(Array(myImage)).Select
Selection.ShapeRange.Height = 0
Selection.ShapeRange.Width = 0
End Sub
Sub showPicture(myImage)
ActiveSheet.Shapes.Range(Array(myImage)).Select
Selection.ShapeRange.Height = 200
Selection.ShapeRange.Width = 300
End Sub
Handy tip: record macro and look at the code it generates!
Might be better just to move your pictures "off screen", particularly if they're of different sizes.
Sub Tester()
ShowPicture "Picture 3"
End Sub
Sub ShowPicture(PicName As String)
Dim s As Shape
For Each s In ActiveSheet.Shapes
With s
.Top = IIf(.Name = PicName, 100, 100)
.Left = IIf(.Name = PicName, 100, 1000)
End With
Next s
End Sub

How to determine if there are hidden columns when copying in Excel VBA

As the title explains, I have an Excel 2003 workbook and I'm copying a number of columns of one sheet to another in VBA. Unknown to me, someone has hidden a few columns on the source sheet and it has messed up how I process the cells in the destination sheet.
How can I programmically determine:
IF there are hidden columns
WHICH columns are hidden?
Thanks!
JFV
For a Range, check the Range.Hidden property.
The following snippet from MSDN is a good example of how to hide/unhide a row/column:
Worksheets("Sheet1").Columns("C").Hidden = True
You can also test the value with an If statement:
For ColCounter = 1 To 10
If Columns("C").Hidden = True Then
Columns("C").Hidden = False
End If
Next
If you only want to copy the visible files then one option that is quite nice it to select only the visible columns as a range.
This can be done by
Set visrng =rng.SpecialCells(xlCellTypeVisible)
It wasn't clear to me if this would be useful in your case, but I decided to post it as it could be useful to others.
Copying Visible Cells to another Range and then comparing the number of cells in each is probably the easiest way to determine if there are Hidden Cells in the Range
eg
Selection.SpecialCells(xlCellTypeVisible).Copy Destination:=VisRan
If Not Selection.Cells.Count = VisRan.Cells.Count Then
MsgBox "Selection contains Hidden Cells"
End If
You can check by using a function like:
Function IsColumnHidden(column As Variant) as Boolean
IsColumnHidden = False
If Columns(column).ColumnWidth = 0.0 Then IsColumnHidden = True
End Function
A Column width or Row height of 0.0 is an indicator of whether or not the range is hidden.
Here is one that I have tested and it works well if you want to hide/unhide columns
Sub Macro1_Test_Hidden() ' ' Macro1_Test_Hidden Macro ' ' ' If Columns("BH:XFA").Hidden = False Then Columns("BH:XFA").Select Range("BH:XFA").Activate Selection.EntireColumn.Hidden = True Else Columns("BH:XFA").Select Range("BH:XFA").Activate Selection.EntireColumn.Hidden = False End If ' ' End Sub