This time, I have a cell holding a natural number, let's say 10. Starting from another cell and for the 9 columns to the right of it, I want to display the numbers 1 through 10. If my reference cell held 20, however, I want to start at the other cell and display the numbers 1 through 20 in 20 columns. I hope you get the gist.
Is there a good way to do this, first of all, without VBA? If not, is there a good way to do this with VBA? Is there some event I should catch on a cell which would allow me to then manually insert the values I want? My knowledge of Excel and VBA is not very good, so sorry if the question is an easy one.
Thank you all in advance!
Let's say that the cell with the number is A3 and the you want to start the series in F3. In F3, put this formula
=IF(COLUMN()-5<=$A$3,COLUMN()-5,"")
Copy F3 and paste to the right (or fill right) for as many columns as you may ever need. If A3 will never be more than 100, then fill right for 106 columns. If you want to start in a column other than F, you'll need to change the -5 parts of the formula.
You won't be able to use a UDF since values are changed in more than one cell. So the easiest way (with VBA) is probably with the Change event. If the value is in the named range "holdvalue" and you want to start the numbers at range "valueStart" then this will work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = Range("holdvalue").Address Then
For i = 1 To Range("holdvalue")
Range("valueStart").Offset(0, i - 1) = i
Next i
End If
End Sub
Note: This doesn't deal with clearing the values.
You can do this with formulas:
=IF(COLUMN()<$A$35+2,COLUMN()-1,"")
Where $A$35 is the reference cell and I started the "another cell" in column B. If you start at a later column you would need to adjust the +2 and -1 in the formula accordingly. Just drag out the formula to where you would expect the max number would be, you can also do a validation on the reference cell to make sure it is below the max number and a whole number.
Related
I have this formula:
IF(ROWS($Q$27:Q27)<=$P$25,INDEX(DataTable[[#All],[Time]],$P27),"")
and if I drag it to the right, it should automatically read each column respectively; example:
=IF(ROWS($Q$27:R27)<=$P$25,INDEX(DataTable[[#All],[Name]],$P27),"")
^Notice that the first Q27 is fixed, the second Q27 is variable.
I drag this formula to the right by 15 columns, and down to 50 rows. that's 750 formulas in total.
I want to do this in vba, but if I did this, it will be 750 lines of code for each cell representing each row/column.
example: .Range("G17").Formula=IF(ROWS($Q$27:R27)<=$P$25,INDEX(DataTable[[#All],[Name]],$P27),"""")
and if I drag it down, it will automatically pick up what I exactly want, example:
=IF(ROWS($Q$27:Q28)<=$P$25,INDEX(DataTable[[#All],[Time]],$P28),"")
so this formula should be written 750 times in total for the cell range [ A27:N76 ]
Any faster / more dynamic approach? and if possible, can I make it depend on more than 50 lines based on a cell value inside the sheet?
Example:
This should do it all in one line:
Range("A27:N76").FormulaR1C1 = "=IF(ROWS(R27C17:RC[16])<=R25C16,INDEX((DataTable[[#All],[Name]],RC16),"""")"
EDIT: Seems a more that one line of code required after all 😊
The code below will do what you want (this time fully tested)
Sub FillFormulas()
Dim inC%, rgHead As Range
''' Assumes the target sheet is Active.
''' o If that's not the case, change this With statement to reference the target sheet
With ActiveSheet
''' Set rgHead to the Table's header row
Set rgHead = .ListObjects("DataTable").Range.Rows(1)
''' Add the formulas to the target range, column by column updating the table header on the fly
With .Range("A27:N76")
For inC = 1 To .Columns.Count
.Columns(inC).FormulaR1C1 = _
"=IF(ROWS(R27C17:RC[16])<=R25C16,INDEX(DataTable[[#All],[" & rgHead.Cells(inC) & "]],RC16),"""")"
Next inC
End With
End With
End Sub
so this formula should be written 750 times in total for the cell range [A27:N76]
You don't need to do that. If you specify range.Formula, it will fill the proper formulas all the way across and down. Just give it the formula of the top/left most cell.
So, in your case
Range("A27:N76").Formula = "=IF(ROWS($Q$27:R27)<=$P$25 ... "
EDIT: This response had some obvious errors
This has an obvious error (as tested part and then merged to the full thing).
Range(A27:N76).FormulaR1C1 = "=IF(ROWS(R27C17:RC[16])<=R25C16,INDEX((DataTable[[#All],[Name]],$P27),"""")"
Good day,
I'm at a loss on this problem.
I have a group of cells that contain words, like apple, this word would be the value. It is separated by a symbol for completing the math. They can be changed by the user to make custom calculations.
Cell A1 is "apple", B1 is "+", cell C1 is "apple", cell D1 is "*", cell E1 is "apple", call F1 is "=" and cell G1 is the suggested total, in this case would be "6".
It would be posted as | apple | + | apple | * | apple | = | 6 |
The legend holds the value for the word, so if you enter 2 in the legend, apple would be 2.
The logic would determine that the formula would be 2+2*2= if written in excel, I would like to combine all the cells and calculate this.
I tried using =sum, sumproduct, concate and the like to no avail.
Any head way I did make, I ended up getting BEDMAS wrong as it calculated it as 2+2=4*2=8, instead of the correct 2*2=4+2=6.
Anyone know of a way to combine these cells and properly sum the values to the correct total?
Thank you for your time!
Go to the Name manager and create named range Eval, into Refers to field add formula:
=EVALUATE(CONCATENATE(VLOOKUP(Sheet1!A1,Sheet1!$A$3:$B$5,2,0),Sheet1!B1,VLOOKUP(Sheet1!C1,Sheet1!$A$3:$B$5,2,0),Sheet1!D1,VLOOKUP(Sheet1!E1,Sheet1!$A$3:$B$5,2,0)))
In range A3:B5 I have legend.
Change references as you need. Then in cell G1 write formula =Eval.
Sample:
This is a UDF based solution. Its advantage is that it's more versatile and by far easier to maintain if you learn its elements of code. The disadvantage is in that you do have to learn the elements of code and you have an xlsm macro-enabled workbook which isn't welcome everywhere.
The setup is simple. I created a table with columns Item and Value. I placed it on another sheet than the task. In fact, you could make the sheet with the table VeryHidden so that the user can't look at it without access to the VBA project, which you can password protect. I called the table Legend. The item columns has names like apple, pear, orange. The Value column has the numeric values associated with each name.
Note that, since this is a VBA project, the entire list can be transferred to VBA leaving no trace on the sheet. You could have a function to display the value of each item as the user clicks on it and have it disappear as he clicks elsewhere.
Next I created a data validation drop-down in A1 with the List defined as =INDIRECT("Legend[Item]"). Copy this cell to C1 and E1.
Then I created another Data Validation drop-down in B1 with the list as +,-,*,/. This drop-down must be copied to D1.
Now the code below goes into a standard code module. Find the way to create it because it isn't any of those Excel sets up automatically. It's default name would be Module1. Paste the code there.
Function Evalue(Definition As Range) As Double
Dim Task As String
Dim Fact(2) As Double
Dim C As Long
Dim i As Long
With Definition
For C = 1 To 5 Step 2
On Error Resume Next
Fact(i) = Application.VLookup(.Cells(C).Value, Range("Legend"), 2, False)
i = i + 1
Next C
Task = "(" & Fact(0) & .Cells(2).Value _
& Fact(1) & ")" & .Cells(4).Value _
& Fact(2)
End With
Evalue = Evaluate(Task)
End Function
Now you are ready for testing. Call the function from the worksheet with a call like
=Evalue(A1:E1). You can use it in comparisons like =IF(G6 = Evalue(A1:E1), "Baravo!", "Try again"). As you change any of the components the cell with the function will change.
Remember to use absolute addressing if you copy formulas containing the range. If you need to get a result in VBA while testing, use this sub to call the function.
Private Sub TestEvalue()
Debug.Print Evalue(Range("A1:E1"))
End Sub
My Sheet
Here is what I have.
In cells M - U, i count all the instances of the word from cells E, G and I from the legend.
=SUMPRODUCT((LEN(E3)-LEN(SUBSTITUTE(E3,$B$3,"")))/LEN($B$3))
In cells W - AE, I multiply the instances with the value to give me a total each time the word appears.
=SUM(M3*$C$3)
In cell E8 - I8, i add the three possible values together.
=SUM(W3:Y3) so each worded cell is now a number.
I'd like to take the cells E8 - I8 to make a calculation in k8 and so on.
So, each cell is put together to make an
=SUM(E8:I8)
statement, which all works except E11 - I11 which equates to 26 instead of 43.
I'm working on data-analysis where i would like to be able to automatize color fill when looking through large amount of data where there are abundant amount of ghost logs and taking too much of my time as they are severely irrelevant.
So what i would like to do in Excel, is to be able to color fill a cell when the number changes in a column marking a different set of logs.
For instance if there are six rows of log number 456455, i would like the code to color fill the first cell when the number changes to 456456 so that it helps me identify logs faster when i know where the sets are starting. I'm kind of a newbie when it comes to Excel but this loop would help me a lot!
Thx for your time
This can be done with conditional formatting. Use a rule that compares the current cell with the cell in the row above and format if the two are different. Note that you will need to use relative references without $ signs. In the screenshot below, the conditional format is applied from row 2 to 19 and in row 2 the formula compares A2 with A1, in the next row it compares A3 with A2, and so on. If the two cells are different, the cell will change colour.
If you have some knowledge in VBA, you can implement a macro that looks at the column where you have your log number and if the value changes from one cell to another, then you highlight this cell.
I attached a template of code that works for this task.
Sub highlightChange()
Dim preVal As Integer
preVal = 0
For Each o In Range("A:A")
'Go through column
If o.Value <> preVal Then
o.Interior.Color = vbRed 'Color the selection
End If
preVal = o.Value
Next o
End Sub
There may be other solution without VBA, however, it is quite easy and practical to use a macro.
I am stuck...
I have a 100 Row sheet with 10 Columns. This list is broken into classes simply by inline headers. I have definitions of the blocks of data under each head, for example:
UNASSOCIATED A2 A19
HOSTS A21 A32
ROOF A34 A100
I compute those ranges as they may change from time to time. I need
first: Define Name of each group by formula
Second: From time to time select the defined group
I am unable to get a formula to work which will allow me to use the "Content" of the cell as opposed to the cell location to define these ranges.
I'm stuck.
Suggestions would be appreciated.
Regards,
RHD
This cannot be done in a cell formula. You'll need to create a macro (VBA) to do this. Essentially, what you need is to grab the value of a cell then use that value as if it were a cell address.
Take a look at the example macro below. If cell A1 contains the characters "B22", then x will become the value of A1 (which is "B22") and "test" will be written to cell B22. This cannot be done in with cell formulas.
Sub test()
Dim x As String
x = Range("A1").Value
Range(x).Value = "test"
End Sub
A good amount of adaptation will been needed to incorporate this technique, and you'll need to play with this a bit. Ping this community to help answer more specific questions as they come up.
I have a formula that I want to use to check if a cell does not have an 'invalid' value in it. However, it is also counting empty cells, and cells that have anything in it that isn't equal to zero:
=COUNTIF(A2:A200,"<>0")
This only checks if the cell does not have a value of "0". What can I add to it so that it will not count empty cells, or cells with values like:
#######
VALUE?
r
etc. All I want is to count how many cells have a number in them that does not equal 0, or an error.
The array formula below first counts all non-0 and non-null values and then subtracts the count of cells that contain errors.
You need to press CTRL + SHIFT + ENTER to properly execute this formula:
=COUNTIFS(A2:A200,"<>0", A2:A200,"<>"&"", A2:A200,"<>"&"NIL") - SUM(IF(ISERROR(A2:A200),1,"")) - SUM(IF(ISNA(A2:A200),1,""))
You can nest up to 7 valid or invalid entries. If you need to have more than that you should perhaps designate a column for your "black list" of entries that you can add to when an occurrence causes a count that you don't believe should be valid. For example:
=IF(ISERR(VLOOKUP(A1,Sheet1!E:E,1,FALSE))=FALSE,1,0)
Where column "E" is your list of values that are considered invalid. Drag this down next to your criteria and sum.
Edit: I wasn't aware of countifs. So you have a couple of solutions, here, depending on your preference.
This formula will count only numbers <> 0, excluding blanks, error messages, etc. BUT it will not exclude the cells that display ###### (but really contain a number) if the reason for that is a column that is too narrow, or a negative date or time value.
=SUMPRODUCT(--ISNUMBER(A2:A200))-COUNTIF(A2:A200,0)
If you really want to avoid counting cells that display ####### when the underlying contents is a number not equal to zero, you will need to use a UDF to act on the Text property of the cell. In addition, narrowing or widening the column to produce that affect will not trigger a calculation event that would update the formula, so you need to somehow do that in order to ensure the formula results are correct.
That is why I added Application.Volatile to the code, but it is still possible to produce a situation where the result of the formula does not agree with the display in the range being checked, at least until the next calculation event takes place.
To enter this User Defined Function (UDF), alt-F11 opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.
To use this User Defined Function (UDF), enter a formula like
=CountNumbersNEZero(A2:A200)
in some cell.
Option Explicit
Function CountNumbersNEZero(rg As Range) As Long
Application.Volatile
Dim C As Range
Dim L As Double
For Each C In rg
If IsNumeric(C.Text) Then
If C.Text <> 0 Then L = L + 1
End If
Next C
CountNumbersNEZero = L
End Function