hiding rows if data is not present in another worksheet - vba

I have about 34,000 lines of data in a worksheet and I need too hide rows that does not have matching data in another worksheet. I have some code but i seems to hide all the data instead of jst hiding the data which is not represented in the other worksheet. The code is shown below and any help would be appreciated!
Sub HideCells()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet, sht As Worksheet
Dim valueToFind
Dim i As Long, lastrow As Long, lastrow2 As Long
Set xlSheet = ActiveWorkbook.Worksheets("Køb VT nummer")
lastrow = xlSheet.Cells(xlSheet.Rows.Count, "A").End(xlUp).Row
Set xlRange = xlSheet.Range("A1:A" & lastrow)
Set sht = ActiveWorkbook.Worksheets("køb total")
lastrow2 = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Debug.Print lastrow
Debug.Print lastrow2
For i = 2 To lastrow2
valueToFind = Sheets("køb total").Cells(i, 7).Value
For Each xlCell In xlRange
If Not xlCell.Value = valueToFind Then
Worksheets("Køb total").Rows(i).EntireRow.Hidden = True
Exit For
End If
Next xlCell
Next i
End Sub

This is because a line containing "ValueToFind" for one value of "i" doesn't necessarily contain it for a different value of "i"
Try
Worksheets("Køb total").usedRange.rows.hidden = true
For i = 2 To lastrow2
valueToFind = Sheets("køb total").Cells(i, 7).Value
For Each xlCell In xlRange
If xlCell.Value = valueToFind Then
Worksheets("Køb total").Rows(i).EntireRow.Hidden = False
End If
Next xlCell
Next i

Your code will run much faster if you do all the hiding at the end of the checking.
Sub HideCells()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet, sht As Worksheet
Dim i As Long, lastrow As Long, lastrow2 As Long
Dim rngHide As Range, c As Range
Set xlSheet = ActiveWorkbook.Worksheets("Køb VT nummer")
lastrow = xlSheet.Cells(xlSheet.Rows.Count, "A").End(xlUp).Row
Set xlRange = xlSheet.Range("A1:A" & lastrow)
Set sht = ActiveWorkbook.Worksheets("køb total")
lastrow2 = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
For Each c In sht.Range("G2:G" & lastrow2)
'any match on the other sheet?
If IsError(Application.Match(c.Value, xlRange, 0)) Then
If rngHide Is Nothing Then
Set rngHide = c
Else
Set rngHide = Application.Union(rngHide, c)
End If
End If
Next c
'any rows to hide? If Yes then hide them all
If Not rngHide Is Nothing Then rngHide.EntireRow.Hidden = True
End Sub

Related

Select the same range in multiple workheets

So I need to select the same range in all worksheets except "Sheet1". The range is dinamic based on the value "s1" on the column A. So I want to select what is in column B for the value s1, make it bold, then to count the s1 values in column C.
This is what I have so far
Sub test()
Dim ws As Worksheet
Dim lastrow As Long
Dim xRg As Range, yRg As Range, zRg As Range
Dim cell As Range
Dim C1 As Range
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For Each xRg In Range("A1:A" & lastrow)
If xRg.Text = "s1" Then
If yRg Is Nothing Then
Set yRg = Range("B" & xRg.Row).Resize(, 1)
k = 1
For Each cell In yRg
yRg.Cells(k, 2) = k
yRg.Cells.Select
k = k + 1
Next cell
Else
Set yRg = Union(yRg, Range("B" & xRg.Row).Resize(, 1))
If Not yRg Is Nothing Then yRg.Select
For Each C1 In yRg
C1.EntireRow.Font.Bold = True
Next C1
End Sub
Try this code:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim xRg As Range, yRg As Range
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
ws.Cells.Font.Bold = False ' clear bold formatting for debugging purposes
Set yRg = Nothing
For Each xRg In ws.Range("A1", ws.Cells(ws.Rows.Count, "A").End(xlUp))
If xRg.Text = "s1" Then
If yRg Is Nothing Then
Set yRg = xRg.Offset(0, 1)
Else
Set yRg = Union(yRg, xRg.Offset(0, 1))
End If
xRg.Offset(0, 2) = yRg.Cells.Count 'set entry number
End If
Next xRg
If Not yRg Is Nothing Then yRg.Font.Bold = True
End If
Next ws
Application.ScreenUpdating = True
End Sub
Before
After
A selection or a range does not extend across multiple sheets; there is a selection per sheet. So you need to work within each sheet.
You had a lot of unclosed loops and conditions. This is my best guess at what you were trying to do:
Sub test()
Dim ws As Worksheet
Dim lastrow As Long
Dim xRg As Range, yRg As Range
Dim cell As Range
Dim s1count As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
Set yRg = Nothing
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For Each xRg In Range("A1:A" & lastrow)
If xRg.Text = "s1" Then
If yRg Is Nothing Then
Set yRg = xRg.Offset(0, 1)
Else
Set yRg = Union(yRg, xRg.Offset(0, 1))
End If
End If
Next xRg
If Not yRg Is Nothing Then
s1count = 0
For Each cell In yRg
cell.EntireRow.Font.Bold = True
s1count = s1count + 1
cell.Offset(0, 1) = s1count
Next cell
End If
End If
Next ws
End Sub

