delete cell in Excel - excel

I have a excel table and I want to delete if three column have same data delete second and third. I can write 4. column which datas same or different but I cannot set first 1. 2. or 3. column data
=IF((AND(A1=B1;B1=C1));"Same";"Different")
a b c
a b b
a a a
to
a b c
a b b
a

You can put this into a new module, but the macro will run on the Active Sheet:
Sub DeleteCells()
Dim intResponse, i As Integer
i = 1
intResponse = MsgBox("Are you sure you want " & _
"to delete the cells?", vbYesNo)
If intResponse = vbYes Then
Do While Cells(i, 1).Value <> "" 'does it one by one going down in the first column while the cell is not empty
If (Cells(i, 1) = Cells(i, 2)) And (Cells(i, 1) = Cells(i, 3)) Then
Cells(i, 2).Value = ""
Cells(i, 3).Value = ""
End If
i = i + 1
Loop
End If
End Sub

With a formula is possible but a little more tedious than VBA. For example in E1 copied across to G1 and then E1:G1 copied down to suit:
=IF(COUNTIF($A1:$C1,A1)<>3,A1,IF(COLUMN()=5,A1,""))
Then Copy, Paste Special, Values over the top and delete Columns A:D.
Alternatively flag the rows where the three values are the same (say with =COUNTIF(A1:C1,A1) copied down to suit) then filter that column to select 3 and blank out the values in ColumnsB:C (other than the header row).

Related

Comparing two data tables on different tabs in Excel using VBA

