473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

[Excel/VBA ] How to Find and copy data(Mapped data) from another worksheet using VBA?

Hi all,

I am new to Excel/VBA and would require your help.
I have stuck again somewhere and will be highly obliged if you can help me.

I have two worksheet
1. Import File Utility (Sheet A)
2. TeamCenterVsFileMapping (Sheet B)

In Sheet A there are five columns(File Path, File Type, Dataset Type, Dataset Name, Named Reference) and "Import" button. if user clicks on Import button and select any folder, then Filepath, Filetype, Dataset Name(i.e Filename) corresponding to all files are imported in the Sheet A's Cells. But i also need to import values corresponding to Dataset Type and Named Reference. These values are fetching from Sheet B i.e TeamCenterVsFileMapping.
There is a mapping of Filetype Vs Dataset Type Vs Named Reference in Sheet B just like:


FileType ---- DatasetType ---- NamedReference
------------------------------------------------
zip ---- zipAddress ---- MScompression
accdb ---- Access ---- MSAccess
jpeg ---- Image ---- Xyz
jpg ---- Image ---- XXX
pdf ---- Acrobat ---- YYY


No what i want for each filetype in Sheet A, it will search File Type in Sheet B and if it finds then copy the Dataset Type and Named Reference value from Sheet B to Sheet A.

i have been able to do it but its:
1. When i import files, these two values are not getting imported for all files (but for few files they are getting imported).
2. However if i import same folder second time then all fields and their value are getting imported.
See the code below
Expand|Select|Wrap|Line Numbers
  1. Sub GetFileList()
  2. ChDrive "M"
  3. ChDir "M:\Certificates"
  4.      'Const cStartRow As Long = 2
  5.      Const cFPathCol As Long = 1
  6.      Const cExtentionCol As Long = 2
  7.      Const cDatasetType As Long = 3
  8.      Const cFNameCol As Long = 4
  9.      Const cNamedReference As Long = 5
  10.      Const cLog As Long = 6
  11.  
  12.      Dim ThisFolder As String
  13.      Dim ThisFile As String
  14.      Dim FileName As String
  15.      Dim Extention As String
  16.      Dim i As Long
  17.  
  18.     If SelectDirectoryOK(ThisFolder) Then
  19.          ThisFile = Dir(ThisFolder & "\*.*")
  20.          i = Cells(50000, cFPathCol).End(xlUp).Offset(1, 0).Row
  21.          'i = cStartRow
  22.          Do Until ThisFile = ""
  23.  
  24.              FileName = Left(ThisFile, InStrRev(ThisFile, ".") - 1)
  25.              Extention = Mid(ThisFile, InStrRev(ThisFile, ".") + 1)
  26.  
  27.              Cells(i, cFPathCol) = ThisFolder & "\" & FileName & "." & Extention
  28.              Cells(i, cExtentionCol) = Extention
  29.              Cells(i, cFNameCol) = FileName
  30.              Call CopyTeamCentreValue(Extention, i)
  31.              'Cells(i, cDatasetType) = DataSetValue(Extention, i)
  32.              'Cells(i, cNamedReference) = NamedReferenceFunction(Extention)
  33.              Cells(i, cLog) = "import.log"
  34.              i = i + 1
  35.              ThisFile = Dir
  36.          Loop
  37.     End If
  38. End Sub
  39.  
  40.  Function SelectDirectoryOK(ByRef Directory As String, Optional InitialPath As String = "") As Boolean
  41.      SelectDirectoryOK = False
  42.  
  43.      With Application.FileDialog(msoFileDialogFolderPicker)
  44.          If InitialPath <> "" Then .InitialFileName = InitialPath
  45.          .Title = "Select File FOLDER"
  46.          .AllowMultiSelect = False
  47.          .Show
  48.          If .SelectedItems.Count = 0 Then Exit Function
  49.          Directory = .SelectedItems(1)
  50.      End With
  51.  
  52.      SelectDirectoryOK = True
  53.  End Function
  54.  
  55. Sub CopyTeamCentreValue(Extention As String, i As Long)
  56.   'Copy cells of cols A,F,E,D from rows containing "Significant" in
  57.   'col D of the active worksheet (source sheet) to cols
  58.   'A,B,C,D of Sheet2 (destination sheet)
  59.   Dim DestSheet As Worksheet
  60.   Const cDatasetType As Long = 3
  61.   Const cNamedReference As Long = 5
  62.   Dim SourceSheet As Worksheet
  63.   Set DestSheet = Worksheets("Import File Utility")
  64.   Set SourceSheet = Worksheets("TeamCenterVsFileMapping")
  65.  
  66.   Dim sRow As Long     'row index on source worksheet
  67.   Dim dRow As Long     'row index on destination worksheet
  68.   Dim sCount As Long
  69.   dRow = i
  70.   For sRow = 2 To Range("A65536").End(xlUp).Row
  71.      'use pattern matching to find "File Type" anywhere in cell
  72.      If SourceSheet.Cells(sRow, "A") = Extention Then
  73.         'copy cols A,F,E & D
  74.          'SourceSheet.Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, cDatasetType)
  75.         DestSheet.Cells(dRow, cDatasetType) = SourceSheet.Cells(sRow, "B")
  76.         'DestSheet.Cells(i, cNamedReference) = SourceSheet.Cells(sRow, "C")
  77.      End If
  78.   Next sRow
  79. End Sub
  80.  
  81.  
Jul 22 '10 #1

