2015-12-29 239 views
-1

我有一个Excel文件:提取数据

Excel.xls

Code date  start end 
2301 12/08/1993 08:02 08:17 
4221 12/08/1993 09:04 09:25 
2312 12/08/1993 10:02 10:28 
1284 19/09/1994 11:02 11:21 
2312 19/09/1994 15:57 16:20 
1284 23/06/1995 17:12 17:35 
2312 22/06/1996 13:14 13:32 
4221 22/06/1996 15:53 16:13 
4221 05/05/1999 08:06 08:22 
2418 05/05/1999 08:10 08:33 
2301 05/05/1999 09:12 09:37 
2301 05/05/1999 09:28 10:28 
2301 05/05/1999 13:28 13:38 

问题:

的问题是,以提取数据从excel文件到另一个由Code连接的行?

希望的解决方案:

从Excel中导入数据文件到另一个或工作簿时,通过Code行的数目接合与数据有关的名片,例如在片材2301我可以具有唯一的数据人谁该代码在Excel文件:

Sheet2301

Code date  start end 
2301 05/05/1999 09:12 09:37 
2301 05/05/1999 09:28 10:28 
2301 05/05/1999 13:28 13:38 
....  ......  .... .... 

林AB例如VBA中的内幕,所以我花了几天的时间找到解决方案。

编辑:

其实我有不同的CSV文件,我导入到使用此VBA代码的唯一工作表:

Option Explicit 

Sub ImportCSV() 

Dim strSourcePath As String 
Dim strDestPath As String 
Dim strFile As String 
Dim strData As String 
Dim x As Variant 
Dim Cnt As Long 
Dim r As Long 
Dim c As Long 

Application.ScreenUpdating = False 

'Change the path to the source folder accordingly 
strSourcePath = "path to csv files\" 

If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\" 

'Change the path to the destination folder accordingly 
strDestPath = "path to csv files\" 

If Right(strDestPath, 1) <> "\" Then strDestPath = strDestPath & "\" 

strFile = Dir(strSourcePath & "*.csv") 

Do While Len(strFile) > 0 
    Cnt = Cnt + 1 
    If Cnt = 1 Then 
     r = 1 
    Else 
     r = Cells(Rows.Count, "A").End(xlUp).Row + 1 
    End If 
    Open strSourcePath & strFile For Input As #1 
     If Cnt > 1 Then 
      Line Input #1, strData 
     End If 
     Do Until EOF(1) 
      Line Input #1, strData 
      x = Split(strData, ",") 
      For c = 0 To UBound(x) 
       Cells(r, c + 1).Value = Trim(x(c)) 
      Next c 
      r = r + 1 
     Loop 
    Close #1 
    Name strSourcePath & strFile As strDestPath & strFile 
    strFile = Dir 
Loop 

Application.ScreenUpdating = True 

If Cnt = 0 Then _ 
    MsgBox "No CSV files were found...", vbExclamation 

End Sub 

和代码工作很细,所以我必须通过加入该数据在不同的电流表中(正如我之前解释的)?

+0

'花了几天'如果这是真的,我可能会建议你雇用一个人XD – findwindow

+1

在这几天里,我希望你已经找到*一些你可以尝试的代码,或者至少是一个想法。如果是这样,请发布该代码! – BruceWayne

+0

@findwindow问题是我非常喜欢VBA Excel初学者,很抱歉! – Yassou

回答

0

无需VBA,您可以使用公式。

我假设你的每张纸(“2301”,“4221”等)被称为只是。另外,我假设每个人都有一个标题行。

在您的非主工作表中输入该公式(作为一个数组,CTRL + SHIFT + Enter并拖动/降: =IFERROR(INDEX(Sheet1!A$2:A$14,SMALL(IF(Sheet1!$A$2:$A$14=INT(RIGHT(CELL("filename",A1),LEN(CELL("filename",A1))-FIND("]",CELL("filename",A1)))),ROW(Sheet1!A$2:A$14)-ROW(Sheet1!A$2)+1),ROWS(Sheet1!A$2:A2))),"")

你可能需要调整的范围内,但如果您有任何问题,请告诉我们!

现在,您可以在任何工作表上使用该公式,它会查看主工作表(在我的示例中为Sheet1)然后返回等价信息。

你会在哪里可能想要VB的帮助,但是,将这个公式添加到每个工作表。让我知道你是否需要帮助。

编辑:尽管如此,这要求您已将工作簿保存在某处。除非保存工作簿,否则返回工作表名称的公式部分不起作用。

+0

我不太明白布鲁斯,你可以看到我的编辑上面请我猜你的解决方案是可能的后,我将数据导入到独特的文件,然后通过表(数据)中的数据加入他们? – Yassou

+0

@Yassou - 哦!这改变了很多。下次,请在您的帖子中添加代码。但是上面的公式应该可以工作,只需要把它们放到每张纸上即可。 – BruceWayne

+0

谢谢,但我需要一些帮助,因为我没有得到你的公式,你可以通过重新调整价值来改变它以上我的文件上面请? – Yassou