Searching for a value in another worksheet and hiding row if it is not present in worksheet

I am trying to write some VBA code to check numbers in one worksheet against numbers in another worksheet. I am trying to get excel to hide rows in one worksheet, if it is not present in another worksheet. I have written some code but I can not seem to get it functioning, any advice would be appreciated. The VBA code is attached underneath.
Sub HideCells()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind
Dim i As Long
For i = 2 To Rows.Count
valueToFind = Sheets("køb total").Cells(i, 1).Value
Set xlSheet = ActiveWorkbook.Worksheets("Køb VT nummer")
Set xlRange = xlSheet.Range("A1:A50000")
For Each xlCell In xlRange
If xlCell.Value = valueToFind Then
Else
Worksheets("Køb total").Rows("i").EntireRow.Hidden = True
End If
Next xlCell
Next i
End Sub
Try this:
Sub HideCells()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind
Dim i As Long
Dim bExists As Boolean
For i = 2 To Rows.Count
valueToFind = Sheets("køb total").Cells(i, 1).Value
Set xlSheet = ActiveWorkbook.Worksheets("Køb VT nummer")
Set xlRange = xlSheet.Range("A1:A50000")
For Each xlCell In xlRange
If xlCell.Value = valueToFind Then
bExists = True
Exit For
End If
Next xlCell
If Not bExists Then
Worksheets("Køb total").Rows("i").EntireRow.Hidden = True
End If
Next i
End Sub
Couple changes - removed the quotation marks from around i in your If statement, moved some Sets outside the loop, and changed your If/Else statement to one case of If Not:
Sub HideCells()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet, sht As Worksheet
Dim valueToFind
Dim i As Long, lastrow As Long, lastrow2 As Long
Set xlSheet = ActiveWorkbook.Worksheets("Køb VT nummer")
lastrow = xlSheet.Cells(xlSheet.Rows.Count, "A").End(xlUp).Row
Set xlRange = xlSheet.Range("A1:A" & lastrow)
Set sht = ActiveWorkbook.Worksheets("køb total")
lastrow2 = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Debug.Print lastrow
Debug.Print lastrow2
For i = 2 To lastrow2
valueToFind = Sheets("køb total").Cells(i, 1).Value
For Each xlCell In xlRange
If Not xlCell.Value = valueToFind Then
Worksheets("Køb total").Rows(i).EntireRow.Hidden = True
Exit For
End If
Next xlCell
Next i
End Sub
Although I think you still need to replace Rows.Count with lastrow, you should make sure that is equaling what you think it is (by stepping through your code with F8, running your cursor over Rows.Count and seeing what its value is).

VBA Sort Rows into Different Worksheets Based on Array of Strings

Beginner VBA scripter here. How can I fix my code so that it will search thru Sheet1 for the string array in strSearch and copy those rows into Sheet2?
Also, how can I extend the code to be able to search for a different string array and copy it into another worksheet?
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Dim lastRow As Long
Dim strSearch As Variant
Dim i As Integer
Set ws1 = Worksheets("Sheet1")
With ws1
.AutoFilterMode = False
lRow = .Range("J" & .Rows.Count).End(xlUp).Row
With .Range("J1:J" & lRow)
On Error Resume Next
strSearch = Array("John","Jim")
.AutoFilter Field:=1, Criteria1:=strSearch
Set copyFrom = .Offset(0).SpecialCells(xlCellTypeVisible).EntireRow
On Error GoTo 0
End With
Set ws2 = Worksheets("Sheet2")
With ws2
On Error Resume Next
lastRow = ws2.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Set Rng = copyFrom.SpecialCells(xlCellTypeConstants)
Rng.Copy .Cells(lastRow + 1, "C")
copyFrom.Delete
On Error GoTo 0
End With
.AutoFilterMode = False
You could iterate through the lines and the array:
Option Explicit
Dim firstRowWs1 As Long
Dim lastRowWs1 As Long
Dim lastRowWs2 As Long
Dim searchColumnWs1 As Integer
Dim i As Integer
Dim check As Variant
Dim strSearch As Variant
Sub test()
lastRowWs1 = ws1.UsedRange.Rows.Count
lastRowWs2 = ws2.UsedRange.Rows.Count
firstRowWs1 = 2
searchColumnWs1 = 1
strSearch = Array("John", "Jim")
For i = firstRowWs1 To lastRowWs1
For Each check In strSearch
If check = ws1.Cells(i, searchColumnWs1).Value Then
ws1.Rows(i).Copy (ws2.Rows(lastRowWs2 + 1))
lastRowWs2 = lastRowWs2 + 1
ws1.Rows(i).Delete shift:=xlUp
i = i - 1
Exit For
End If
Next check
Next i
End Sub
Dim strsearchlocation as integer
strSearchLocation = Sheet1.Cells.Find(what:= strSearch, After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).row
Sheet1.Rows(strSearchLocation).Copy
Finds and copies the row of strSearch

