I want to change all cells in Excel spreadsheet of specific, user defined style (let's say 'Beauty' style) to value "Beast".
Sub BulkChangeValeOfStyle()
Dim TheCell As Range
For Each TheCell In ActiveSheet.UsedRange.Cells
If TheCell.Style = "Beauty" Then
TheCell.Value = "Beast"
End If
Next
End Sub
This code is too slow. I got lots of those Beauty cells scattered round the spreadsheet. Is it possible to make it like this:
ActiveSheet.AllCellsWithaStyle="Beauty".value="Beast"
Update
This is just an idea:
ActiveSheet.Cells.SpecialCells(xlCellTypeSameFormatConditions).Activate
or alternatively
ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation).Activate
but I do not know how to set up the criteria which determine xlCellTypeSameFormatConditions. Or criteria for xlCellTypeSameValidation. Anybody knows?
I don't think it is possible with a better solution than your For Each loop. But you should create a new Style for the cells you want to modify, and modify the format properties of that style when needed like this:
ThisWorkbook.Styles.Item("Good").Interior.ColorIndex=4
Related
I am working on an excel spreadsheet that contains several rows worth of data that I need to sort based on the values in Column A. Column B contains values that are controlled by Spinners also located in Column B.
What I am trying to do is create buttons that when pressed will sort all of the rows based on Column A but without breaking the spinners.
What I have tried so far:
When I just use a simple sort function, it moves the rows as required along with the spinners but it doesn't update the Cell Links on the spinners so they remain linked to the cells in the old questions.
I tried adding in a line in the VBA for the spinners that would update the spinners Cell Link when the spinner was pressed. The problem here is that excel would first run the increment portion of the spinner before updating the link, resulting in it incrementing/decrementing the old cell before updating the link.
I tried adding in a line in the VBA for the SORT Macros that would run a For loop to update all of the spinner Cell Links based on their new topleftcell value. This works for the Cell Link but it also updates the Cell Value based on what seems to be the Cell Value of the last spinner moved. Needless to say this is also a problem.
I'm not sure what else to try as I am still quite inexperienced with VBA.
If anyone has any suggestions for sorting cells that contain spinners without breaking the spinner's values or links I would be very appreciative!
Thanks in advance and please let me know if further information is required. I can include snippets of the code I've used so far but I wasn't sure if it owuld help much.
Try something like this.
Sub SORT()
Range("A2:B4").SORT. Key1:=Range("A2"), Order:=xlAscending
Call SpinnerControl
End Sub
Sub SpinnerControl()
Dim spn As Spinner
Dim rng As Range
For Each spn In ActiveSheet.Spinners
Set rng = spn.TopLeftCell
spn.LinkedCell = rng.Address(external:=False)
Next spn
End Sub
I have close to zero knowledge in excel and vba.
What I'm trying to do the the following:
for each row in ActiveSheet.ListObjects("SheetPotatoData")
if cell (column 5):(row) value equals "potato"
do something with (column 2):(row)
I would really appreciate it if you could enlighten me on the proper syntax to do this.
Thank you very much!
Look here: http://www.excel-easy.com/vba/range-object.html
For i = 1 To ActiveSheet.ListObjects("TableNameHere").ListRows.Count
If Cells(i,5).Value = "potato" Then
Cells(i,2).Value = "New value"
End If
Next
Alternatively, just add ".DataBodyRange.Rows" to the end of the first line in your line "For... Each" structure. You're asking VBA to iterate through each Row in a ListObject, rather each Row in the collection of Rows contained within the ListObject.
That allows sticking with the more elegant, readable, and reliable 'For...Each' structure rather than fetching range bounds and traversing it using counters. Using the DataBodyRange property of the list allows you to automatically handle any Header rows that may be present and not include them in your row inspection loop. You can reference the ListObject.Rows collection instead if you want Headers included.
Final code would look something like:
For Each row In ActiveSheet.ListObjects("SheetPotatoData").DataBodyRows.Rows
if row.Cells(1,5) = "potato"
'Do something
End If
Next
I have a somewhat large spreadsheet with a type of summary page that follows a calender layout.
On this page I manually change the font and color of cells to make it easy for me to find certain things on it. For example, (I lecture mathematics) if I have revision on a certain lesson, I make that cell bold and green. (exact type of green I can sort out myself). I want a VBA code if possible so that if I type the word revision into a cell on that sheet only, not whole workbook, that it would automatically change it to green.
Realistically, I don't manually type in the word revision always. Some of it uses lookups of various types to find what happens on that day to display a word (for example revision) in that given cell.
I don't know if this is possible to do. I realize that if "revision" is shown due to a lookup then the contents of that cell is not equal to "revision" but a formula which simply displays "revision"
Any assistance would be appreciated. If I have a basic code I can manipulate to get it right.
Thanks
Maybe you're looking for something along the lines of:
Sub CheckRevision()
Dim CurCell As Object
For Each CurCell In ActiveWorkbook.ActiveSheet.Range("A1:AZ500")
If CurCell.Value = "Revision" Then CurCell.Interior.Color = RGB(0,204,0)
Next
End Sub
Or equivalently, you can probably use conditional formatting. Home Tab > Conditional Formatting > Highlight Cells Rules > Text that Contains. From there, type the value "Revision" into the value box and you can change the format of the cell to how you like it.
I have a top cell which contains a formula referring to other cells.
Those cells contain formulas referring to other cells.
I would like to evaluate the top cell while replacing in the whole evaluation tree every occurrence of a cell, say A4, by another cell, say A5.
I know you can navigate the tree of dependants, but can you evaluate the formula after performing a replacement ?
Why not just set Calculation to Manual, make the changes to the cells and then call Application.Calculate. Seems a lot simpler and safer to use Excel's built-in dependency tracking rather than try to re-invent it.
Yes You can use the Application.Evaluate to evaluate a formula. See this example
Sub Sample()
Dim frmla As String
frmla = Range("E1").Formula
frmla = Replace(frmla, "A2", "A1")
MsgBox Application.Evaluate(frmla)
End Sub
All I'm trying to do is figure out how to change the cell in one worksheet to that of another when the one in the other is double clicked.
In my second worksheet, I have the following code set up:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
End Sub
My question would be what's the code to change a cell?
I imagine it'd look something like this:
Sheet1!CellA1!Value = Target.Value
But clearly that's not right. So what is right?
If you look in the Project Explorer in the VBA editor, there are actually two sheet names. The name on the left is how the sheet is seen within VBA, the name on the right is the name given on the tab strip at the bottom when the user is using Excel. (By default they're both "Sheet1", that can be confusing.)
I find it easiest to use the first name; it removes the need to say Worksheet("Whatever"). If the name hasn't been changed, just use that.
Sheet1.Range("A1").Value = Target.Value
edit: As a note, the only way to change the name on the left is to make sure the "Properties" window is visible, click on the sheet, and then rename. I find it to be best practice to rename all my sheets right away, to avoid Sheet1, Sheet2, Sheet3. wsTotals, wsCoverForm, and wsConfigForm can make code much more readable.
another edit: The reason the other method isn't working is because it would have to follow this structure:
ActiveWorkbook.Worksheets("Sheet1").Range("A1").Value = Target.Value
That's the only way I could get it to work, I've had both work for me testing it out just now. It's a little less cumbersome to use the first way.
Try
Worksheet("Sheet1").Range("A1").Value = Target.Value
Or, make the cell you want to change a Named Range, and just use
Range("MyName").Value = Target.Value