Update Cell Content Based on Existing Reference Cells - excel

I have the following situation:
I would like have EACH initial Ref. Cell cell and its associated blank cells, for example A2:A7, updated so that its results in the following data structure:
I have tried using formulas containing a combination of COUNT, COUNTBLANK, ROW() but have failed miserably in achieving the desired outcome.
Can you help?

In A2, formula copied down :
="1:"&COUNTA(B$2:B2)&":"&ROW(A1)-MATCH("zz",B$2:B2)+1

If with vba, maybe something like this ?
Sub Macro1()
For Each cell In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants)
If cell.End(xlDown).Row = Rows.Count Then Exit Sub
Set oEnd = cell.End(xlDown).Offset(-1, 0)
Set rngNum = cell
num = "'" & cell.Value & ":"
n = Range(cell, oEnd).Rows.Count
For i = 1 To n
rngNum.Value = num & i
Set rngNum = rngNum.Offset(1, 0)
Next i
Next cell
End Sub
But the weakness, the code will stop at the last row value in column A.
For example based on your first image, the code will stop at 3:1 while you are expecting this 3:1 row will change to 3:1:1 and the next row value is 3:1:2

Related

Dividing values in one column with another column and setting value based on another cell

I am fairly new to VBA and need some help in understanding what i am doing wrong.
I have 3 columns. If the value in the first column is zero, i want to set the third column as "N/A" . If the value in first column is not zero, then the third column should be second column / first column.
First column is c4:c6
Second column is d4:d6
Third column is e4:e6
Dim k As Range
For Each k In ws.Range("c4:c6")
If k = 0 Then
Range("e4:e6").Value = "N/A"
Else
Range("e4:e6").Formula = "=d4/c4"
End If
Next
Alternatively
Sub demo()
Range("E4:E6").FormulaR1C1 = _
"=IF(RC[-2]=0,""N/A"",RC[-1]/RC[-2])"
End Sub
Once you've defined the range you're going through, don't reference ranges again inside your loop unless you want that entire loop to be affected during each journey through the loop.
So if k=0, you don't want all of the e values to be "N/A". You only want the one related to the k cell you're looking at that has a zero.
You can use offset for this. First time through the loop, k is cell c4. So, if c4=0 then e4=N/A. e4 is the same as c4.offset(0,2). That is, it's in the same row, but two columns over.
You can also use k as a reference point to help build the formula in e, so that it's not the exact same formula each time.
With that in mind, let's rewrite your code:
Dim k As Range
Dim ws As Worksheet
Set ws = ActiveSheet
For Each k In ws.Range("C4:C6")
If k = 0 Then
k.Offset(0, 2).Value = "N/A"
Else
k.Offset(0, 2).Formula = "=" & k.Offset(0, 1).Address & "/" & k.Address
End If
Next
If you set everything in terms of k, you make sure it's all relevant, line after line.

How to create independent filter for each column with same condition in excel?

I have data in excel file for which filter has to be applied for each column independently but the filter condition is same. The reason for asking this is each column has that cell that meets the condition in a different row number.In table 1 I have 3 columns a,b and c.
I want to filter each columns independently with value=20 so that the result looks like table below
Try out this VBA code,
Sub matchvalues()
Dim i As Long, j As Long
Sheets.Add.Name = "newSheet"
j = InputBox("Enter the value to filter")
Rows("1:1").Copy Sheets("newSheet").Cells(1, 1)
For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
If IsError(Application.Match(j, Columns(i), 0)) Then
Sheets("newSheet").Cells(2, i) = ""
Else
Sheets("newSheet").Cells(2, i) = j
End If
Next i
End Sub
This code will prompt the value that has to be filtered. Need to give that as input which will create a new sheet and output the values if present.
If you want to do this with just formulas, try the below. If the value that you are searching is in cell E1, enter the below formula in cell G2 and drag across.
=IF(ISNUMBER(MATCH($E$1,A:A,0)),$E$1,"")
You can change the values in E1 directly to see the updated result. Hope this helps.

Use CELL on an array to return string made of types of the cells