Compare Multiple Columns VBA EXCEL (Nested Loops)

I have the following code which compares two different sheets at the same time.
Dim compareRange As Range
Dim toCompare As Range
Dim rFound As Range
Dim cel As Range
Set compareRange = Worksheets("sheet2").Range("A1:A" & Lastrow3)
Set toCompare = Worksheets("sheet3").Range("A1:A" & Lastrow4)
Set rFound = Nothing
For Each cel In toCompare
Set rFound = compareRange.Find(cel)
Z = compareRange.Find(cel).Row
If Not rFound Is Nothing Then
cel.EntireRow.Interior.Color = vbGreen
Set rFound = Nothing
End If
Next cel
However this only compares column A's when I want to compare column A's C'd and D's at the same time and only pass when all three match. The sheets have duplicate values that is why I need to compare 3 items at a time but some columns are identical. I have to use a nested loop. Any idea where to start?
I thought I could do something like
Set compareRange = Worksheets("sheet2").Range("A1:A, C1:C, D1:D" & Lastrow3)
But apparently I can't
Actually you are not comparing other two columns in your code. Try below code.
Sub Demo()
Dim compareRange As Range, toCompare As Range
Dim lastRow1 As Long, lastRow2 As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Dim i As Long, j As Long
Set ws1 = ThisWorkbook.Worksheets("Sheet2")
Set ws2 = ThisWorkbook.Worksheets("Sheet3")
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
Set compareRange = ws1.Range("A1:A" & lastRow1)
Set toCompare = ws2.Range("A1:A" & lastRow2)
For i = 1 To lastRow2
For j = 1 To lastRow1
If ws2.Cells(i, 1) = ws1.Cells(j, 1) And ws2.Cells(i, 3) = ws1.Cells(j, 3) And ws2.Cells(i, 4) = ws1.Cells(j, 4) Then
ws2.Cells(i, 1).Interior.Color = vbGreen
Exit For
End If
Next j
Next i
End Sub
Let me know if anything is not clear.

Comparing value of cells from two different sheets

First my code:
Option Explicit
Sub UpdateCandidates()
Application.ScreenUpdating = False
Dim wks As Worksheet, wks2 As Worksheet
Dim Lastrow As String, Lastrow2 As String
Dim Rng As Range, i As Long, Rng2 As Range, i2 As Long
Dim cell As Variant, cell2 As Variant
Set wks = ThisWorkbook.Worksheets("Candidates")
Lastrow = wks.Range("B" & Rows.Count).End(xlUp).Row
If Lastrow > 1 Then
cell = wks.Range("B2:B" & Lastrow).Value
i = 1: Set Rng = Nothing
While i <= Lastrow
For i = i To Lastrow
Set wks2 = ThisWorkbook.Worksheets("Job live")
Lastrow2 = wks2.Range("A" & Rows.Count).End(xlUp).Row
If Lastrow2 > 1 Then
cell2 = wks2.Range("A2:A" & Lastrow2).Value
i2 = 1: Set Rng2 = Nothing
While i2 <= Lastrow2
For i2 = i2 To Lastrow2
If cell = cell2(i2, 1) Then
MsgBox ("found")
End If
Next
Wend
End If
Next
Wend
End If
Application.ScreenUpdating = True
End Sub
This basically works and compares the two columns but at the end it shows an error:
"Subscript out of range"
I don't understand why. I thought it's because of <= Lastrow but fixing to < Lastrow doesn't change anything.
I also would like to copy a value from the first sheet to the second one to a particular cell. And also insert a row below the cell from my second sheet.
I also don't understand why I have to compare cell to cell2(i2,1) and not cell to cell2. If I compare cell to cell2 it says type mismatch. And I have the same error if I enter a second value in my sheets.
What's wrong with my code?
I see your code, and here's a proposal
Option Explicit
Sub CompareDefinedRanges()
Dim rng1, rng2 As Range
Dim found As Boolean
Dim i, j, foundAt As Integer
Set rng1 = Worksheets("Candidates").Range("B2", Worksheets("candidates").Range("B2").End(xlDown).Address)
Set rng2 = Worksheets("Job live").Range("A2", Worksheets("Job Live").Range("A2").End(xlDown).Address)
'show items
For i = 1 To rng1.Rows.Count
found = False
foundAt = 0
For j = 1 To rng2.Rows.Count
If rng1.Item(i) = rng2.Item(j) Then
found = True
foundAt = j
End If
Next j
If found Then
MsgBox rng1.Item(i).Value & " found at " & CStr(foundAt), , "Candidates"
Else
MsgBox rng1.Item(i).Value & " not found", , "Candidates"
End If
Next i
Set rng1 = Nothing
Set rng2 = Nothing
End Sub