I want to hide a column in excel using VBA macro in run time but while executing other columns also get selected. - vba

I want to hide column C based on value in cell AB7.
If value in cell AB7 is 117 then entire column C needs to hide.
Else I want column C to be there.
If Range("AB7").Value = "117" Then
Columns("C:C").Select
Selection.EntireColumn.Hidden = True
Else
Columns("C:C").EntireColumn.Hidden = False
End If
When I run the code columns A:G also get selected. Please let me know if there are any limitations while using the above code. I use excel 2010. Thanks in advance.

I would recommend not using select. Columns("C:C").Hidden = False and Columns("C:C").Hidden = True

Related

How to code button to hide a range of cells?

I am trying to create buttons that imitate the nature of nested rows, where when clicked, will hide the indented columns below it. I previously targeted a specific range of rows for each button, but as my company will attempt to add rows in the future, I want the code to continue to run properly regardless of where in the sheet the new row is added. Below is the code that I used:
Sub aButton()
With Rows("1:22")
If .Hidden Then
.Hidden = False
Else
.Hidden = True
End If
End With
End Sub
My attempted work around is to make a hidden column with A, B, C values per row to identify which range of rows go together. I want to be able to target all rows with "A" in column G, and use those values as my range in my existing code.
Can you help?

Filter Table VBA

I am doing my project and I am encountering some problems. Moreover, my scope was to use VBA as a primary software to do my project.
As for now, I am hoping that I can use a drop down list to filter my table whereby it only shows the particular department and weeks I want to show.
From the image below, From the range("C7:L26"), whenever I filter cell(F2) or cell(J2), it will leave the data that I want from the dropdown list.
For example, if cell(F2) = 2 and cell J2 = e, From range("C7,L26"), it will only show department with value "e" and have week 2 in it. As for cells that does not have the department value or week value, it will be cleared or blank.
I also hope that if it is possible to press a button to return the table back to default.
Do guide me and I really need your help!!
[1]http://imgur.com/GNGyh91 [2]http://imgur.com/uuh2Y1u
Update: I have tried recording Macros
Sub Filter()
'
' Filter Macro
Range("B9:BR38").Select
ActiveWindow.ScrollRow = 32
ActiveWindow.ScrollRow = 5
ActiveWindow.ScrollRow = 1
Selection.AutoFilter
ActiveSheet.Range("$B$9:$BR$38").AutoFilter Field:=1, Criteria1:="e"
End Sub
Update 2: Instead of recording Macros, i decided to use a Textbox to filter my table data. However, i realised that my table is not filtering according to what i type in the Text Box.
Private Sub TextBox1_Change()
Dim Text
Text = TextBox1.Value
If Text <> "" Then
Sheet2.Range("A5:AV26").AutoFilter
Field = 1
criteria = "text,_"
visibledropdown = False
Else
Sheet2.AutoFilterMode = False
End If
End Sub
You need to keep the parameters on the same line separated by commas and it is Criteria1, not criteria. If you want to hide the drop-down arrows, you need to do each individually. The code below only hides the dropdown for the field being filtered. If course, if you are only filtering one field then you could just use Sheet2.Range("A5:A26") since filtering goes across all columns.
If True Then
Sheet2.Range("A5:AV26").AutoFilter Field:=1, Criteria1:="text,_", VisibleDropDown:=False
Else
If Sheet2.AutoFilterMode Then Sheet2.AutoFilterMode = False
End If
See Range.AutoFilter Method for more information.

Macro to Hide/Unhide Columns in Excel

I created a simple macro to hide/unhide columns in Excel (attached below).
It works fine, however, when adding a column within the range of columns in the macro, the last column supposed to be hidden remains unhidden.
To make myself clear: The range of columns in macro is AM:BF. When I need to add a column within this range, the column BF stays unhidden. Could you help me how to improve the code so that the initial range of columns would stay hidden as well as the added one?
With Columns("AM:BF")
If .EntireColumn.Hidden = True Then
.EntireColumn.Hidden = False
Else
.EntireColumn.Hidden = True
End If
End With
You need to have a placer for the column. You could used a named range along the top row of columns AM:BF (Which will then change if you add a column in the middle of it). Your code could then look like
With ThisWorkbook.Sheets("MySheet").Range("NamedRange").EntireColumn
.Hidden = Not .Hidden
End With

How to change values in spesific cells after copying VBA

I need to change all the values in a workbook that is not active. All the values are listed in the same column.
In the code i'm first going to copy some specific cells, after copy them I want to set the value = 0 in cells C3:C19. My code looks like this:
Range("A3:D19").Copy
ActiveWorkbook.Close False
I then tried to used replace on C3:C19, this did not work.
Thanks :)
Worksheets(1).Range("A3:D19").Copy
Worksheets(2).Range("A3:D19").Paste ' <~~ Do whatever else here
Worksheets(1).Range("C3:C19").Value = 0
It will be better if you could show some of your code.
Range("whatever") = 0

Excel VBA: More efficient way to compare values with formulas for large range

I have large table with values in range H2:PIG2202. I need to compare the first rows H2:PIG2 values with all other rows values. And if there is a match in the result table it pastes just those values which matched.
Now I'm using this formula in the result table to display needed values:
=IF(sheet!H$2=sheet!H3;IF(AND(sheet!H3;ISBLANK(sheet!H3))=FALSE;sheet!H3;"");"")
The VBA code is:
Sub find()
Application.ScreenUpdating = False
Range("H2:PIG2202").FormulaR1C1 = _
"=IF('sheet'!R2C='sheet'!R[1]C,IF(AND('sheet'!R[1]C,ISBLANK('sheet'!R[1]C))=FALSE,'sheet'!R[1]C,""""),"""")"
Application.ScreenUpdating = True
End Sub
The problem is that Excel shows an error when I run this macros that there are not enough system resources.
Also I would like that in the result table would be just values, not formulas.
Is that possible to do? I have no idea how to achieve this :(
Thank you in advance!
Quick solution for your which is not sophisticated and I'm rather not proud of it (but it was quickest to prepare). Keep in mind that you are going to run it 24m times which could take several minutes, maybe an hour. Some comments inside the code.
Sub Solution()
Application.ScreenUpdating = False
'-----previous one
'Range("H2:PIG2202").FormulaR1C1 = _
"=IF('sheet'!R2C='sheet'!R[1]C,IF(AND('sheet'!R[1]C,ISBLANK('sheet'!R[1]C))=FALSE,'sheet'!R[1]C,""""),"""")"
'----- new one- inserting values- first idea, simple code
Dim Cell As Range
'run for one row first to check if it is ok! and check time needed per row
'next change range to one you expect
'next- take a cup of coffee and relax...
For Each Cell In Range("h2:PIG2")
Cell.FormulaR1C1 = _
"=IF('sheet'!R2C='sheet'!R[1]C,IF(AND('sheet'!R[1]C,ISBLANK('sheet'!R[1]C))=FALSE,'sheet'!R[1]C,""""),"""")"
Cell.Value = Cell.Value
'to trace progress in Excel status bar
Application.StatusBar = Cell.Address
Next Cell
Application.ScreenUpdating = True
End Sub