I am relatively new to Macros and VBA in Excel, so I need some guidance on how to solve my current issue.
The end goal of my project is to have a macro compare two sets of data organized into rows and columns (We'll say table A is the source data, and table B is based off of user input). Each row in table B should correspond to a row in table A, but they could be out of order, or there could be incorrect entries in table B.
My thought is that for the first row in each table, the macro would compare each cell left to right:
If Sheets("sheet1").Cells(2, 1) = Sheets("sheet2").Cells(2, 1) Then
If Sheets("sheet1").Cells(2, 2) = Seets("sheet2").Cells(2, 2)
Ect, ect.
My problem comes in when the Cell in table B does not match Table A.
First, I would want it to check B row 1 against the next row in A, and keep going throughout table A until it finds a "complete match" with all five columns of the row matching.
I've been trying to do this with Else if and For/Next staements
For row= 2 to 10
'if statements go here
Else If Sheets("sheet1").Cells(2, 1) <> Sheets("sheet2").Cells(2, 1)
Next row
I may be completely misunderstanding the syntax here, but I have yet to produce a situation where if the criteria is not met, it goes to the next row.
If no complete match is found, the last cell in table B row 1 that couldn't be matched should be highlighted.
Then regardless of whether a match was found or not, we would move to table B row 2, and start the whole process over.
So, I have the logic worked out (I think), where the comparison ifs would be inside a loop (or something) that would cycle through table A row by row. Then that whole process would be in another loop (or something) that would cycle through Table B.
At the end of the process, there would either be no highlighted cells showing that all entered data is correct, or cells would be highlighted showing data that do no match.
I am fairly certain that the cycling through table B is not the issue. Rather, I'm having difficulty getting the Macro to move to the next table A row if something doesn't match.
Please let me know if I need to elaborate on anything.
Thanks!
You could try:
Option Explicit
Sub test()
Dim Lastrow1 As Long, Lastrow2 As Long, i As Long, j As Long
Dim Str1 As String, Str2 As String
'Find the last row of sheet 1
Lastrow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
'Find the last row of sheet 2
Lastrow2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow1
'Let us assume that table has 3 columns. Merge 3 columns' values and create a string for each line
Str1 = Sheet1.Cells(i, 1).Value & "_" & Sheet1.Cells(i, 2).Value & "_" & Sheet1.Cells(i, 3).Value
For j = 2 To Lastrow2
'Let us assume that table has 3 columns. Merge 3 columns' values and create a string for each line
Str2 = Sheet2.Cells(j, 1).Value & "_" & Sheet2.Cells(j, 2).Value & "_" & Sheet2.Cells(j, 3).Value
'If both strings match a message box will appear
If Str1 = Str2 Then
MsgBox "Line " & i & " in table A match with line " & j & " in table B!"
Exit For
End If
Next j
Next i
End Sub
Sheet 1 structure:
Sheet 2 structure:

Remove row if value in one cell of the row is duplicated in the same column in the next row

I have a data sheet where column B is a list of data types. The only value I care about is if the value is SIS.
If row 2 has the value SIS in column B and row 3 has the value SIS in column B, then delete row 2. If row 3 contained instead a value of Topic, then keep row 2, ignore row 3, and look at row 4.
The attached image shows the sample data with a column called VBA Instructions. Any help is appreciated.
I think the below could work, this is testing if the cell in front has the same value as the current cell, if so then it would delete it. I would copy the dataset into another sheet before running a macro which deletes rows. The only problem with the below code is if you have two "Topic" cells right next to each other, for example if "Topic" tags were in rows 3 and 4 then row 3 would be deleted.
Sub test()
Columns("A:A").Insert Shift:=xlToRight
last = Cells(Rows.Count, 3).End(xlUp).Row
For Each Cell In Range("C2:C" & last)
If (Cell = Cells(Cell.Row + 1, 3)) = True Then
Range("A" & Cell.Row).Value = True
Else:
Range("A" & Cell.Row).Value = False
End If
Next Cell
For x = 2 To last:
If Cells(x, 1) = True Then
Rows(x).Delete
x = x - 1
End If
Next x
Columns("A:A").Delete
End Sub

How do I pull text from a cell in a different sheet if the cell two columns away contains specific text?

So essentially what I am trying to do is make name-tags in a spreadsheet for an attendance list I have created in a separate spreadsheet.
I have their full names in one column, and two columns over I have what colour they have been assigned to.
I have separate spreadsheets for each colour for the name-tags.
I want the formula to be written in a separate spreadsheet, let's call it "Green 20". So Green 20 would have multiple cells containing the formula that does the following:
Check to see if the word "Pink" is in a cell between D6:D100 in Spreadsheet
If "Pink" is in the cell, I want the formula to look at the row "Pink" is in and print out cell B? (? meaning whatever row it is) that contains the attendee's name.
So essentially the formula has to get data from a separate spreadsheet, check column D for specific text containing a colour, if the text is in one of the cells in the column then it needs to check the row the text is in and print out the text (First name and last name) in column B corresponding to the row that colour was in.
So I actually went ahead and wrote a macro with one of my co-workers and this is what it ended up looking like:
Public Sub NameTags()
' NameTags Macro
' March 20
Dim Rng As Range
Set Rng = Range("$B$5:$E$100")
Dim rowcount, colselect As Integer
rowcount = 1
colselect = 2
Sheets("March 20, 2017").Range("$B$4:$E$100").AutoFilter Field:=4, Criteria1:="Green"
For Each Row In Rng.Rows
'MsgBox Row.Row
If Cells(Row.Row, 2).EntireRow.Hidden = False Then
If Cells(Row.Row, 2).Value <> "" Then
copystring = Cells(Row.Row, 2).Value & " " & Cells(Row.Row, 3).Value
Sheets("Green 20").Cells(rowcount, colselect) = copystring
Sheets("Green 20").Activate
Cells(rowcount, colselect).Select
Set mergecellrng = Cells(rowcount, colselect)
Set mergecellrng2 = mergecellrng.Offset(0, 3)
Range(mergecellrng, mergecellrng2).Merge
mergecellrng.Font.Name = "Segoe UI"
mergecellrng.Font.Size = 22
mergecellrng.HorizontalAlignment = xlCenter
mergecellrng.VerticalAlignment = xlCenter
If colselect = 8 Then
rowcount = rowcount + 7
End If
Select Case colselect
Case 2
colselect = 8
Case 8
colselect = 2
End Select
End If
End If
Sheets("March 20, 2017").Activate
Next
An it repeats for each colour

Replacing strings in a column sequentially

I am naive to macros. I have data in column A with 500 rows of data. Note some rows are blank in between. The data are image files names like (X0011#00.jpg). I need to rename them sequentially with user input. The user input must be only 400500. The new name must be like (400500_Image-1.jpg). The _Image-1, _Image-2 and so on must be generated automatically in sequence omitting blank rows.
See below how my data is displayed in column A and how I want it in column B.
I appreciate if anyone can provide macro for this.
Col A Col B
X0011#00.jpg 400500_Image-1.jpg
X0021#00.jpg 400500_Image-2.jpg
X0041#00.jpg 400500_Image-3.jpg
X0071#00.jpg 400500_Image-4.jpg
X0051#00.jpg 400500_Image-5.jpg
X0031#00.jpg 400500_Image-6.jpg
X0061#00.jpg 400500_Image-7.jpg
X0091#00.jpg 400500_Image-8.jpg
Thanks
Sub naming()
RowsToProcess = Range("A" & Rows.Count).End(xlUp).Row
j = 1
userInput = InputBox("Give me text")
For i = 1 To RowsToProcess
If Not Cells(i, 1).Value = "" Then
Cells(i, 2).Value = userInput & "_Image-" & j & ".jpg"
j = j + 1
End If
Next
End Sub
This macro creates column B as desired

VBA loop one cell upward until cell not equal "" and a cell incorporate two IF functions

I'm totally new to VBA, am learning it by myself. Here is the problem:
enter image description here
So for Column E
If B = "S" Then
E = "" // null
If B = "F" Then
E = (Reverse Rank x PREVIOUS Adjusted Rank + no of failure + 1)/(Reverse Rank + 1)
or in formula excel format
E5 = ((D5*E4)+($K$1+1))/(D5+1)
So the problem lies on the PREVIOUS adjusted rank in the formula. Say, to get value of E5 it needs value of E4 BUT if E4 = "" so it has to go one cell upward. If one cell upward is also ="", it has to go one cell upward again until is not equal <> "".
The problem is I'm not sure what function is right to use. I think it would be IF and LOOP but I don't understand how to write the condition. Like I said I'm totally new and time constraints cause me anxiety. Also, if you notice, for Column E there are two IFs function I suppose? One is E is depended on Column B.
If Range("B2:B" & lastRow) = "S" or "F" and one is if E="" or <> ""
How I incorporated with that?
I think you can do this without using VBA. You can use the following to get the previous non-blank value in column E:
E5=INDEX($E:$E,MAX(ROW($E$1:$E4)*(LEN($E$1:$E4)>0)))
This formula looks in column E above cell E5 (i.e. E1:E4) and returns the one cell with the largest row value that is also not blank (length > 0).
From there, your whole formula becomes:
E5=IF($B5="S","",IF($B5="F",($D5*INDEX($E:$E,MAX(ROW($E$1:$E4)*(LEN($E$1:$E4)>0)))+($K$1+1))/($D5+1)))
Note that these are array formulas, so they must be validated using Ctrl+Shift+Enter. Also, the top non-blank value in column E has to be input manually or with a different formula.
What's the first value in column E when you meet the first "F" in
column B? I mean, there is nothing in column E, what is the "PREVIOUS Adjusted Rank". In this case, I just set it 0.
What's "no of failure"?
Here is my solution for you and I set "no of failure" as zero.
Sub test()
Dim i, j As Integer
Dim previous As Single
Dim NoOfFailure As Single
For i = 2 To Cells.CurrentRegion.Rows.Count
If Cells(i, 2).Value = "S" Then
Cells(i, 5).Value = ""
ElseIf Cells(i, 2).Value = "F" Then
previous = 0
NoOfFailure = 0
For j = i To 2 Step -1
If Cells(j, 5).Value <> "" Then
previous = Cells(j, 5).Value
Exit For
End If
Next
Cells(i, 5).Value = (Cells(i, 4).Value * previous + NoOfFailure + 1) / (Cells(i, 4).Value + 1)
End If
Next
End Sub

Resources