Excel: How to move text from multiple cells to first cell? - excel

i have a hug excel table:
I need a formula to move data to the first cell, like below.
please help me to do that.

This works. Just be sure to set the source_rng to the range that you want. Should scale up well too. EDIT: realize now that this doesn't preserve color, will work on that
Sub main()
Dim ws As Worksheet
Dim source_rng As Range
Dim target_rng As Range
Dim source_data() As Variant
Dim clean_data() As Variant
Dim column_count As Long
Dim row_count As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set ws = ActiveSheet
Set source_rng = ws.Range("A1:F19200")
column_count = source_rng.Columns.Count
row_count = source_rng.Rows.Count
ReDim source_data(1 To row_count, 1 To column_count)
ReDim clean_data(1 To row_count * column_count, 1)
source_data = source_rng
k = 1
For j = 1 To row_count
For i = 1 To column_count
If source_data(j, i) <> "" Then
k = k + 1
End If
Next i
Next j
ReDim clean_data(1 To k, 1)
k = 1
For j = 1 To row_count
For i = 1 To column_count
If source_data(j, i) <> "" Then
clean_data(k, 0) = source_data(j, i)
k = k + 1
End If
Next i
Next j
source_rng.Clear
Set target_range = ThisWorkbook.ActiveSheet.Range("A1:A" & UBound(clean_data))
target_range.value = clean_data
End Sub

Related

Add 5 rows for each row using VBA Macros

Hi All i have two worksheets from Sheet1 i am copying to Sheet2 which copy data to each row once now i want my data should be look like "To be Version" :
First column will be repeated 5 time and other column should be only once i am trying but it does not working ..
I am using Macros to do this
Function getLastRow(targetSheet As Worksheet, colLetter As String) As Integer
Dim lastRow As Integer
With targetSheet
getLastRow = .Cells(.Rows.count, colLetter).End(xlUp).Row
End With
End Function
Function getColumn(targetSheet As Worksheet, FindWord As String, Optional iRow As Integer = 1) As Integer
Dim iCol As Integer
Dim tmpString As String
For iCol = 1 To getLastColumn(targetSheet, 2)
'targetSheet.Activate
tmpString = VBA.Replace(targetSheet.Cells(iRow, iCol).Value, "", "")
If VBA.InStr(1, VBA.LCase(tmpString), VBA.Replace(VBA.LCase(FindWord), "", "")) Then
getColumn = iCol
Exit Function
End If
Next iCol
End Function
Sub ProcFile()
Dim wsRaw As Worksheet: Set wsRaw = ThisWorkbook.Sheets("Sheet1")
Dim wsAR As Worksheet: Set wsAR = ThisWorkbook.Sheets("Sheet2")
Dim iRow, x, LRow, sRow, col As Long
Dim Tes1, Test2, Test3 As String
sRow = getLastRow(wsAR, "E") + 1
LRow = getLastRow(wsRaw, "A")
If wsRaw.Range("A2").Value = "" Then MsgBox "Raw Data tab is Empty!!", vbCritical: Exit Sub
For x = 2 To LRow
Tes1 = wsRaw.Cells(x, getColumn(wsRaw, "Tes1")).Value
Test2 = wsRaw.Cells(x, getColumn(wsRaw, "Test2")).Value
Test3 = wsRaw.Cells(x, getColumn(wsRaw, "Test3")).Value
For col = 3 To 45 Step 2
If wsRaw.Cells(x, col).Value <> "" Then
wsAR.Range("A" & sRow).Value = Tes1
wsAR.Range("B" & sRow).Value = Test2
wsAR.Range("C" & sRow).Value = Test3
End If
Next col
sRow = sRow + 1
Next x
MsgBox "Done!!"
End Sub
Sheet 1 Raw Data
Sheet 2 Data in after macro excution
The Data i am getting in my Macro excution:
Repeat Rows
Quick Help (Not tested)
Replace the line
sRow = sRow + 1
with
sRow = sRow + 5
Instead of the lines:
wsAR.Range("A" & sRow).Value = Tes1
wsAR.Range("B" & sRow).Value = Test2
wsAR.Range("C" & sRow).Value = Test3
use the following:
wsAR.Range("B" & sRow).Value = Test2
wsAR.Range("C" & sRow).Value = Test3
For sRow = sRow To sRow + 4
wsAR.Range("A" & sRow).Value = Test1
Next sRow
sRow = sRow - 5
Initial Answer
The following will overwrite a range in the following way:
The first row (the headers) stays the same.
Every value in the first column will be written five times, one below the other.
The remaining columns will be written only once leaving four empty cells below.
Option Explicit
Sub repeatRows()
Const wsName As String = "Sheet2"
Const RowsCount As Long = 4
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim rg As Range: Set rg = wb.Worksheets(wsName).Range("A1").CurrentRegion
Dim Data As Variant: Data = rg.Value
Dim srCount As Long: srCount = UBound(Data, 1)
Dim cCount As Long: cCount = UBound(Data, 2)
Dim drCount As Long: drCount = (srCount - 1) * (RowsCount + 1) + 1
Dim Result As Variant: ReDim Result(1 To drCount, 1 To cCount)
Dim c As Long
For c = 1 To cCount
Result(1, c) = Data(1, c)
Next c
Dim n As Long: n = 1
Dim r As Long, i As Long
For r = 2 To srCount
n = n + 1
For c = 1 To cCount
Result(n, c) = Data(r, c)
Next c
For i = 1 To RowsCount
n = n + 1
Result(n, 1) = Data(r, 1)
Next i
Next r
rg.Resize(n).Value = Result
End Sub

