我正在尝试从一本书中复制一个范围,打开目标书并将值附加到该工作表,使Range类失败并且不确定如何解决这个问题。这是我的代码,感谢您的关注。
Sub openDATfiles()
' openDATfiles Macro
Dim ws As Worksheet, strFile As String, x As Integer, _
y As Long, Pressure As Variant, Tstamp As Variant, LastRow As Long, LastRow2 As Long, cn As Variant, fPath As String
fPath = "F:\McMAHON\From David\SJ15_10_01_CD\"
strFile = fPath & Dir(fPath & "*.dat")
x = 1
y = 1
' Start Loop 1
Do While Len(strFile) > 0
Workbooks.OpenText FileName:= _
strFile, Origin:=437, StartRow _
:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
Array(10, 1), Array(11, 1)), TrailingMinusNumbers:=True
Set ws = ActiveSheet
Do Until x = 31
Pressure = WorksheetFunction.Max(Range("J" & y + 4 & ":J" & y + 1203))
Tstamp = WorksheetFunction.Max(Range("A" & y + 4 & ":A" & y + 1203))
x = x + 1
y = y + 1201
LastRow = ws.Range("N" & Rows.Count).End(xlUp).Row + 1
ws.Range("O" & LastRow).Value = Pressure
ws.Range("N" & LastRow).Value = Tstamp
Loop
strFile = fPath & Dir
Range("A1:K36004").delete Shift:=xlUp
Range("N2:O31").Copy
ActiveWorkbook.Close savechanges:=False
Dim Pastebook As Workbook
'## Open both workbooks first:
Set Pastebook = Workbooks.Open("F:\McMAHON\Useful Things\VBA\PiezoData")
LastRow2 = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
'Now, paste to y worksheet:
Pastebook.Sheets("sheet1").Range("A" & LastRow2).PasteSpecial xlPasteValues
Loop
End Sub
非常感谢任何提示或帮助,谢谢。
我不能让工作簿保持打开状态,因为如果它完全运行,它将最终打开大约400。有没有办法切换回源书并在粘贴值后关闭它,这样我就不会崩溃我的电脑?
—
Workinatwork
我没说 让它永远敞开 , 我说 保持打开直到你粘贴 - 当然你应该关闭它!
—
Mathieu Guindon
CutCopyMode
至False
。尝试保持源书籍打开并运行.Copy
立即 之前.PasteSpecial
。至于你可以做得更好,一旦你的代码按预期工作就成了问题 代码审查 。