I am new creating macros and I usually record them but I'm having problems copying and paste as values to remove the formula from 3 highlighted cells
For example I have a formula on cell B3 C3 & H3 so I would like to highlight only those cells manually and then the macro can remove the formula on each like copy and paste as values on the same cells
I have tried with ActiveCell.Select but that only works for 1 cell not for multiple cells and also the problem is that I need to change to values different cells each day
Not sure I follow your question exactly but here is what I think your asking for.
You can loop through each cell that is selected and then set the cell value to it's current value which effectively removes all formulas. There are other ways to do this but I think this best answers your specific question.
Sub removeFormula()
For Each cell In Selection
cell.Value = cell.Value
Next cell
End Sub
Related
I'm looking for a way by VBA or whichever works, even 1 command button but 4-5 processes to be called
Call process1
Call process2
End Sub
on one click which somehow seems impossible for me as I'm not that advanced in Excel VBA, but hopefully this challenge is a piece of cake for others. The task is to highlight cells horizontally by range based on the formula referring to a cell in Column AM. So "B8" is an amount of 100 which needs to be divided by the number of partitions appearing on "AM3". So 100/6. Now "AM3" is 6 so starting from "C8" a number of 6 cells (Merged in 4's) will be highlighted horizontally. "C9" is relating to "AM4" which is having a value of 9 and will highlight 9 cells (Total of 36 cells since merged) horizontally .
For now this is what i have applied but it limits to only within that range :
Sub HighlightRangeOfCells()
Dim rng As Range
For Each rng In Range("C8:AL12")
If IsNumeric(rng.Value) Then
If rng.Value <> 0 Then
rng.Interior.Color = vbRed
End If
End If
Next rng
End Sub
Thanks for your efforts and reply whatever it may be if possible or not.
As mentioned by SSlinky, the best way to do this is using conditional formatting. I have created a simple Excel sheet for explaining you how to do such a thing: I have put the formula =COLUMN()<=$J1 in cell "A1", I have used this formula for conditional formatting, I have dragged to the right, and then I have dragged down.
This is what it looks like:
As you see, cells get highlighted when their column number in some way corresponds with the value of column "J". All you need to do is replace "J" by "AM" and describe the correspondence as you see fit.
For your information: in the formula, I'm using the reference $J1, which means that while dragging and dropping, the row number might change, but the referred column always needs to be "J" (it's a combination of absolute and relative cell references).
What is the VBA code in Excel for selecting the cell where your macro has initially started. For instance, I will run the macro n times in one workbook and each time before starting it I will choose different cell - A2, A3, A4 etc. What I need is that the very last line of the macro must be to select the cell where the macro has initially started - A2, A3, A4 etc.
My code now looks like this:
Selection.Copy
Range("I2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
So, the first line is copy - I started the macro from cell A2 and copied the content and pasted in cell I2. Then I want to select again cell A2 because I will copy all cells 3 columns rightward from cell A2 (B2, C2 and D2 must then be copied). On the next run, I will start the macro from cell A3 and then I want to copy all cells 3 columns rightward from cell A3 and so on.
I hope I explained my issue as clear as possible. Could you please help me with that problem?
You don not need to select anything, but if you insist you can firstly set the cell to be used as the active cell:
Sub copyFromReferenceCell()
Dim cellCopy As Range
Set cellCopy = ActiveCell 'if you want using a previously selected cell
cellCopy.Copy Destination:=cellCopy.Offset(0, 8)
Range(cellCopy.Offset(0, 1), cellCopy.Offset(0, 3)).Copy Destination:=cellCopy.Offset(0, 9)
End Sub
You did not tell us where do you need copying the range of three cells, I only supposed that you try copying in a similar way and choose "J2". If my supposition is wrong, you can set a different range
I've noticed a behaviour in Excel which doesn't intuitively make sense.
If any formula causes a cell to evaluate as a blank string, such as ="", then you copy that cell and paste as value so that the formula disappears - the cell will still count when included in a COUNTA formula. Until you press F2 to edit the cell, and then press Enter - then the cell will no longer be counted.
I can understand why COUNTA counts cells including a formula, but once you copy and paste a cell as a value, if the formula evaluated to blank, the cell should also be blank, at least intuitively. What's weird is that updating the formula causes the COUNT to decrease. This seems like a bug, but I wanted to put it on here to be sure this wasn't a weird feature I was missing.
Here are the steps I took to reproduce this (also in the image below):
Enter ="" into cells A1:A10
Select cells A1:A10 and Copy
Select cell A1 and Paste as Value
Select any cell within the range and press F2
Press Enter
The count in cell A11 will decrease by one
Alternatively to Step 4, if you select any cell within the range and press Delete, the count will also decrease.
The count in the image is 7 because I've tested Steps 4 and 5 several times - the count started at 10.
I am using the Office Insider program, so if it is a bug this may be the reason why.
Can anyone shed some light on this?
Not a bug.
="" returns an empty string, which is not the same as a truly empty cell. Copy/pasting values maintains the empty string.
Editing the cell with F2 and then Enter effectively renders the cell blank.
Here's an interesting little VBA test for more detail:
Sub Test()
With ActiveCell
.Formula = "=""""" ' enter ="" in the ActiveCell
Debug.Print TypeName(.Value) ' returns String
.Copy
.PasteSpecial xlPasteValues
Debug.Print TypeName(.Value) ' returns String
.Value = .Value
Debug.Print TypeName(.Value) ' returns Empty
End With
End Sub
I am working with a list of data where one or multiple cells in a row can be blank.
Lets say the list is cells A1, A2, A3, A4. I am trying to create a function that will do the following:
IF A1 has a value I want the cell to return A1.
IF A1 is empty then I want it to return A2.
IF A1 and A2 are both empty I want it to return A3.
If A1, A2 and A3 are all empty I want it to return A4.
first result on google: http://chandoo.org/wp/2014/01/15/find-first-non-blank-item-in-a-list-excel-formulas/
This formula returns the first TEXT cell for a range B1:B100:
=VLOOKUP("*", B1:B100, 1,FALSE)
* is a wild card in Excel. When you ask VLOOKUP to find *, it finds the first cell that contains anything.
NOTE: This approach finds first cell that contains any TEXT. So if the first non-blank cell is a number (or date, % or Boolean value), the formula shows next cell that contains text.
If you need to find non-blank that url gives the following solution:
If you want to find first non-blank value, whether it is text or number, then you can use below array formula.
=INDEX(B1:B100, MATCH(FALSE, ISBLANK(B1:B100), 0))
Make sure you press CTRL+Shift+Enter after typing this formula.
How this formula works?
ISBLANK(B1:B100) portion: This gives us list of TRUE / FALSE values depending on the 98 cells in B1:B100 are blank or not. It looks like this:
{TRUE;TRUE;TRUE;FALSE;FALSE;FALSE;FALSE; ...}
MATCH(FALSE, ISBLANK(…), 0) portion: Once we have the TRUE / FALSE values, we just need to find the first FALSE value (ie, first non-blank cell). That is what this MATCH function does. It finds an exact match of FALSE value in the list.
INDEX(B1:B100, MATCH(…)) portion: Once we know which cell is the first non-blank cell, we need its value. That is what INDEX does.
As indicated in your comment on your question, you have 500 rows interspersed with blank cells. You want to fill blank cells with the value of the last non blank cell.
I'd write some VBA code that'd work as follows: select the range of cells you want to back fill and run this VBA:
Sub fillBlanks()
For Each c In Selection.Cells
If c.Value <> "" Then
lastVal = c.Value
Else
c.Value = lastVal
End If
Next c
End Sub
basically, if the cell is empty, use the value of the last non blank cell (if there were no blank cells above, it will remain blank). Else, if the cell is not empty, save this as the last non blank cell. Repeat for every cell in the selected range.
Step by Step instructions on using this vba code - for this sample worksheet:
Make sure the range is selected, press ALT+F11.
This should open the Visual Basic Editor:
Press F7, This should bring up the code for the activesheet. Paste the VB code from above:
Press F5 (or use the menu to run the code).
The end result should be as follows:
Select ColumnA:
HOME > Editing > Find & Select > Go To Special... > Blanks, OK, =, ↓, Ctrl+Enter.
You can just put a rank.eq formula in the column next to it, and do a vlookup to bring all of your data to the top. This will bring all of your data to the top.
For example, in the image below I am ranking using the percentage, I want to bring the cells with data to the top for presentation, I will hide all columns other than where my vlookups are.
This did the trick for me
=LOOKUP(2,1/(A1:A13<>""),A1:A13)
Source credit: here
I'm developing some excel macros, and now I'm stucked with following,
I want the macro getting the cells from another file and put it on the new one but it is important to consider that copy the full range is not an option, so for example first i Need to copy a1 then a2 , etc ...
the reason is because after each "paste" process, I have to check, the old value and then start a triger of another macro depending on both values, and if ai paste the full range at once it is not working,.
Try getting the Cells value at given row and column from the Worksheet objects you are manipulating.
Example : I want to copye the value (cell content only, not style) from worksheet1 cell A1 to worksheet2 cell B3.
worksheet2.Cells(3, 2).Value = worksheet1.Cells(1, 1).Value
Simple as that.