首先,这段代码是"有点工作",这意味着它会将信息粘贴到目标但不会粘贴到正确的单元格中,并添加其他行而不是跳过空行。一旦我将其正确粘贴到适当的单元格,它就不再需要工作了。
我确信这是一个相当简单的问题,但我不知所措。任何帮助将不胜感激
Private Sub CommandButton1_Click()
Dim Myfile As String
Dim erowFilepath = ("C:\Users\YOU DONT NEED TO SEE THIS PART\Desktop\LINKED TRACKERS\")
Myfile = Dir("C:\Users\OR THIS PART\Desktop\LINKED TRACKERS\")
Do While Len(Myfile) > 0
If Myfile = "COMPANY_CYCLE.xlsm" Then
Exit Sub
End If
Workbooks.Open (Filepath & Myfile)
Worksheets("ROSTER").Range("A3:O3").Copy
ActiveWorkbook.Save
ActiveWorkbook.Close
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
' ***The line below is the line I get an error***
ActiveSheet.Paste Destination:=Worksheets("MANNINGROSTER").Range(Cells(erow, 1), Cells(erow, 1))
Myfile = Dir
Loop
End Sub
长话短说:我试图将用户将输入数据的4个不同工作簿中的数据合并到一个工作簿中,我可以监视和处理日常工作中的信息。
我非常肯定这个问题源于密码保护的源工作表,并且所有4个工作簿的粘贴目标也是相同的。
我似乎无法找到解决这个问题的方法。一个错误导致另一个,另一个等等。
答案 0 :(得分:1)
Private Sub CommandButton1_Click()
Dim Myfile As String, Filepath As String
Dim wb As Workbook, rng As Range
Myfile = Dir("C:\Users\OR THIS PART\Desktop\LINKED TRACKERS\")
Filepath = ("C:\Users\YOU DONT NEED TO SEE THIS PART\Desktop\LINKED TRACKERS\")
Do While Len(Myfile) > 0
If Myfile Like "*.xls*" And UCase(Myfile) <> "COMPANY_CYCLE.XLSM" Then
Set wb = Workbooks.Open(Filepath & Myfile)
Set rng = wb.Worksheets("ROSTER").Range("A3:O3")
'EDIT: copy values only, (when there's something to copy)
If Application.CountA(rng) > 0 Then
With Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
End With
End If
'Note: if there's nothing in ColA for the copied range, this will
' throw off the next copy and you may end up overwriting data...
wb.Close False
End If
Myfile = Dir
Loop
End Sub