Lets say we have 5000 rows with random values (blanks, numbers, characters). I need to show type of all the cells in these 5000 rows in a single cell using a formula. Is it actually possible? I've tried to CONCATENATE(CELL("type";array)) and ctrl+shift+enter but it didn't work (it returns the type of the first cell in the array).
If you want to know, this is for finding a cell with text rather than values or blanks in a very big file. Maybe you have a better solution.
Thanks in advance.
UPD: thanks for macros but I can't use them in this workbook, I need a formula-solution.
UPD: I've got how to do it with conditional formatting => new rule => use a formula to determine... => use ISTEXT('first cell of the range') formula
But still, is it possible to create the formula?
The best way to go about this is to use MACROs
Here is my sample code:
Sub Button2_Click()
numRows = 10 ' Number fo rows to loop through, in your case 5000
'loop through each cell located in column 1
'Check its type
'Concatenate each one in 1 cell on the 8th column
For i = 1 To numRows
Sheet1.Cells(1, 8).Value = Sheet1.Cells(1, 8).Value & TypeName(Sheet1.Cells(i, 1).Value) & ","
Next i
End Sub
You can adapt this small user defined function to your needs.
Say we are looking at cells in the first row, from A1 through D1 and we want to concatenate their types into a single cell. Enter the following UDF() in a standard module:
Public Function KonKaType(rIN As Range) As String
Dim r As Range
For Each r In rIN
s = "cell(""type""," & r.Address(0, 0) & ")"
KonKaType = KonKaType & Evaluate(s)
Next r
End Function
and then in the worksheet cell E1 enter:
=KonKaType(A1:D1)

Fill in the blanks excel formula to replace particular text in a cell

I'm looking for a formula to find and replace particular piece of text in a cell.
It sound a bit confusing but you can see what I mean by viewing the following image.
What I'm trying to achieve is when I fill for example cell B1 I would like to replace "SYS-NAME" in cell A25 and other cells where "SYS-NSME" is present.
You need to replace the ** before and after your values. You could change it to "" instead if you want to highlight those inserted values. ** will cause the entire cell to be replaced, instead of its match.
So the code splits column A into 2 sub columns. The first column runs until the first blank row, this contains all your variables and their new value. The second column runs until the end of the last used row. This contains your configuration with the variables that need to be changed.
Sub FindReplace()
Dim NewText, OldText As String
Dim LastRowText, i As Integer
LastRowText = Range("A1").End(xlDown).Offset(1, 0).Row
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
i = 1
Do While i < LastRowText
OldText = Range("A" & i).Value
NewText = Range("B" & i).Value
With ActiveSheet.Range("A" & LastRowText & ":A" & LastRow)
.Replace OldText, NewText, xlPart
End With
i = i + 1
Loop
End Sub
Option without a macro
In your configuration, edit each line with a variable to the following format. This will instantly update the configuration file too.
Cell A25: ="sysname " & B1
Cell A34: ="irf domain " & B2
...
The short answer would be
=SUBSTITUTE(A1,"**SYS-NAME**","MYSYSNAME")
starting in B1.

Vba Excel Formula for following condition(Circular Referencing)

Hi, i tried to put a formula like ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")" If the country is not India then Check1, Check2 and Check3 should be empty otherwise they should display their own value. when i tried to put that formula the excel has given me circular referencing warning. I just want that formula. Any help would be appreciated greatly.
When a formula refers back to its own cell, either directly or indirectly, it creates a circular reference.
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")"
You are writing the formula in activecell and the formula says if RC[-1]="India"
and if its true RC which is same as activecell. Hence you are getting the circular reference error.
But if you put this formula in Column E as below it would work.
Range("E2:E" & lastRow).FormulaR1C1 = "=IF(RC[-4]=""India"",RC[-3],"""")"
Alternatively below is simple VBA code.
Sub sample()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
For i = 2 To lastRow
If (InStr(1, Cells(i, 1), "India") <= 0) Then
Range("B" & i & ":D" & i).Clear
End If
Next
End Sub
You can't use RC as a result only as this is referencing the current formula.
Option 1: You need to have another cell to be the validated cell, e.g.the following will create a cell to the left one of the current cell:
ActiveCell.Offset(0,1).FormulaR1C1 = "=IF(RC[-2]=""India"",RC[-1],"""")"
Option 2 after comment: If your line of code is only going to clear the current cells value if the left cell is not India then use this:
If ActiveCell.Offset(0,-1).Value <> "India" Then ActiveCell.Value = ""
Option 3: If your default value in RC has to stay but a formula is needed to override it if the value of RC[-1] becomes a not equal to India you must hard code your value in the formula like so:
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",""" & ActiveCell.Value & ""","""")"

Resources