我有以下代碼將年度降水數據從132個不同的excel文件複製到一個大型數據集。我有來自多個不同地點的降水數據,我將這些數據放在不同的列中,因此列出不同的數值。我也想匹配日期,因此rw值。但是,我得到我的sub沒有定義,我不知道爲什麼。子或功能未定義從一個文件粘貼到另一個
Sub f()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim directory As String, fileName As String
directory = "C:\Working-Directory\Precipdata"
fileName = Dir(directory & "*.csv")
Do While fileName <> ""
Workbooks.Open (directory & fileName)
With (Workbooks(directory & fileName))
If Range("B2").Value = "GJOA HAVEN A" Then col = "B & rw :D & rw+lngth-27"
If Range("B2").Value = "TALOYOAK A" Then col = "E & rw :G & rw+lngth-27"
If Range("B2").Value = "GJOA HAVEN CLIMATE" Then col = "H & rw :J & rw+lngth-27"
If Range("B2").Value = "HAT ISLAND" Then col = " & rw :M & rw+lngth-27"
If Range("B2").Value = "BACK RIVER (AUT)" Then col = "N & rw :P & rw+lngth-27"
yr = Range("B27").Value
lngth = (Range("B27").End(xlDown).Row)
End With
Workbook(Macroforprecip.xlsm).Activate
rw = Cells.Find("01/01/" & yr).Row
Workbooks(fileName).Range("P&R&T" & (Range("B27").End(xlDown).Row)).Copy_Workbooks(Macroforprecip.xlsm).Range (col)
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
而且我真的想用細胞功能,所以我可以做山坳數值,只是增加兩個,但我找不到怎麼辦範圍相當於(「」 A「&我:」 G「&我」)。
好,所以我更新了它稍微更簡單。我一次只複製一列,並且將workbook()函數更改爲workbooks(),我的新代碼看起來像這樣。
Sub precipitation()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim directory As String, fileName As String
directory = "C:\Working-Directory\Precipdata\"
fileName = Dir(directory & "*.csv")
Do While fileName <> ""
sheetName = Left(fileName, Len(fileName) - 4)
Workbooks.Open (directory & fileName)
Workbooks(fileName).Activate
If Range("B1").Value = "GJOA HAVEN A" Then
col = "B"
End If
If Range("B1").Value = "TALOYOAK A" Then
col = "E"
End If
If Range("B1").Value = "GJOA HAVEN CLIMATE" Then
col = "H"
End If
If Range("B1").Value = "HAT ISLAND" Then
col = "K"
End If
If Range("B1").Value = "BACK RIVER (AUT)" Then
col = "N"
End If
yr = Range("B27").Value
lngth = (Range("B27").End(xlDown).Row)
Workbooks("Macroforprecip.xlsm").Activate
Set rw = ActiveSheet.Cells.Find(what:=DateValue("01/01/" & yr))
r = rw.Row
Workbooks(fileName).Range("P27", "P" & lngth).Copy_Workbooks("Macroforprecip.xlsm").Range (col & r)
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
我的新的錯誤是,我碰到一個「運行時錯誤‘438’:
對象不支持此屬性或方法」
這發生在了`
(Workbooks(fileName).Range("P27", "P" & lngth).Copy_Workbooks("Macroforprecip.xlsm").Range (col & r)`
我不完全理解這是什麼意思,甚至更多,所以我不知道該怎麼辦它實際執行復制的線。謝謝大家迄今爲止的幫助。
你在做什麼,說sub沒有定義?明確幷包含實際的錯誤信息。 – RBarryYoung
錯誤是編譯錯誤: 子或函數沒有定義 –
此外,我約95%確定'「..&..」'不能做你希望在範圍表達式中。它幾乎肯定會導致無效的範圍表達式。 – RBarryYoung