2017-08-01 25 views
0

我有以下代码将年度降水数据从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)` 

我不完全理解这是什么意思,甚至更多,所以我不知道该怎么办它实际执行复制的线。谢谢大家迄今为止的帮助。

+0

你在做什么,说sub没有定义?明确并包含实际的错误信息。 – RBarryYoung

+0

错误是编译错误: 子或函数没有定义 –

+0

此外,我约95%确定'“..&..”'不能做你希望在范围表达式中。它几乎肯定会导致无效的范围表达式。 – RBarryYoung

回答

0

此行失败:

Workbook(Macroforprecip.xlsm).Activate 

,因为没有所谓的Workbook功能。你可能意味着使用该应用程序的工作簿集合,像这样:

Workbooks("Macroforprecip.xlsm").Activate 
0

我犯了一个很大的变化,因为有很多问题。看看这是否让你更接近:

Sub f() 
    Dim wb As Workbook ' define workbook object 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    Dim directory As String, fileName As String 
    directory = "C:\Working-Directory\Precipdata\" ' added backslash 
    fileName = Dir(directory & "*.csv") 
    Do While fileName <> "" 
    Set wb = Workbooks.Open(directory & fileName) ' set a workbook object 
    With wb ' use workbook object instead 
     If Range("B2").Value = "GJOA HAVEN A" Then col = "B" & rw & ":D" & rw + lngth - 27 ' fixed 
     If Range("B2").Value = "TALOYOAK A" Then col = "E" & rw & ":G" & rw + lngth - 27 ' fixed 
     If Range("B2").Value = "GJOA HAVEN CLIMATE" Then col = "H" & rw & ":J" & rw + lngth - 27 ' fixed 
     'If Range("B2").Value = "HAT ISLAND" Then col = " & rw :M & rw+lngth-27" ' missing column so removed 
     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) 
     .Activate 
     'Workbook(Macroforprecip.xlsm).Activate ' moved into the With 
     rw = Cells.Find("01/01/" & yr).Row 
     wb.Range("P&R&T" & (Range("B27").End(xlDown).Row)).Copy wb.Range(col) ' not sure what you are trying to do here 
    End With 
    wb.Close 
    fileName = Dir() 
    Loop 
    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True 

End Sub 
相关问题