I'm looking to make a button where I can clear all the cells in a range (e.g. B6:M10000) as well as a single cell (ie C3)
This is what I have so far but it doesn't seem to work.
Sub ClearContents()
Range("B6:M100000" & "C3").Select
Selection.ClearContents
End Sub
Thanks in advance .
Sub ClearContents()
Range("B6:M100000, C3").ClearContents
End Sub
Related
Have a problem with summing cells in vba using =sum(number, string) function.
Here is an example, that is working in regular excel: sum(5, A1)
But it doesn't work via VBA formula - asking for a number instead of a string. Is there a way to use =sum(5, A1) in VBA?
In VBA:
Sub dural()
MsgBox Application.WorksheetFunction.Sum(5, Range("A1").Value)
End Sub
or as JNevill points out:
Sub dural2()
MsgBox Evaluate("SUM(5,A1)")
End Sub
EDIT:
Here are some alternatives:
Sub poiuyt()
MsgBox Application.WorksheetFunction.Sum(5, [A1])
MsgBox 5 + [A1]
End Sub
To put a formula in a cell, say cell B9, use:
Sub PutFormulaInCell()
Range("B9").Formula = "=SUM(5,A1)"
End Sub
I am VBA beginner and was trying to filter the data based a cell value,after googling around a bit I have written a code which works
Sub FilterDepartment_Sales()
Sheet6.Activate
Sheet6.Cells.Select
Selection.AutoFilter
Selection.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End Sub
I had to assign this macro to object to run , the data was not getting automatically refreshed wen I change the cell value in B3, how to make it auto refresh whenevr I change the value in the B3
Also in the above code when I try to do the following it gives me an wrror
Sub FilterDepartment_Sales()
Sheet6.Activate
'Sheet6.Cells.Select
'Selection.AutoFilter
'Selection.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
Sheet6.Cells.Select.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End Sub
I get an "Object not found error" , any reason on why I can't condense the code like that
Base on the answer I had modified the code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet6.Activate
Sheet6.Cells.Select
Selection.AutoFilter
Selection.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End Sub
But now when I change the value in B3 nothing is happening , do I need to add anything ?
If you want to run a code every time you change something in a sheet, you have to place the code within a method, which handles event of changing something.
https://i.stack.imgur.com/sZQqH.png
Here is the place, where you have to find change event, on the left you have to select Sheet1, then chose Worksheet fromd ropdown list, the right one will contain all events. Change event will be raised on every change, so you'll need to ,,filter" this and handle only situation when the particular cell was changed:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 2 And Target.Column = 3 Then
Sheet6.Cells.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End If
End Sub
In the example we watch for changes only in cell B3.
Anyone know how to block cells from input (also gray it out) if for example cells A1:C1 = "No" then the rest of the row up to a say F1 is grayed out and blocked from input? I was hoping to do this in VBA but if there are other easier ways, please let me know! Thank you!
Didi
Just to show a different approach:
Put this in the sheet-code-tab:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (Intersect(Target, Me.Range("D1:F1")) Is Nothing) And Me.Evaluate("AND(LOWER(A1:C1)=""no"")") Then Me.Range("A1").Select
End Sub
And to grey them out, best will be conditional formatting:
Range: =$D$1:$F$1
Formula: =AND(LOWER($A$1:$C$1)="no")
Using the conditional formatting allows to change the cells as you like without the need to alter the VBA code (this also will be faster)
The VBA part itself, just sets the selected cell to A1 if A1:C1 is "No" and a range is selected which also includes any of the cells of D1:F1
The LOWER can be skipped if you want it to be case sensitive
The only con is: if A1:C1 is "no" you still can paste a range to a cell (not any of D1:F1 directly) which also include the locked cells.
The biggest pro is: this also works for shared workbooks (as there is no need to lock/unlock sheets)
EDIT
If the cells need to be protected then something like this will do:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Boolean
a = Me.Evaluate("AND(LOWER(A1:C1)=""no"")")
If a <> Me.Range("D1").Locked Then
Me.Unprotect
Me.Range("D1:F1").Locked = a
Me.Protect
End If
End Sub
as was mentioned in the comments, look to use a workbook change event with the following sub
Sub test()
If Worksheets("Sheet1").Range("A1").Value = "no" And Worksheets("Sheet1").Range("B1").Value = "no" And Worksheets("Sheet1").Range("C1").Value = "no" Then
Worksheets("Sheet1").Range("D1:F1").Interior.Color = RGB(220, 220, 220)
Worksheets("Sheet1").Range("D1:F1").Locked = True
Worksheets("Sheet1").Protect
End If
End Sub
I have added a private sub worksheet_calculate() in a sheet called Main. I have a value in column AP with formulas derived from other sheets and if that number is greater than value in X I want to display a message as a warning that it's over, but the code is not working any suggestions why?
Private Sub Worksheet_Calculate()
If Sheets("Main").Range("AP7").value > Sheets("Main").Range("x7").value Then
MsgBox "You Are Over Pieces Suggested"
End If
End Sub
Try this.
Private Sub Worksheet_Calculate()
If Range("AP7").Value > Range("X7").Value Then
MsgBox "You Are Over Pieces Suggested."
End If
End Sub
EDITED####
Edited the original code to run as a Worksheet_Calculate rather than a Change.
Working on trying to set the ranges to columns for you now.
EDIT#########
I flippin love a challenge. Try This.
Private Sub Worksheet_Calculate()
Set Target = Range("AP:AP").SpecialCells(xlCellTypeFormulas)
If Target Is Nothing Then Exit Sub
For Each c In Target
If c > Range("X" & c.Row) Then
MsgBox "You Are Over Pieces Suggested - Cell " & "AP" & c.Row
End If
Next
End Sub
Consider using Data Validation on cell AP7 using a "Custom" formula of: =AP7<=$X$7
Fill in the Error Alert tab on the validation menu: Stop; "You Are Over Pieces Suggested". I think this might achieve what you want without any macros. In fact, it can prevent an invalid number from being entered in the first place.
***It's working!! Thanks everyone for the help :D - turns out range was declared and set as "columnnrange" and the for loop was using "columnrange". It's the little things ;) . Appreciate the insight and guidance all!
*Thanks for the help everyone! I've updated the code per the responses. The error definitely comes when the For loop runs - if it is commented out there is no error. I believe the error is with the range "columnrange" object. Thanks!!
I have a simple Workbook_Open() routine that opens a UserForm "SelectData" and sets a ComboBox "DisplayData" value. I have a MsgBox that confirms the value set in the ComboBox "DisplayData".
Once set, the UserForm "SelectData" is hidden. Then, there is a "for" loop to hide all columns where the given cell in range "columnrange" is not equal to the ComboBox value. I'm getting an error "object required" but for the life of me cannot figure out where I'm going wrong. The goal of this spreadsheet is: "on open", allow the user to filter the visible columns on an excel doc exported from a SharePoint list.
Thanks in advance!!
Private Sub Workbook_Open()
Dim columnrange As Range
Dim cell As Range
Set columnnrange = ActiveWorkbook.Worksheets("owssvr").Range("G1:Z1")
SelectData.Show
MsgBox (SelectData.DisplayData.Value)
For Each cell In columnrange
If SelectData.DisplayData.Value <> cell Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
End Sub
Change the loop condition to verify cell instead of columnrange. Here is a simplified code that should work.
For Each cell In columnrange
If SelectData.DisplayData.Value <> cell Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
You will need to be sure the SelectData form is open. Do you have the code behind for the OK or Close button? If you use something like Me.Close then you won't have an object. You can, instead, use Me.Hide.