OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 421
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for your assistance. I am attempting to write a generic VBA Code to pull data from one sheet into another based on the first column in each data set. I am currently using a Range Find Method, but that takes too long as the data set is very large and trying to make it more efficient.
The first row in each data set is the header. The pull will be based on what's in the first column and may not be pulling every column and the columns may not be in the same order. A sample data set is in the image. A sample code to start off is below and is very generic so I can use with all data sizes and sets.
The first row in each data set is the header. The pull will be based on what's in the first column and may not be pulling every column and the columns may not be in the same order. A sample data set is in the image. A sample code to start off is below and is very generic so I can use with all data sizes and sets.
VBA Code:
Option Explicit
'***************************************************************************************************************
Sub GetData()
'_______________________________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlManual
'_______________________________________________________________________________________________________________
'Dimensioning
'Dim longs
Dim FRSA As Long
Dim LRSA As Long
Dim FCSA As Long
Dim LCSA As Long
'Dim strings
Dim ShtNmSA As String
Dim ShtNmDT As String
'______________________________________________________________________________________________________________
'Code -
ShtNmSA = "SA" 'name of sheet with the source data
ShtNmDT = "Data" 'name of the sheet which needs to pull
With Sheets(ShtNmSA)
FRSA = .Cells.Find(What:="*", After:=Cells(Rows.Count, Columns.Count), _
LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Row
'First row in SA which will be a header row
LRSA = .Cells.Find(What:="*", After:=.Cells(1), SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'Last row in SA
FCSA = .Cells.Find(What:="*", After:=Cells(Rows.Count, Columns.Count), _
LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Column
'First row in SA which will be a header row
LCSA = .Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
'last column in SA
End With
With Sheets(ShtNmDT)
FRDT = .Cells.Find(What:="*", After:=Cells(Rows.Count, Columns.Count), _
LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Row
'First row in SA which will be a header row
LRDT = .Cells.Find(What:="*", After:=.Cells(1), SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'Last row in SA
FCDT = .Cells.Find(What:="*", After:=Cells(Rows.Count, Columns.Count), _
LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Column
'First row in SA which will be a header row
LCDT = .Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
'last column in SA
End With
'_________________________________________________________________________________________________________________
'Code to get data
'_________________________________________________________________________________________________________________
'Turn on alerts and screen updates, and calculate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Calculate
End Sub