How to change font colour on Variant excel VBA?

I have some code that takes a cell value such as <0.2 and discards the 'less than' symbol and returns half the numeric portion e.g. 0.1
This works well but I want to indicate the cells that have been changed by making the font colour red.
I can't get the formatting applied. I have tried a few variations on the code below but get a run-time error 424 object required, on the formatting line. MyVar = MyVar.Font.ColorIndex = 3
Sub RemoveLessThanSpedUp()
'
'
'Values with Red Text were reported as less than the method detection limit and are shown here as one-half the detection limit
'
Dim r As Range
Dim Datarange As Variant
Dim Irow As Integer
Dim MaxRows As Long
Dim Icol As Integer
Dim MaxCols As Long
Dim MyVar As Variant
Datarange = Range("A1").CurrentRegion.Value
MaxRows = Range("A1").CurrentRegion.Rows.Count
MaxCols = Range("A1").CurrentRegion.Columns.Count
For Irow = 1 To MaxRows
For Icol = 1 To MaxCols
MyVar = Datarange(Irow, Icol)
If Left(MyVar, 1) = "<" Then
MyVar = (Right(MyVar, Len(MyVar) - 1)) / 2
MyVar = MyVar.Characters.ColorIndex = 3
'MyVar.Font.ColorIndex = 3
Datarange(Irow, Icol) = MyVar
End If
Next Icol
Next Irow
Range("A1").CurrentRegion = Datarange
End Sub```
Try the next adapted code, please. Your code uses an array, which is not bad in terms of speed, but it does not handle ranges which have a font property. Coloring many cells may take a lot of time. I will build an array to be filled with the modified cells address, which will be dropped on the first column after the existing one:
Sub RemoveLessThanSpedUp()
Dim r As Range, Datarange As Variant, Irow As Integer
Dim MaxCols As Long, MyVar As Variant, MaxRows As Long, Icol As Long
Dim sh As Worksheet, i As Long, arrEr, k As Long
Set sh = ActiveSheet 'use here what you need
MaxRows = sh.Range("A1").CurrentRegion.Rows.Count
MaxCols = sh.Range("A1").CurrentRegion.Columns.Count
If sh.Cells(1, MaxCols).Value = "Errors" Then
sh.Cells(1, MaxCols).EntireColumn.Clear
MaxCols = MaxCols - 1
End If
Datarange = sh.Range("A1").CurrentRegion.Value
ReDim arrEr(MaxRows * MaxCols)
For Irow = 1 To MaxRows
For Icol = 1 To MaxCols
i = i + 1
MyVar = Datarange(Irow, Icol)
If Left(MyVar, 1) = "<" Then
MyVar = (Right(MyVar, Len(MyVar) - 1)) / 2
arrEr(k) = sh.Cells(Irow, Icol).Address(0, 0): k = k + 1
Datarange(Irow, Icol) = MyVar
End If
Next Icol
Next Irow
If k > 0 Then
ReDim Preserve arrEr(k - 1)
sh.Range("A1").CurrentRegion = Datarange
With sh.Cells(2, MaxCols + 1)
.Offset(-1).Value = "Errors"
.Resize(k, 1).Value = Application.Transpose(arrEr)
End With
Else
MsgBox "Nothing to be corrected..."
End If
End Sub

VBA: faster way to compare two ranges?

I need to compare two ranges and see if value in one range appears in the other. This is the code I use:
Dim rng1 As Range
Dim rng2 As Range
Dim cell as Range
Dim found as Range
set rng1 = ....
set rng2 = ....
for each cell in rng1
set found = rng2.Find(what:=cell,.....
Next cell
This code is OK if the range is in thousands of rows, single column. When it comes to tens of thousands, it's very slow.
Anyway to speed it up?
This might be the fastest way for large amounts of data:
Option Explicit
Sub Test()
Dim rng1 As Range
Set rng1 = YourShorterRange
Dim rng2 As Range
Set rng2 = YourLargerRange
Dim C As Range
Dim Matches As Object: Set Matches = CreateObject("Scripting.Dictionary")
'input the larger data inside a dictionary
For Each C In rng2
If Not Matches.Exists(C.Value) Then Matches.Add C.Value, 1
Next C
Dim i As Long
Dim arr As Variant
'input the shorter data inside an array
arr = rng1.Value
For i = 1 To UBound(arr)
If Matches.Exists(arr(i, 1)) Then
'your code if the value is found
End If
Next i
End Sub
Edit for Dorian:
Option Explicit
Sub Test()
Dim rng1 As Range
Set rng1 = YourShorterRange
Dim rng2 As Range
Set rng2 = YourLargerRange
Dim i As Long, j As Long
Dim arr As Variant
Dim Matches As Object: Set Matches = CreateObject("Scripting.Dictionary")
arr = rng1.Value
'input the larger data inside a dictionary
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If Not Matches.Exists(arr(i, j)) Then Matches.Add arr(i, j), 1
Next j
Next i
'input the shorter data inside an array
arr = rng2.Value
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If Matches.Exists(arr(i, j)) Then
'your code if the value is found
End If
Next j
Next i
End Sub
Maybe something along these lines:
Sub Test()
Dim arr1 As Variant, arr2 As Variant
Dim arrList As Object: Set arrList = CreateObject("System.Collections.ArrayList")
Dim x As Long
arr1 = rng1 'Specify your range
arr2 = rng2 'Specify your range
For x = LBound(arr2) To UBound(arr2)
arrList.Add arr2(x, 1)
Next x
For x = LBound(arr1) To UBound(arr1)
If arrList.contains(arr1(x, 1)) = True Then
Debug.Print arr1(x, 1) & " contained within range 2"
End If
Next x
End Sub
I would suggest you :
Application.match
You can also look here you will find an interesting studies on 3 different ways of Search. Those 3 Different way will be studied By Time and By number of occurences.
According Fastexcel the conclusion of this study is :
Don’t use Range.Find unless you want to search a large number of
columns for the same thing (you would have to do a Match for each
column).
The Variant array approach is surprisingly effective,
particularly when you expect a large number of hits.
Match wins easily
for a small number of hits.
So If you except a large number of hit you might have to give a try variant array method. The 3 ways are listed in Fastexcel tuto
Edit
After reading some comment I did a new test :
Variant code
Sub Test1()
Dim vArr As Variant
Dim j As Long
Dim n As Long
Dim dTime As Double
dTime = MicroTimer
vArr = Range("A1:B100000").Value2
Dim Matches As Object: Set Matches = CreateObject("Scripting.Dictionary")
arr = Range("G1:G15").Value
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If Not Matches.Exists(arr(i, j)) Then Matches.Add arr(i, j), 1
Next j
Next i
For j = LBound(vArr) To UBound(vArr)
If Matches.Exists(vArr(j, 1)) Or Matches.Exists(vArr(j, 2)) Then n = n + 1
Next j
Debug.Print "Using Variant : " & n & " Timer :" & (MicroTimer - dTime) * 1000
End Sub
Dictionary
Sub Test()
Dim rng1 As Range
Set rng1 = Range("A1:B100000")
Dim rng2 As Range
Set rng2 = Range("G1:G15")
Dim i As Long, j As Long
Dim arr As Variant
Dim dTime As Double
dTime = MicroTimer
Dim Matches As Object: Set Matches = CreateObject("Scripting.Dictionary")
arr = rng2.Value
'input the larger data inside a dictionary
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If Not Matches.Exists(arr(i, j)) Then Matches.Add arr(i, j), 1
Next j
Next i
'input the shorter data inside an array
arr = rng1.Value
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If Matches.Exists(arr(i, j)) Then
'your code if the value is found
cpt = cpt + 1
End If
Next j
Next i
Debug.Print "Using Damian Method : " & cpt & " Timer : " & (MicroTimer - dTime) * 1000
End Sub

How to fix: compare two sheets code to output ENTIRE ROW matches only?

I have a code that takes two sheets, compares them, and outputs the matches to another sheet. The code works fine but the only problem is that it outputs matches that are from any column. For example; if column A equals "Cab" in sheet 1 & column A equals "Cab" in sheet 2, it outputs the row as a match. What I'm trying to have the code do is check for a FULL ROW match, so that if every value in all columns of a row matches the entire row of the other sheet, then output those rows.
My current code:
Sub CompareSolve()
Dim i As Long
Dim j As Long
Dim n As Long
Dim ar As Variant
ar = Sheet2.Cells(10, 1).CurrentRegion.Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 2 To UBound(ar, 1)
.Item(ar(i, 1)) = Empty
Next
ar = Sheet1.Cells(10, 1).CurrentRegion.Value
n = 1
For i = 2 To UBound(ar, 1)
If .exists(ar(i, 1)) Then
n = n + 1
For j = 1 To UBound(ar, 2)
ar(n, j) = ar(i, j)
Next j
End If
Next i
End With
Sheet3.Cells(10, 8).Resize(n, UBound(ar, 2)).Value = ar
End Sub
Any ideas on how I can modify this to work?
*EDIT:
before:
after:
Sub CompareSolve()
Dim arr As Variant, wsa As Worksheet, lra As Long, lca As Long
Dim brr As Variant, wsb As Worksheet, lrb As Long, lcb As Long
Set wsa = Sheets(1) 'starting sheet
With wsa
lra = .Cells(.Rows.Count, 1).End(xlUp).Row
lca = .Cells(10, .Columns.Count).End(xlToLeft).Column
arr = .Range(.Cells(10, 1), .Cells(lra, lca)).Value
End With
Set wsb = Sheets(2) 'sheet to match against
With wsb
lrb = .Cells(.Rows.Count, 1).End(xlUp).Row
lcb = .Cells(10, .Columns.Count).End(xlToLeft).Column
brr = .Range(.Cells(10, 1), .Cells(lrb, lcb)).Value
End With
If Not lca = lcb Then Exit Sub
'
Dim i As Long, j As Long, r As Long, k As Long
For r = LBound(arr) To UBound(arr)
For i = LBound(brr) To UBound(brr)
For j = 10 To lcb
If brr(i, j) = arr(r, j) Then
If j = lca Then wsa.Cells(r, lca + 1).Value = i
k = 1
Exit For 'exit j
Else
Exit For 'exit j
End If
Next j
If k = 1 Then Exit For 'exit i
Next i
k = 0
Next r
End Sub
Can do a slightly different approach to this with variant array and exit for (untested code to give the concept)
Dim arr as variant, wsa as worksheet, lra as long, lca as long
Dim brr as variant, wsb as worksheet, lrb as long, lcb as long
set wsa = sheets(1) 'starting sheet
with wsa
lra = .cells(.rows.count,1).end(xlup).row
lca = .cells(1, .columns.count).end(xltoleft).column
arr = .range(.cells(1,1),.cells(lra,lca)).value
end with
set wsb = sheets(2) 'sheet to match against
with wsb
lrb = .cells(.rows.count,1).end(xlup).row
lcb = .cells(1, .columns.count).end(xltoleft).column
brr = .range(.cells(1,1),.cells(lrb,lcb)).value
end with
if not lca = lcb then exit sub
'
Dim i as long, j as long, r as long, k as long
for r = lbound(arr) to ubound(arr)
for i = lbound(brr) to ubound(brr)
for j = 1 to lcb
if brr(i,j) = arr(r,j) then
If j = lca Then wsa.Cells(r, lca + 1).Value = i
k = 1
exit for 'exit j
else
exit for 'exit j
end if
next j
if k = 1 then exit for 'exit i
next i
k = 0
next r
general concept compare row (r) to row (i) by comparing col/col; if the first column isn't a match, then exit that for loop and move to the next comparison, row (r) and row (i+1).
edit1
Moved a misplaced k = 1 inside the true-scenario of the if-statement to skip to the next r when a match has been found

VBA - Array assign values For loop

I have to loop search multiple ranges and find match to 100k + records. Problem is I get mismatch error when assigning value to variant Arr2(i, 1).
Dim Arr1, Arr2 As Variant
Dim Wks0, Wks1 As Worksheet
Dim i As Integer
Dim Row0, Row1 As Long
Dim C As Object
Set Wks0 = Sheets("HOST")
Set Wks1 = Sheets("OFICI_BANC_USA")
'-- Create array of range -------------------------------------------*
Row0 = Wks0.Cells(Rows.Count, "A").End(xlUp).Row
Row1 = Wks1.Cells(Rows.Count, "A").End(xlUp).Row
Arr1 = Wks1.Range("A2:A" & Row1)
'-- Loop create value on sheet OFIC_BANC_USA found value in sheet HOST -----*
For i = 1 To 5 'UBound(Arr1)
With Wks0.Range("A2:A" & Row0)
Set C = .Find(Arr1(i, 1), LookAt:=xlPart,SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not C Is Nothing Then
'ReDim Preserve Arr2(i, 1)
Arr2(i, 1) = "OK"
Else
Arr2(i, 1) = "NO"
End If
End With
Next
' Transpose new array onto worksheet -------------------------------*
Wks1.Range("B2:B6") = WorksheetFunction.Transpose(Arr2)
'Arr1 = Nothing
'Arr2 = Nothing
I think you want to deal with a two-dimensioned array for the values coming in from wks1 (since you have no choice in the matter) and a single dimensioned array to hold the OK / NO values before stuffing them back into the worksheet.
Sub t()
Dim Arr0() As Variant, Arr1() As Variant, Arr2() As Variant
Dim Wks0 As Worksheet, Wks1 As Worksheet
Dim i As Long
Dim Row0 As Long, Row1 As Long
Dim C As Range
Set Wks0 = Sheets("HOST")
Set Wks1 = Sheets("OFICI_BANC_USA")
'-- Create array of range -------------------------------------------*
Row0 = Wks0.Cells(Wks0.Rows.Count, "A").End(xlUp).Row
Row1 = Wks1.Cells(Wks1.Rows.Count, "A").End(xlUp).Row
Arr1 = Wks1.Range("A2:A" & Row1)
'-- Loop create value on sheet OFIC_BANC_USA found value in sheet HOST -----*
For i = 1 To UBound(Arr1, 1)
With Wks0.Range("A2:A" & Row0)
Set C = .Find(Arr1(i, 1), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
ReDim Preserve Arr2(i) '<~~ NOTE ReDim single dimensioned array here!
If Not C Is Nothing Then
Arr2(i) = "OK"
Else
Arr2(i) = "NO"
End If
End With
Next
' Transpose new array onto worksheet -------------------------------*
Wks1.Range("B2").Resize(UBound(Arr2), 1) = WorksheetFunction.Transpose(Arr2)
End Sub
Note where I've redimmed arr2. It's going to get a value either way so you need to extend its size in preparation to receive an OK / NO.
Scripting.Dictionary
Sub tt()
Dim arr As Variant, dHOST As Object
Dim Wks0 As Worksheet, Wks1 As Worksheet
Dim i As Long, j As Long
Dim Row0 As Long, Row1 As Long
Dim c As Range, rHOST As Range
Debug.Print Timer
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set Wks0 = Worksheets("HOST")
Set Wks1 = Sheets("OFICI_BANC_USA")
Set dHOST = CreateObject("Scripting.Dictionary")
dHOST.CompareMode = vbTextCompare
'-- Create dictionary of HOST range --------------------------
Row0 = Wks0.Cells(Wks0.Rows.Count, "A").End(xlUp).Row
arr = Wks0.Range("A2:D" & Row0).Value2
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
'If Not dHOST.Exists(arr(i, j)) Then _
dHOST.Item(arr(i, j)) = j '<~~ for first match (adds 1½ seconds)
dHOST.Item(arr(i, j)) = j '<~~ for overwrite match
Next j
Next i
'-- Create array of OFICI_BANC_USA range ----------------------
Row1 = Wks1.Cells(Wks1.Rows.Count, "A").End(xlUp).Row
arr = Wks1.Range("A2:E" & Row1).Value2
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) + 1 To UBound(arr, 2)
arr(i, j) = "NO" '<~~ seed all NO matches
Next j
Next i
'-- Loop arrayed values from sheet OFIC_BANC_USA found value in dictionary HOST values --
For i = LBound(arr, 1) To UBound(arr, 1)
If dHOST.Exists(arr(i, 1)) Then _
arr(i, dHOST.Item(arr(i, 1)) + 1) = "OK"
Next i
' Stuff it all back into worksheet -------------------------------*
With Wks1.Range("A2:E" & Row1)
.Cells = arr
End With
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Debug.Print Timer
End Sub
200K records in column A of OFICI_BANC_USA worksheet
4 columns # 50K rows each in HOSTS worksheet
~76% match rate
14.73 seconds start-to-finish
In addition to #VincentG's comment, you need to explicitly state which Rows you're using. Also, I uncommented the ReDim, and it seems to be working now:
Sub t()
Dim Arr0() As Variant, Arr1() As Variant, Arr2() As Variant
Dim Wks0 As Worksheet, Wks1 As Worksheet
Dim i As Integer
Dim Row0 As Long, Row1 As Long
Dim C As Object
Set Wks0 = Sheets("HOST")
Set Wks1 = Sheets("OFICI_BANC_USA")
'-- Create array of range -------------------------------------------*
Row0 = Wks0.Cells(Wks0.Rows.Count, "A").End(xlUp).Row
'Arr0 = Wks0.Range("A2:A" & Row0)
Row1 = Wks1.Cells(Wks1.Rows.Count, "A").End(xlUp).Row
Arr1 = Wks1.Range("A2:A" & Row1)
'-- Loop create value on sheet OFIC_BANC_USA found value in sheet HOST -----*
For i = 1 To 5 'UBound(Arr1)
With Wks0.Range("A2:A" & Row0)
Set C = .Find(Arr1(i, 1), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not C Is Nothing Then
ReDim Preserve Arr2(i, 1)
Arr2(i, 1) = "OK"
Else
Arr2(i, 1) = "NO"
End If
End With
Next
' Transpose new array onto worksheet -------------------------------*
Wks1.Range("B2:B6") = WorksheetFunction.Transpose(Arr2)
'Arr0 = Nothing
'Arr1 = Nothing
'Arr2 = Nothing
End Sub
I think I am understanding what you are trying to do. I set my two sheets up like this:
Then using the following code:
Sub jorge()
Application.ScreenUpdating = False
Dim Arr1 As Variant, Arr2 As Variant, Arr3 As Variant
Dim Wks0 As Worksheet, Wks1 As Worksheet
Dim i As Long, j As Long, k As Long
Dim Row0 As Long, Row1 As Long
Set Wks0 = Sheets("HOST")
Set Wks1 = Sheets("OFICI_BANC_USA")
'-- Create array of range -------------------------------------------*
Row0 = Wks0.Cells(Rows.Count, "A").End(xlUp).Row
Row1 = Wks1.Cells(Rows.Count, "A").End(xlUp).Row
Arr1 = Wks1.Range("A2:A" & Row1)
ReDim Arr2(1 To Row1, 1 To 4)
Arr3 = Wks0.Range("A2:D" & Row0)
'-- Loop create value on sheet OFIC_BANC_USA found value in sheet HOST -----*
For i = 1 To UBound(Arr1, 1)
For j = 1 To UBound(Arr3, 2)
Arr2(i, j) = "NO"
For k = 1 To UBound(Arr3, 1)
If Arr3(k, j) = Arr1(i, 1) Then
Arr2(i, j) = "OK"
Exit For
End If
Next k
Next j
Next i
Wks1.Range("B2").Resize(Row1, 4).value = Arr2
Application.ScreenUpdating = true
End Sub
I get this:
This formula will do the same thing, put this in B2:
=IF(ISNUMBER(MATCH($A2,HOST!A:A,0)),"OK","NO")
Copy across and down. This may be prohibitive with the sheer number of calculations, but it is here if you want to try it.

Resources