Related:
- Copy rows by column match condition
- Music match jukebox - Download - Audio playback
- How to delete rows and columns in word - Guide
- Tweetdeck remove column - Guide
- Air condition lidl - Home - Apps & Sites
- Position of mouse pointer does not match screen coordinates clicked - Guide
1 response
venkat1926
Posts
1863
Registration date
Sunday June 14, 2009
Status
Contributor
Last seen
August 7, 2021
811
May 11, 2011 at 05:21 AM
May 11, 2011 at 05:21 AM
not clear . what is meant by "sheet1.ENO=sheet2.ENO"
I suppose sheet11 has raju, ravi ramesh in column A2 to A4(row no 1 column headings)
and some data in columns B and C
in sheet 2
you have name "ramesh" in A2
you want to fill B and C in this sheet based on sheet 1
then you have to use vlookup function
post small extract of sheet 1 and sheet 2 as you want it to be.
I suppose sheet11 has raju, ravi ramesh in column A2 to A4(row no 1 column headings)
and some data in columns B and C
in sheet 2
you have name "ramesh" in A2
you want to fill B and C in this sheet based on sheet 1
then you have to use vlookup function
post small extract of sheet 1 and sheet 2 as you want it to be.
Nov 28, 2011 at 01:32 PM
Option Explicit
Private Const MASTER_ABCDEFG_COL_IDX As Integer = 7
Private Const DATA_ABCDEFG_COL_IDX As Integer = 5
Public Sub FindMatchingData()
Dim MasterWrkSh As Worksheet
Dim MasterColCount As Integer
Dim MasterRowCount As Long
Dim MasterCol As Integer
Dim MasterRow As Long
Dim DataWrkSh As Worksheet
Dim DataColCount As Integer
Dim DataRowCount As Long
Dim DataRow As Long
Dim CopyData As Integer
Dim PrintData As Integer
Dim Data() As Variant
' Prevent ScreenUpdating and Alerts to improve performance while running.
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Cursor = xlWait
' We use objects to store the WorkSheets we'll be working on to make altering the _
code easier if required.
Set MasterWrkSh = Sheets("Master")
Set DataWrkSh = Sheets("Data")
' Store the Row/Column limits. This prevents us from continuously checking these, _
improving performance.
MasterColCount = MasterWrkSh.UsedRange.Columns.Count
MasterRowCount = MasterWrkSh.UsedRange.Rows.Count
DataColCount = DataWrkSh.UsedRange.Columns.Count
DataRowCount = DataWrkSh.UsedRange.Rows.Count
' Prepare the dynamic array for data. If this is not done, the code will fall over.
ReDim Data(DataColCount, 0)
' These nested For loops, check each row on the Master and Data WorkSheets and _
compare the PSN columns. Where a match is found, the data for the equivalent _
row on the Data WorkSheet is stored in the Data array, which we will use later _
copy the data over to the Master WorkSheet.
For MasterRow = 1 To MasterRowCount
' These 2 lines simply update the StatusBar so the user knows what stage the _
"query" is at and also free's the CPU, preventing crashes and freezes should _
the user wish to cancel the code. _
NOTE: The running of the code can be cancelled by pressing Ctrl + Pause/Break.
Application.StatusBar = Format(MasterRow / MasterRowCount, "0 %")
DoEvents
For DataRow = 1 To DataRowCount
' This is the If statement that checks whether we have a match.
If MasterWrkSh.Cells(MasterRow, MASTER_PSN_COL_IDX) = _
DataWrkSh.Cells(DataRow, DATA_PSN_COL_IDX) Then
ReDim Preserve Data(DataColCount, UBound(Data, 2) + 1)
Data(0, UBound(Data, 2)) = MasterRow
For CopyData = 1 To DataColCount
Data(CopyData, UBound(Data, 2)) = DataWrkSh.Cells(DataRow, CopyData)
Next
' As we found a match, we can Exit the For loop as we don't need to check _
for further matches.
Exit For
End If
Next
Next
' Now we've compaired all the data we need to, these nested For loops will "paste" _
this data on the Master WorkSheet. _
NOTE: No StatusBar updates are provided during this process. If the process takes _
an excessive amount of time, it may be beneficial to change this.
For PrintData = 1 To UBound(Data, 2)
For MasterCol = 1 To UBound(Data, 1)
MasterWrkSh.Cells(Data(0, PrintData), MasterCol + MasterColCount) = Data(MasterCol, PrintData)
Next
Next
' Return full control to Excel and refresh the sheet.
Application.StatusBar = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Cursor = xlDefault
' Free up resources.
Set DataWrkSh = Nothing
Set MasterWrkSh = Nothing
End Sub