matt1107

New Member
Joined
Mar 20, 2017
Messages
4
Hi All,

I have a large data set in sheet1 which has ~20 columns and 10000+rows and each row has a set of limits based on what value is in column B this data set changes ~once per week.

The limits are stored in sheet2 where column A contains the unique list of values from column B in sheet1 and the min and max limits are stored over ~40 rows to the right of the value.

Easiest way I can think to explain is below example I have searched a lot and can't come up with the answer but I am currently doing this with index match then a formula to check if it is within the limits but it takes an age to load so i wish to do this with vba so i can update it as and when and if I need to leave it to run for a bit I can; but the sheet would then be usable and quick afterwards.

example sheet 1:

header 1header 2header 3header 4
random value1A12
random value2B42
random value3A63

<tbody>
</tbody>


example sheet 2:

sheet 1 header 2 valuemin limit for header 3 sheet 1max limit for header 3 sheet 1min limit for header 4 sheet 1max limit for header 4 sheet 1
A3914
B3758

<tbody>
</tbody>


result:

header 1header 2header 3header 4min limit for header 3 sheet 1max limit for header 3 sheet 1min limit for header 4 sheet 1max limit for header 4 sheet 1within limits header 3within limits header 4within limits overall
random value1A123914noyesfail
random value2B423758yesnofail
random value3A633914yesyespass

<tbody>
</tbody>


I hope this makes sense as this problem is driving me crazy as it takes me a long time to do anything with the data as I have to review the data on an almost daily basis.

Thanks in advance to any help I can get as I am fairly new to vba but I'm sure this is where the answer lies.

Regards,
Matt
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
OK so progress update I have done a bit more digging and used some different phrases for searches and have found this thread VBA Macro To Find Value & Copy Corresponding Cell and got the following code (below) working to find the value (header 2 in original post and go down for the whole column) and copy 1 value (i.e. the very first min limit) and paste in it the correct place!!! YAY!!! However I need to copy a range of ~50 columns and can't figure out how to adjust the code accordingly...any help?

code below

'=========================================================
'- GENERIC LOOKUP MACRO TO
'- FIND A VALUE IN ANOTHER WORKSHEET
'- AND RETURN A VALUE FROM ANOTHER COLUMN
'=========================================================
'- select the cell containing the first search value
'- and run this macro from there.
'- can be set to continue down the column
'- [** need to make changes below as required **]
'- Brian Baulsom May 2005
'==========================================================
Dim MyValue As Variant
Dim FromSheet As Worksheet
Dim LookupColumn As Integer
Dim FromRow As Long
Dim FromColumn As Integer
'-
Dim ToSheet As Worksheet
Dim StartRow As Long
Dim LastRow As Long
Dim ActiveColumn As Integer
Dim ReturnColumnNumber
Dim ToRow As Long
Dim FoundCell As Object

'=============================================================
'- MAIN ROUTINE
'=============================================================
Sub DO_LOOKUP()
Application.Calculation = xlCalculationManual
'----------------------------------------------------------
'- LOOKUP SHEET [**AMEND AS REQUIRED**]
Set FromSheet = Workbooks("Feb 2017 new2 (2).xlsm").Worksheets("Limits")
LookupColumn = 1 ' look for match here
FromColumn = 2 ' return value from here
'-----------------------------------------------------------
'- ACTIVE SHEET
Set ToSheet = ActiveSheet
ActiveColumn = ActiveCell.Column
StartRow = ActiveCell.Row
'-------------------------------------------------------------
'- COMMENT OUT UNWANTED LINE, UNCOMMENT THE OTHER
'- ..............................[** FOR MULTIPLE ROWS **]
LastRow = ToSheet.Cells(65536, ActiveColumn).End(xlUp).Row
'-
'- ..............................[** FOR A SINGLE VALUE **]
'-LastRow = ActiveCell.Row
'-------------------------------------------------------------
'- COLUMN NUMBER TO PUT RETURNED VALUE [**AMEND AS REQUIRED**]
ReturnColumnNumber = 100 ' column number
'-------------------------------------------------------------
'- loop through each row (which may be only 1)
For ToRow = StartRow To LastRow
MyValue = ToSheet.Cells(ToRow, ActiveColumn).Value
FindValue
Next
'-------------------------------------------------------------
'- finish
MsgBox ("Done")
Application.Calculation = xlCalculationAutomatic
End Sub
'== END OF PROCEDURE ====================================================

