Alternative to Vlookup using macros

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
229
Office Version
  1. 365
Platform
  1. Windows
I am hoping this wonderful community can help me once again! with one problem of using Copy paste to paste on criteria from one work sheet another has been solved. i found that becuase my SOURCE worksheet for this task is 1000+ rows and 22 columns of Vlookups referencing a master file who will be overwritten as needed based on information we get from customers. This is causing a very long processing time for the newly added copy paste macro to work. i have found that if my Sheet 1 source data is just text it works INSTANTLY.

my solution needs to be my source sheet also needs to be a copy pasted data from our Masterfile instead of Vlookups. and as it wont be as easy as using the same macro.

I want to copy and paste all rows of data from my master file but i want to EXCLUDE certain columns because this information is irrelevent.

Source: Master file has headers in Row 1, 39 Columns and then Data below that about 1000+ rows of it and grows over time
Destination: Current working file "Sheet1" Same headers in Row 1 but only 23 of the columns of information that we want in this sheet

how would i target in VBA to only copy those specified columns and give me all the corresponding information below that column.

heres headers from source file

ToyotaData.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAM
1QIMS#Doc TypeInstance Handle KeyRankSupplier CodeTMMC Supplier CodeSupplier NamePart NamePart NumberOverall StatusNCD DescriptionQENameFull NameFull NameModelSQA RankLT CM Plan DueOriginal LTCM Actual Due DateRevised LTCM Actual Due DateCloseInitial Issuance DateOfficial Issuance DateLTCM Plan SubmittedLTCM Plan Accepted DateLTCM Plan Rejection DateLTCM Actual SubmittedLTCM Actual Accepted DateLTCMActualRejectionDateOccurenceWhere NC found?Will Parts Be Quarantined ?Why Made CategoryWhy Shipped CategoryNAMCST/CM StatusLT/CM StatusStandardActualAssigned To
Data


heres what i want to get to

Customer database test 2.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1QIMS#Doc TypeRankSupplier CodePart NamePart NumberOverall StatusNCD DescriptionModelLT CM Plan DueOriginal LTCM Actual Due DateRevised LTCM Actual Due DateCloseInitial Issuance DateOfficial Issuance DateLTCM Plan SubmittedLTCM Plan Accepted DateLTCM Plan Rejection DateLTCM Actual SubmittedLTCM Actual Accepted DateLTCMActualRejectionDateNAMC
Sheet1


I apologize but due to nature of the info i cant publically share it. just assume there 1000+ rows of data in each cell below up and that will grow over time so lastrow will always be different.
 
Yes i tried that code and it would threw up Run time error 9 at
Set srcSH = Workbooks("ToyotaData.xlsx").Sheets("Data")

any ideas?
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Let's save the step of opening the file, first open the file and try the macro:
 
Upvote 0
Forget the previous post, I put here the code to open the file, try this new code.

VBA Code:
Sub Update_trial_3()
  Dim srcSH As Worksheet, desSH As Worksheet
  Dim i As Long, j As Long, nRow As Long, n As Long
  Dim rng As Range, col As Range, c As Range, f As Range
  
  Workbooks.Open ("O:\1_All Customers\Current Complaints\ToyotaData.xlsx")
  Set srcSH = Workbooks("ToyotaData.xlsx").Sheets("Data")
  Set desSH = Workbooks("Customer Database Test 2.xlsm").Sheets("sheet1")
  Set rng = srcSH.Range("A:B,D:E,H:K,O:O,Q:AB,AH:AH")
  
  For Each c In srcSH.Range("A2", srcSH.Range("A" & Rows.Count).End(3))
    Set f = desSH.Range("A:A").Find(c.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then nRow = f.Row Else nRow = desSH.Range("A" & Rows.Count).End(3).Row + 1
    j = 0
    For Each col In rng.Columns
      n = col.Column
      j = j + 1
      desSH.Cells(nRow, j).Value = srcSH.Cells(c.Row, n).Value
    Next
  Next
  srcSH.Parent.Close False
End Sub
 
Upvote 0
I was ahead of you on that gonna do that and the closing bit and see what happens aaanndd itt WORKS!!!!

I added a fake record and some extra dates and it picked up the new dates and added them in and changed the status and all updated correctly!

Thank you so much! this is resolved do you think theres any benefit to adding screenupdate true and false and begining or end?
 
Upvote 0
this is resolved do you think theres any benefit to adding screenupdate
Of course.

VBA Code:
Sub Update_trial_3()
  Dim srcSH As Worksheet, desSH As Worksheet
  Dim i As Long, j As Long, nRow As Long, n As Long
  Dim rng As Range, col As Range, c As Range, f As Range
  
  Application.ScreenUpdating = False
  
  Workbooks.Open ("O:\1_All Customers\Current Complaints\ToyotaData.xlsx")
  Set srcSH = Workbooks("ToyotaData.xlsx").Sheets("Data")
  Set desSH = Workbooks("Customer Database Test 2.xlsm").Sheets("sheet1")
  Set rng = srcSH.Range("A:B,D:E,H:K,O:O,Q:AB,AH:AH")
  
  For Each c In srcSH.Range("A2", srcSH.Range("A" & Rows.Count).End(3))
    Set f = desSH.Range("A:A").Find(c.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then nRow = f.Row Else nRow = desSH.Range("A" & Rows.Count).End(3).Row + 1
    j = 0
    For Each col In rng.Columns
      n = col.Column
      j = j + 1
      desSH.Cells(nRow, j).Value = srcSH.Cells(c.Row, n).Value
    Next
  Next
  srcSH.Parent.Close False

  Application.ScreenUpdating = True

End Sub
 
Upvote 0
So i tried to add this into "Thisworkbook" as Workbook Open so that it would run automatically when i opened the workbook but i get an error any reason why it will function correctly as a module but not in "Thisworkbook"? Specifically i get a Compile Error
 
Upvote 0
So i tried to add this into "Thisworkbook" as Workbook Open so that it would run automatically when i opened the workbook but i get an error any reason why it will function correctly as a module but not in "Thisworkbook"? Specifically i get a Compile Error
Exactly what you want to do. Write here what the situation is. Which of the 2 books will contain the macro. Which book is going to be open, which one is going to be closed, etc.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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