与此问题类似https://stackoverflow.com/questions/ask/advice?我想复制并粘贴值但是收到运行时错误438:对象不支持此属性或方法。不幸的是,我没有以与其他人相同的方式进行复制和粘贴,因此答案不适用于我
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
我的错误出现在Workbooks(fileName).Range(“P27”,“P”和lngth).Copy _ 工作簿(“Macroforprecip.xlsm”)。范围(col&amp; r)行
谢谢
答案 0 :(得分: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