'========================================================================
'- FIND VALUE
'========================================================================
Private Sub FindValue()
Set FoundCell = _
FromSheet.Columns(LookupColumn).Find(MyValue, LookIn:=xlValues)
If FoundCell Is Nothing Then
'- MsgBox (MyValue & " not found.")
Else
FromRow = FoundCell.Row
'- transfer additional data.
ToSheet.Cells(ToRow, ReturnColumnNumber).Value = _
FromSheet.Cells(FromRow, FromColumn).Value
End If
End Sub
'=========================================================================
 
Upvote 0
Figured it out, took me a bit but i got there....see code attached if anyone is interested. :)

'=========================================================
'- GENERIC LOOKUP MACRO TO
'- FIND A VALUE IN ANOTHER WORKSHEET
'- AND RETURN A VALUE FROM ANOTHER COLUMN
'=========================================================
'- select the cell containing the first search value
'- and run this macro from there.
'- can be set to continue down the column
'- [** need to make changes below as required **]
'- Brian Baulsom May 2005
'==========================================================
Dim MyValue As Variant
Dim FromSheet As Worksheet
Dim LookupColumn As Integer
Dim FromRow As Long
Dim FromColumn As Integer
'-
Dim ToSheet As Worksheet
Dim StartRow As Long
Dim LastRow As Long
Dim ActiveColumn As Integer
Dim ReturnColumnNumber
Dim ToRow As Long
Dim FoundCell As Object

'=============================================================
'- MAIN ROUTINE
'=============================================================
Sub DO_LOOKUP()
Application.Calculation = xlCalculationManual
'----------------------------------------------------------
'- LOOKUP SHEET [**AMEND AS REQUIRED**]
Set FromSheet = Workbooks("Feb 2017 new2 (2).xlsm").Worksheets("Limits")
LookupColumn = 1 ' look for match here
FromColumn = 2 ' return value from here
'-----------------------------------------------------------
'- ACTIVE SHEET
Set ToSheet = ActiveSheet
ActiveColumn = ActiveCell.Column
StartRow = ActiveCell.Row
'-------------------------------------------------------------
'- COMMENT OUT UNWANTED LINE, UNCOMMENT THE OTHER
'- ..............................[** FOR MULTIPLE ROWS **]
LastRow = ToSheet.Cells(65536, ActiveColumn).End(xlUp).Row
'-
'- ..............................[** FOR A SINGLE VALUE **]
'-LastRow = ActiveCell.Row
'-------------------------------------------------------------
'- COLUMN NUMBER TO PUT RETURNED VALUE [**AMEND AS REQUIRED**]
ReturnColumnNumber = 100 ' column number
'-------------------------------------------------------------
'- loop through each row (which may be only 1)
For ToRow = StartRow To LastRow
MyValue = ToSheet.Cells(ToRow, ActiveColumn).Value
FindValue
Next
'-------------------------------------------------------------
'- finish
MsgBox ("Done")
Application.Calculation = xlCalculationAutomatic
End Sub
'== END OF PROCEDURE ====================================================

'========================================================================
'- FIND VALUE
'========================================================================
Private Sub FindValue()
Set FoundCell = _
FromSheet.Columns(LookupColumn).Find(MyValue, LookIn:=xlValues)
If FoundCell Is Nothing Then
'- MsgBox (MyValue & " not found.")
Else
FromRow = FoundCell.Row

'- transfer additional data RANGE. option 1(385 rows & 41 columns copied 17 seconds)


FromSheet.Select
Range(Cells(FromRow, FromColumn), Cells(FromRow, 41)).Select
Selection.Copy
ToSheet.Select
Cells(ToRow, ReturnColumnNumber).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

'- transfer additional data RANGE. option 2 (385 rows & 41 columns copied 49 seconds)

'- inumberofloops = 40
'- For i = 0 To inumberofloops
'- ToSheet.Cells(ToRow, ReturnColumnNumber).Offset(, i).Value = _
'- FromSheet.Cells(FromRow, FromColumn).Offset(, i).Value
'- Next i


'- transfer additional data SINGLE cell.

'- ToSheet.Cells(ToRow, ReturnColumnNumber).Value = _
'- FromSheet.Cells(FromRow, FromColumn).Value
End If
End Sub
'=========================================================================
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,287
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top