✓ answered by MikeTheBike

@prashantdixit
Hi again

Not to short this asnswers all you problems but I suggest you change the bit of code
Expand|Select|Wrap|Line Numbers
  1. For sRow = 2 To Range("A65536").End(xlUp).Row 
  2.      'use pattern matching to find "File Type" anywhere in cell 
  3.      If SourceSheet.Cells(sRow, "A") = Extention Then 
  4.         'copy cols A,F,E & D 
  5.          'SourceSheet.Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, cDatasetType) 
  6.         DestSheet.Cells(dRow, cDatasetType) = SourceSheet.Cells(sRow, "B") 
  7.         'DestSheet.Cells(i, cNamedReference) = SourceSheet.Cells(sRow, "C") 
  8.      End If 
  9.   Next sRow 
to this
Expand|Select|Wrap|Line Numbers
  1. For sRow = 2 To SourceSheet.Range("A65536").End(xlUp).Row
  2.      'use pattern matching to find "File Type" anywhere in cell
  3.      If Trim(UCase(SourceSheet.Cells(sRow, "A"))) = Trim(UCase(Extention)) Then
  4.         'copy cols A,F,E & D
  5.          'SourceSheet.Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, cDatasetType)
  6.         DestSheet.Cells(dRow, cDatasetType) = SourceSheet.Cells(sRow, "B")
  7.         'DestSheet.Cells(i, cNamedReference) = SourceSheet.Cells(sRow, "C")
  8.      End If
  9.   Next sRow
Using Trim() and UCase() will remove any ambiguity with leading and trailing spaces and make it case insensity.

I have also added the sourcesheet reference when finding the last row in the source sheet (as it was you were finding the last row in the currently active sheet, which is the destination sheet!).

Other than that, ity seem to work OK.

HTH


MTB

2 4121
MikeTheBike
639 Expert 512MB
@prashantdixit
Hi again

Not to short this asnswers all you problems but I suggest you change the bit of code
Expand|Select|Wrap|Line Numbers
  1. For sRow = 2 To Range("A65536").End(xlUp).Row 
  2.      'use pattern matching to find "File Type" anywhere in cell 
  3.      If SourceSheet.Cells(sRow, "A") = Extention Then 
  4.         'copy cols A,F,E & D 
  5.          'SourceSheet.Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, cDatasetType) 
  6.         DestSheet.Cells(dRow, cDatasetType) = SourceSheet.Cells(sRow, "B") 
  7.         'DestSheet.Cells(i, cNamedReference) = SourceSheet.Cells(sRow, "C") 
  8.      End If 
  9.   Next sRow 
to this
Expand|Select|Wrap|Line Numbers
  1. For sRow = 2 To SourceSheet.Range("A65536").End(xlUp).Row
  2.      'use pattern matching to find "File Type" anywhere in cell
  3.      If Trim(UCase(SourceSheet.Cells(sRow, "A"))) = Trim(UCase(Extention)) Then
  4.         'copy cols A,F,E & D
  5.          'SourceSheet.Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, cDatasetType)
  6.         DestSheet.Cells(dRow, cDatasetType) = SourceSheet.Cells(sRow, "B")
  7.         'DestSheet.Cells(i, cNamedReference) = SourceSheet.Cells(sRow, "C")
  8.      End If
  9.   Next sRow
Using Trim() and UCase() will remove any ambiguity with leading and trailing spaces and make it case insensity.

I have also added the sourcesheet reference when finding the last row in the source sheet (as it was you were finding the last row in the currently active sheet, which is the destination sheet!).

Other than that, ity seem to work OK.

HTH


MTB
Jul 22 '10 #2
Thanks again.
it worked
Jul 23 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Will | last post by:
On the subject of Data Warehouses, Data Cubes & OLAP…. I would like to speak frankly about Data Warehouses, Data Cubes and OLAP (on-line analytical processing). Has it dawned on anyone else...
3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
4
by: jnb0012 | last post by:
I am trying to run a query (in access) to retrieve information for several centers. I need to put this in excel. I'm sure this is an easy answer but I need the info for each center on it's own...
0
by: Weyhooi | last post by:
Help ! please help! i can't find the way to insert a new blank row into excel worksheet using C#. Example macro in vb6 as below : Rows("15:15").Select Selection.Insert Shift:=xlDown i have...
0
by: Alexander | last post by:
Because it's impossible to Server.Transfer custom HttpHandlers I need for workaround and search for Httphandler mapped to custom URI by hands. Is it possible to find which Httphandler mapped to...
4
by: system55 | last post by:
I need some help.... How can i create a program wherein it can copy a file about 2MB in size, into another file using the extended memory allocation??? Do i need to use some interrupt calls in dos??
0
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am learning how to use Excel to connect to other external data source (like database). What I want to do is to develop a plug-in for Excel and automatically access external...
0
by: Rowena P Porley | last post by:
Hi everyone, I hope someone can help me out! I have 2 tables with 2 related colums: CHARGE_BREAKDOWN with Approvalcode and td columns while MERCHANT_BANK has Appcode and trandate columns. The...
1
by: crystalgal | last post by:
Help. I am using Excel to enter in daily numbers in worksheet 2. I want to use vb (w/in excel) to create a command button in worksheet 1. I want to enter a date in a cell and click the command...
0
by: Mansi Shah | last post by:
I have one gridview with one column, and at runtime i m adding template columns to it.. now i want to copy the whole grid to another because i want to add 2 more column in between on a button...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.