i need help with the below if then statement. will try and explain as best as i can.
cells E21 and F21 is merged and the value varies. i have a list of numbers underneath each other from o1 to o100 and the same for p q r s t u that the values are constant.
i need a macro to check in which column the value in cell E21 is and put the number (that is in the macro) in cell E23(also merged with F23).
below is the macro that works if it only checks cell o1 but it does not work if i change it to o1:o100. the macro is very basic and only the first piece. please can you tell me how to get the macro to search the entire list.
Sub RunSelect()
If Range("E21").Value = Range("o1:o100").Value Then
Range("E23").Value = "3"
ElseIf Range("E21").Value = Range("p1:p100").Value Then
Range("E23").Value = "4"
Else: MsgBox ("Incorrect number entered")
End If
End Sub
thanks for all the help
I'm not ENTIRELY sure what you're looking for, but you need to write a For Each...Next loop to loop through each cell and check it's value. Excel doesn't know you want to look at each cell in those ranges, therefore you need to tell it to using a loop.
Trying working with this.
For Each c In Worksheets("Sheet1").Range("O1:O100").Cells
If c.Value = Range("E21").Value Then Range("E23").Value = "3"
Next
http://msdn.microsoft.com/en-us/library/office/aa221353(v=office.11).aspx
Related
Sub Filter_empty_Rows()
Dim Row_nr As Integer
With Worksheets("Boutenlijst Kist B")
For Row_nr = 3 To 1009
If Cells(Row_nr, 4).Value = "" Then
Cells(Row_nr, 4).EntireRow.Hidden = True
End If
Next Row_nr
End With
End Sub
This should hide all rows containing empty cells in column D. But it doesn´t. Because the empty cells still link to an other sheet. So there's is no real value but it is not empty as well.
Anyone got a workaround?
If a cell holds a reference to another cell and that cell has no value then the value of that cell's value is set to 0 by excel. So if you want one way to do it would be to do
If Cells(Row_nr, 4).Value = "" Or Cells(Row_nr, 4).Value = 0 Then
If some of your data is going to be genuinely zero then it might be a little trickier. You can use .Range.HasFormula to see if it contains a formula or not and use that to decide if it is a bad piece of data or not.
If that isn't good enough then you can use .Range.Formula to get the actual formula in a cell and then use that to perform the analysis that you need.
I have a column in excel sheet that contains the IF condition i.e.
=If(Cond 1 is TRUE, X, Y)
Now, after using this condition, i get certain values in the column. Following format can be considered (these are actual values):
4L
4L
4L
4L
Note: The two empty cells in the above col are an outcome of the TRUE condition(entry 4 and 5, i entered total 6 entries, two are empty cells ). Therefore, they are valid. (let me call the above col "A" for future reference)
Now, these empty cells actually contains formulas (the if condition). I want to CLEARCONTENT them using VBA but it is not working.
And I'm trying the below code:
If ActiveSheet.Cells(row_no, col_no) = "" Then
ActiveSheet.Cells(row_no, col_no).ClearContents
End If
But this does not work. I just want to CLEAR CONTENT those empty cells by running a loop over the whole column. The cells of the column where TEXT exist (i.e. 4L), that should be skipped but as soon the code encounters the EMPTY CELL (which actually have an IF condition), it should CLEAR CONTENT it and complete the loop. So the final result would be again the same as column "A", the only difference would be that the empty cells will be BLANK now i.e. they will not have any IF condition.
I do not have problem running the loops but i am not getting how to tell VBA about that empty cell and clear contenting it. Hopefully i was able to post a clear query. Thanking in advance.
Regards
Nayyar
Please try the below sample code it should work..
Sub test()
'Assuming your data in column A from A2
Set Rng = Range("A2", Cells(Rows.Count, 1).End(xlUp))
For Each cell In Rng
If cell.Value = "" Then
cell.ClearContents
End If
Next
End Sub
And in your formula =If(Cond 1 is TRUE, X, Y) your not giving any output like "" which will give you blank. Please update it and then try :)
Try this as well
If (Range("A35").Value = "") Then
Range("A35").Formula = ""
End If
I have a spread sheet for tracking different savings, so column A has a name, B has a currency value. I want to be able to enter a value in column C and have it update the B cell next to it, then return to 0. For example:
B1 = £50.00
I type -£12.00 in C1
B1 = £38.00
C1 = £0.00
I thought there would be a built in function, but I can't find one. I think I will need to write a macro to do this. Can anyone show me how this would be done?
You'll want to add the following code to the sheet your working on. Right Click the sheet name Tab and choose View code and paste this in.
Private Sub Worksheet_Change (ByVal Target as Range)
Dim FirstNum as Currency 'Long is for number currency should help keep format
Dim SecNum as Currency
If Target.Column = 3 Then 'Only Runs if Cell being changed is in column C, Might need to be Columns
Application.EnableEvents = False ' Stop macro changes calling function repeatedly
FirstNum = Target.offset(-1,0).Value ' Value in Column B
SecNum = Target.Value ' Value being typed in C
Target.Offset(-1,0).Value = FirstNum - SecNum ' Makes Cell B equal to difference of previous value and value typed in C
MsgBox("Difference Found") ' Just to display code worked Remove when confirm code works
Target.Clear ' Clears Value you typed
Application.EnableEvents = True ' Re-enable the macro call
End if
End Sub
Seems like you are wanting to do something here that is a little different to the way that excel would normally work. Suggest you use more rows then you wouldn't need a macro. e.g. B1 = $50 C1=-$12
then B2==IF(C1<>"",B1+C1,""). Then you copy cell B1 down to propegate the formulae in cells below. You enter you next value in C2. Does this do what you need?
I am trying to create a macro that will allows me to deleted dupilcation "only if" the cell beside contains the exact same number.
Example:
Range will be P15:Q34
P Q
Row 15 8.22 8.22
Row 16 16.33 32.22
Row 17 25.66 25.66
.
.
. and so on
As you can see, I have duplication in Q15 and Q17. I would like to keep the all the information in P columns and any Q cells (like Q16) but only delete the duplcation. Can someone help me please with this macro?
Hope this helps
Sub Test()
For Each Cell In Sheets("New Summary").Range("P15:Q34")
If Cell.Value = Cell.Offset(0, 1) Then
Cell.Offset(0, 1) = ""
End If
Next
End Sub
I think what you want is easy without VBA. In another column (I suggest in Row15) put:
=IF(P15=Q15,"",Q15)
and copy down to suit. Select the results and paste Special Values into Q15.
The two columns look like on this image.
When I want to show only the cells which contain a letter 'b', I can no longer see the text "Title1" and "Title2" which is normally visible in the column B.
I guess although the cells in column B are merged, the text is still bound to A3, respectively to A7.
So how can I at the same time filter the visible content and preserve the merged text? In simple words, I want to filter content by letter 'b' and I still want to see the text "title 1/2" in the column B.
You tagged excel so here is a solution in excel:
You need to click on that column with the merged cells and unmerge all cells.
Then you need to put this formula at the top of your list and enter it with ctrl+shift+enter(this will enter it as an array formula):
=OFFSET(C3,MAX(IF(NOT(ISBLANK(C$3:C3)),ROW(C$3:C3),0))-ROW(C3),0)
Then you need to autofill that down.(this function seems a little verbose but I just got it online - there is probably a simpler way to do this - but it finds the last nonblank cell in a range).
I think openoffice has similar functions so you should be able do the same or something similar in openoffice.
Alternatively if you are using excel you could click on the column you want to unmerge and run this macro:
Sub UnMergeSelectedColumn()
Dim C As Range, CC As Range
Dim MA As Range, RepeatVal As Variant
For Each C In Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
If C.MergeCells = True Then
Set MA = C.MergeArea
If RepeatVal = "" Then RepeatVal = C.Value
MA.MergeCells = False
For Each CC In MA
CC.Value = RepeatVal
Next
End If
RepeatVal = ""
Next
End Sub
Good Luck.
EDIT:
I found a Non-VBA solution that will work in both excel and openoffice and doesn't require you to enter it as an array formula(with ctrl+shift+enter):
=INDEX(B:B,ROUND(SUMPRODUCT(MAX((B$1:B1<>"")*(ROW(B$1:B1)))),0),1)
In open office I think you want to enter it like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0),1)
or maybe like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0))
You just need to autofill that formula down:
Your main problem seems to be the one "blank row" that you have left after the filter fields.
Remove it, and it will work fine.