2017-08-01 79 views
0

與此問題類似https://stackoverflow.com/questions/ask/advice?我想複製並粘貼值,但收到運行時錯誤438:對象不支持此屬性或方法。不幸的是我沒有做我的複製和粘貼以同樣的方式,因爲這等人這樣的答案並不適用於我爲什麼不能粘貼#2對象不支持此屬性或方法

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).Worksheet(sheetName).Range("P27", "P" & lngth).Copy Workbooks("Macroforprecip.xlsm").Worksheets("Sheet1").Range(col & r) 
     Workbooks(fileName).Close 
     fileName = Dir() 
    Loop 

Application.ScreenUpdating = True 
Application.DisplayAlerts = True 

End Sub 

我的錯誤出現在工作簿(文件名).Range(「P27」,「P 「& lngth).Copy _ 工作簿(」 Macroforprecip.xlsm「)範圍(COL & R)線

謝謝

+0

您的鏈接「問題」只是「如何問」頁面。 – YowE3K

+1

崩潰時變量'filename','sheetname','lngth','col'和'r'的值是什麼? – YowE3K

+1

'.Worksheet(sheetName)'應該是'.Worksheets(sheetName)' –

回答

0

取代:

Workbooks(fileName).Worksheet(sheetName).Range("P27", "P" & lngth).Copy Workbooks("Macroforprecip.xlsm").Worksheets("Sheet1").Range(col & r) 

附:

Workbooks(fileName).Activate 
Workbooks(fileName).Worksheet(sheetName).Range("P27", "P" & lngth).Select 
Selection.Copy 
Workbooks("Macroforprecip.xlsm").Activate 
worksheets("Sheet1").Activate 
Range(col & r).Select 
ActiveSheet.Paste 
+1

謝謝!有效。我很放心。我被困約2017年。 (給或2017年) –

相關問題