我陷入了困境, 我意识到我是新来的,希望我不要犯太多错误。
我的文件夹中有不同名称的工作簿。我的VBA列出了.csv文件的路径。
每个工作簿都有一个唯一的名称,与其中的工作表相同。
接下来,我想循环浏览结合到MASTER.xlsm的工作簿
数据来自OBDII汽车应用程序,因此行数始终未知。
Range("A1").Resize(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select
上面的这段代码在我手动在各种工作簿之一中使用时起作用。但是我得到了
运行时错误1004应用程序或对象定义的错误
当我尝试从MasterWorkbook实施它时。
Private Sub cmd_COPYpaste()
Application.ScreenUpdating = False
Application.CutCopyMode = False
Application.Calculation = xlCalculationManual
''''DIMMENSIONS
Dim SourceRow As Long
Dim EditRow As Long
Dim sFile As String
Dim wb As Workbook
Dim FileName1 As String
Dim wksSource As Worksheet
Const scWkbSourceName As String = "MASTER.xlsm"
Set wkbSource = Workbooks(scWkbSourceName)
Set wksSource = wkbSource.Sheets("Sheet1") ' Replace Sheet1 with the sheet name
Const wsOriginalBook As String = "MASTER.xlsm"
SourceRow = 2
''ENSURE SELECT SOURCE SHEET
Sheets("Sheet1").Select
Do While Cells(SourceRow, "B").Value <> ""
FileName1 = wksSource.Range("B" & SourceRow).Value
sFile = FileName1
Set wb = Workbooks.Open(sFile)
''CREATE TABLE1 ON AWORKBOOK
Dim AWorkbook As Workbook
Set AWorkbook = ActiveWorkbook
''CREATE a REFERENCE-ABLE Sheet
Dim AWorksheet As Worksheet
Set AWorksheet = AWorkbook.ActiveSheet
''SELECT DATA RANGE
下一行是我的错误
AWorksheet.Range("A1").Resize(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select
Selection.Copy
Windows("MASTER.xlsm").Activate
Sheets("Sheet3").Select 'IMPORT TO SHEET
ActiveSheet.Range("A1").Select
ActiveSheet.Paste
''CLOSE WORKBOOK W/O BEFORE SAVE
AWorkbook.Activate
Application.EnableEvents = False
ActiveWorkbook.Close savechanges:=False 'Will NOT save changes
Application.EnableEvents = True
Windows("MASTER.xlsm").Activate
Sheets("Sheet1").Select
'''FUTURE CODE
''GO TO NEXT FILE
SourceRow = SourceRow + 1 ' Move down 1 row for source sheet
Loop
End Sub
我计划将来完成代码,但现在我希望它是一遍又一遍地复制并粘贴到Sheet3上的A1中。
我一直以为我搞砸了
''Need to CREATE a REFERENCE-ABLE Sheet
Dim AWorksheet As Worksheet
Set AWorksheet = AWorkbook.ActiveSheet
如何解决不知道工作表名称的问题,仍然可以完全控制其他工作簿中的工作表?
我以为我指定了AWorkbook的活动表
主工作簿工作表1上的A列具有文件名,而filename1是B列中的完整路径。
我很高兴您能继续与我保持联系,并且很想听听您的想法和简化内容(向每个了解我的人提供支持)?
答案 0 :(得分:0)
您可以替换以下代码:
Set wb = Workbooks.Open(sFile)
收件人:
Sheets("Sheet1").Select
具有以下内容(注释中的解释):
With Workbooks.Open(sFile) ' open and referecne your current workbook
With .Worksheets(Split(.Name, ".")(0)) ' reference referenced workbook sheet named after it
.Range("A1").Resize(.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column) _
.Copy Destination:=Workbooks("MASTER.xlsm").Worksheets("Sheet3").Range("A1") ' copy and paste in one shot
End With
Application.EnableEvents = False
.Close savechanges:=False 'Will NOT save changes
Application.EnableEvents = True
'''FUTURE CODE
End With
''GO TO NEXT FILE
SourceRow = SourceRow + 1 ' Move down 1 row for source sheet