2015-12-29 242 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