I would like to ask for some help regarding the following problem:
I have a workbook with two sheets. Sheet 2 contains 2 lists with phrases in them and Sheet 1 has some data. I want to create a macro that checks the data in Sheet 1 and if it finds a phrase that does not appear in the lists in Sheet 2 it gives a pop-up message. Furthermore, Sheet 1 will contain phrases separated by commas. Is it possible to also check that as well? (see screenshot)
Although really simple, I have worked out the following, however, I am sure the answer is more complicated.
Any help is appreciated!
Thank you in advance!
My code:
Sub check()
For i = 2 To 2
If Cells(i, "A") <> Worksheets("Sheet2").Cells(i, "B") Then
MsgBox "Phrase 1 does not match"
End If
For j = 2 To 2
If Cells(j, "B") <> Worksheets("Sheet2").Cells(j, "C") Then
MsgBox "Phrase 2 does not match"
End If
Next j
Next i
End Sub
The VBA code should look like this
For all cells in column 'Phase 1'
Use the SPLIT() function to split the comma separated values
and store them into a list of distinct values
For all distinct values in the list
Use the VLOOKUP function on the 'Phrase list 1' column in sheet2,
searching for the current value
If vlookup returns a valid result then
do nothing
else
pop up message
end if
end loop of distinct values
end loop of cells in column 'Phase 1'
Related
I am trying to create a data table for a user who has multiple columns with varying lists of names. They have created a new sheet for each date needed. They now want one sheet that will show the total number of times a name has appeared in each column.
I brute forced a solution that used Sum(List of COUNTIFs the name appeared in a column for that date/sheet), but this of course isnt feasible to continue
I was wondering if there is a way, probably in VBA, to make a loop to automate this by drawing from a list of sheet names that have been entered already. If I can automatically populate the cells next to the first name on the list, I can easily fill the rest of the table
If you have a better idea of how to implement this, or need more info, please let me know
-No name will appear more than once in a range
-there are 9 ranges per sheet to check
-there will be a total of 180 sheets to count, but not all are created
=SUM(COUNTIF('8-5-19'!$A$2:$A$11,$A3),COUNTIF('8-6-19'!$A$2:$A$11,$A3),COUNTIF....
Sub SumAll()
For Each cell In Worksheets("Data").Range("A2:A10")
N = 0
If cell.Value = "" Then Exit Sub
For Each sht In Worksheets
If sht.Name <> "Data" Then
For Each rngCheck In sht.Range("A2:A10")
If cell.Value = rngCheck.Value Then
N = N + 1
End If
Next
End If
Next
cell.Offset(0, 1).Value = N
Next
End Sub
I have a spreadsheet with numerous blank rows, and I need to extract specific data from cells below each blank row to populate the blank one. I'm just not good enough yet at VBA to figure out how to do this complex set of steps, so thank you in advance, and sorry to the developers who don't like how to do this questions.
I've broken down the steps I need.
Determine if row is empty.
If true, copy text from column A of the row below the empty row.
Paste text into column A of the empty row.
Change last three digits of text to "XXX".
Continue until end of spreadsheet.
EDIT: Cells as they currently are:
What I want them to look like:
I'm trying to do this for every set, but there may be 2, 3, 4, or more groups of rows with a blank row above them. So the VBA needs to include all of the rows in that group.
This sounds like what you want. If your numbers are numeric you'll want to convert them to text before your try to take out digits.
Sub dostuff()
For ir = 1 To 10000
If (Cells(ir, 1).value = "" And Cells(ir + 1, 1).value = "") Then _
Exit For
If (Cells(ir, 1).value = "") Then _
Cells(ir, 1).value = _
Mid(Format(Cells(ir + 1, 1).value,"00000000"),1,5) & "XXX"
Next ir
End Sub
I am writing and Excel VBA if statement, but I can't figure out why it is not working.
I have 1 Sheet called "Data" and I want to check if some variables in column I are the same as in my ActiveSheet row 2, column B (which is number 2). I used the following code which ends automatically because it is not working. Anybody an idea?
Example:
Sub test()
If Sheets("Data").Range("I:I") = ActiveSheet(2, 2) Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
End Sub
You should create a loop in column I if you want to validate every item in that column; use a flag to bail out as soon as you find a mismatching value, so as to avoid looping through all cells once you already know the outcome:
Dim x as long, result As Boolean
result = True
For x = 1 to 100 'let's say up to row 100
If Worksheets("Data").Range("I" & x).value <> ActiveSheet.Cells(2, 2).value Then
result = False
End If
If Not result Then Exit For
Next x
If result Then
MsgBox "Yes"
Else
MsgBox "No"
End If
You compare a whole column (i.e. 1048576 values) with a single value which obviously does not work. Furthermore if you want to access a specific cell you have to use the Cells-collection of your worksheet i.e. ActiveSheet.Cells(2,2)
If you want to compare each cell in column I individually, use a Loop. If you only want to know if the search-value exists somewhere within the column, you can use the Range.Find method.
I'm trying to write a macro that will select records in sheet1, column A (1 row per record) and attempt to match to records in sheet2, column A (zero to 40 rows per record).
If a match is found, I want the macro to search from a list of strings (found in sheet3, A1:A75) that may appear in the sheet2 matched rows, column F. If the records match, and one
of the target strings is found, I want the macro to enter "1" for the row being matched in sheet1, column P, 0 if no match is found. And then continue looping through all of the records of sheet1, column A. I haven't included my working code because it would be laughable to you all. Please help. Thank you in advance.
Since you asked a question without providing any code, I'm giving you an answer which is probably going to have to be adapted to what you are trying to do.
My macro looks for value from sheet1 and in sheet2 and returns matched values.
Sub MatchRecord()
Dim i As Integer
Dim j As Integer
i = 1
Do Until Sheets("Sheet1").Cells(i, 1) = "" 'do until empty cell in A column (in first sheet)
CompareValue = Sheets("Sheet1").Cells(i, 1)
j = 1
Do Until Sheets("Sheet2").Cells(j, 1) = "" ' do until empty cell in A column (in second sheet)
If CompareValue = Sheets("Sheet2").Cells(j, 1) Then
FindedValue = Sheets("Sheet2").Cells(j, 2) 'if CompareValue is funded in sheet2 it will return value in next column of matching row
Else
End If
Loop
Sheets("Arkusz2").Cells(i, 2) = FindedValue ' Cell in next column of searching cell will takes value from funded value
FindedValue = "" ' clear this variable
i = i + 1 'next cell to compare
Loop
End Sub
I would like to find and replace a large list of words in excel; If I have the set of words I'd like to find/search through in Sheet1, ColumnA and then if Sheet2, ColumnA reflects what is to be found and ColumnB contains the word(s) to replace found word with (all comma separated values), how do I go about doing this so that the replacements end up back in Sheet1 ColumnA?
I suspect this requires a macro, which I am not very familiar with.
Many thanks in advance for your time and assistance!
It's not super efficient but it will get the job done. You will have to account for the length of your two lists as well as if you change your sheet names.
Public Sub findsometext()
For i = 1 To 10
' change 10 to however many items are in your replacement list
' start at 2 if your data has headers
Worksheets("Sheet2").Activate
target = Cells(i, 1)
replacer = Cells(i, 2)
Worksheets("Sheet1").Activate
For j = 1 To 10
' change 10 to however many items are in your data list to be processed
' start at 2 if your data has headers
Cells(j, 1) = Replace(Cells(j, 1), target, replacer)
Next j
Next i
End Sub