Vba Excel How to edit cell values in particular columns without looping all Rows based on Cell Contents? - vba

I have a SpreadSheet with data like shown. I want to hide the value of the cell in last column of a row if the corresponding SOA column in the same row has a value of 1A. I have some thousands of rows so i don't want to loop through all rows. Is there any alternative instead of looping through all rows? Any help would be appreciated greatly.

You do not need VBA for this. Insert the following formula into cell F2 and fill down:
=IF(C2="1A","",E2)
You could also do this using Format as Table, filtering SOA by the value 1A and then deleting the contents of the Exclusive row.
Finally, if you must use VBA, use program logic like the following pseudo-code:
For each cell in SOA
If cell.value = "1A"
Range("E" & cell.Row).Value = ""
Next cell

Related

How to copy/paste formula in filtered/visible cells via VBA in Excel

I am struggling with copying and pasting formula from one column to another one within using filter in another column.
I got a table with 52 columns (number of rows is changing each month).
In column AW (#49) I have applied filter. It shows only CTD.
After applying this filter I need to copy formula from column AH to column AG. Of course I need to apply this only for filtered/visible cells.
The code I have written is copying/pasting this formula into all cells in column AG (it doesn't consider my filter in column AW).
Another problem is that when applying the filter in column AW then the first visible row doesn't need to be all the time AH2 but it can be e.g. AH15 or whatever. I guess this could be avoid via some dynamic solution. Unfortunately I have no clue how to do it.
Afterwards I would like to apply the same for filters in another columns.
Many warm thanks in advance for any hint! :)
This is my code:
Sub ApplyFilterInColumnAW()
Sheets("DATA").Select
ActiveSheet.ListObjects("tb_DATA").Range.AutoFilter Field:=49, Criteria1:="CTD"
Range("AG2").Select 'dynamic solution?
ActiveCell.FormulaR1C1 = "=[#[Service/Log Formula]]" 'header name of column AH
End Sub
Range.Copy and Range.Paste only work on visible cells. In my example I target the cells in the column using the Column Header.
Sub ApplyFilterInColumnAW()
Const TagetColumnLabel = "Test"
Dim tbl As ListObject
Set tbl = Sheets("DATA").ListObjects("tb_DATA")
With Sheets("DATA")
.ListObjects("tb_DATA").Range.AutoFilter Field:=49, Criteria1:="CTD"
tbl.ListColumns(TagetColumnLabel).DataBodyRange.FormulaR1C1 = "=tb_DATA[[#This Row],[Service/Log Formula]]"
End With
End Sub

MS Excel Formula - Determine count for specific text across multiple columns

I am trying to write a formula in MS Excel that will let me know if the text "Yes" is found in any of three selected columns. Below is the formula I created but it does not seem to be working. When I drag the formula to the other rows I get a "Yes" value for rows that do not have the text "Yes". The values in the columns I am searching are blank or have "Yes" text.
=IF(COUNTIF(AF3:AF2378,"Yes") + COUNTIF(AG3:AG2378,"Yes")
+ COUNTIF(AI3:AI2378,"Yes")=0, "1","Yes")
Use:
Application.WorksheetFunction.Match("Yes", Array1, Array2, Array3)
From your question it seems that you want to check for each row alone. To do this you need to enter the formula for each row and fill down For example, at cell AJ3, you can write:
=IF(OR(AF3="Yes", AG3="Yes", AI3="Yes"), "Yes", "1")
Enter this formula at AJ3 (or any other column at row 3) then copy/paste on all the column.

Merging duplicate column data without losing the values in the rest of the rows in Excel

I'm trying to find a way to merge duplicate values in the first column of my data without losing the unique values in the rest of the rows.
e.g. at the moment my data looks like this:
and I want it to look like this:
Actually, you need to focus only on the first column. The second has nothing to do with it.
Here is some pseudo code, that would work, if you translate it to VBA:
FOR EACH CELL IN COLUMN NUMBER
IF CELL = OFFSET(PREVIOUS CELL) AND CELL.COLUMN > 1 THEN
CELL.TEXT = ""
END IF
NEXT CELL
FOR EACH CELL IN COLUMN NUMBER
IF CELL = "" THEN
ENLARGE THE RANGE
ELSE
MERGE THE RANGE
END IF
NEXT CELL
IF RANGE <> NOTHING THEN
MERGE THE RANGE
END IF

Excel: Check if cell string value exists in column, and get all cell references to that string

I suspect this may be a job for VBA, which is beyond my abilities. But here's the scenario:
Column A in Sheet 1 (CAS1) contains x rows of text values
Column A in Sheet 2 (CAS2) contains x rows of text values
Part A - For each row value in CAS1, I need to know if the string is contained in any of the cells in CAS2. Not exact match, the string can be only part of the searched cells.
Part B - I need to know the cell value of each cell in CAS2 that contains the CAS1 value (if they do exist, they can be listed in the cells adjacent to the cell being searched in CAS1).
I've tried the following to attempt Part A, all to no avail:
vlookup(A1,sheet2!A:A,1,false)
NOT(ISNA(MATCH(A1,sheet2!A:A,0)))
ISNUMBER(MATCH(A1,sheet2!A:A,0))
COUNTIF(sheet2!A:A,A1)>0
IF(ISERROR(MATCH(A1,sheet2!A:A, 0)), "No Match", "Match")
I know some of the cell values in CAS2 contain the cell values in CAS1, so I don't know why they return false or No Match. I suspect it may be down to the nature of the text content. So here's some sample data:
CAS1
LQ056
RV007H
RV008
RV009H
TSN304
TSN305
CAS2
RV009-satin-nickel-CO.jpg
STR314.jpg
STR315.jpg
HCY001.jpg
RV008-oval-rad-CO.jpg
HCY001-BRAC006.jpg
Any help would be appreciated.
This problem can be faced through VBA (at least, I imagine the VBA solution much more easily than the possible Excel one). You need a macro that, for each row in CAS1, search the content in each row of CAS2 and returns you the address.
For Each cell In Sheets("CAS1").Range("A1:A" & Sheets("CAS1").Range("A1").End(xlDown).Row) '<-- check each cell of the range A1:A? of sheet CAS1 (adapt "A" and "1" if they're different)
recFound = 0 '<-- count how many findings there are
For Each cell2 In Sheets("CAS2").Range("A1:A" & Sheets("CAS2").Range("A1").End(xlDown).Row) '<-- check in each cell of the range A1:A? of sheet CAS2 (adapt "A" and "1" if they're different)
If InStr(cell2.Value, cell.Value) <> 0 Then '<-- if the value in cell is contained in the value in cell2..
recFound = recFound + 1 '<-- account the new finding
cell.Offset(0, recFound) = Split(cell2.Address, "$")(1) & Split(cell2.Address, "$")(2) '<--write the address on the right of the currently searched cell
End If
Next cell2
Next cell
All the above should be enclosed in a macro, e.g. Sub makeMySearch(), that should be run to get the results. As commented in my code, I'm assuming that data are in A1:A? of both sheets; but they of course might be, for example, in B5:B? of the sheet 1 and in C7:C? of the sheet 2. You need clearly to adapt the code to your current data.
There's no need for VBA. Some simple array-formulas can do the job.
To see if the entry in CAS1 is present in CAS2:
=OR(ISNUMBER(SEARCH(A2,CAS2_)))
will return TRUE or FALSE. BUT this formula has to be entered by holding down CTRL-SHIFT while hitting ENTER If you do this correctly, Excel will place braces {...} around the formula that you can see in the formula bar.
The SEARCH function returns an array of results, which will be either the #VALUE! error, or a number.
In order to return the address, the following array-formula can be entered adjacent to a cell in CAS1:
=IFERROR(ADDRESS(LARGE(ISNUMBER(SEARCH($A2,CAS2_))*ROW(CAS2_),COLUMNS($A:A)),1),"")
Fill right for the maximum number of addresses possible, then select the group and fill down.
In this case, the array being returned is a string of either 0's, or 1 * the row number (i.e. the row number). I assumend the data in CAS2 was in column A, but you can change the column number if needed (or even compute it if necessary, by replacing the 1 in the ADDRESS function with COLUMN(CAS2_))
CAS1_ and CAS2_ are either named ranges, or absolute range references to the two text groups.

Excel VBA - selecting the range from first row to the last row

I have a problem with VBA code. I have a sheet with a lot of data around the cells I want. I need to select data in column F, but the position of the first and last cells between the range is to be selected is changing up and down. I created the non empty cells in row X where there is no data do the LastRow function has any refernece but now I dont know how to select the first row
thx for help
If F1 is empty, this selects the first cell with data in the F column
Worksheets("Sheet1").Range("F1").End(xlDown).Select
If F1 may be non-empty you can add a check to see whether it contains text.