我在MacBook上的一个文件夹中有大约50张Excel表格 - (/ Users / myusername / Desktop / Tidy / folder")
我想对它们执行以下宏:
Sub SmartCopy()
Dim s1 As Worksheet, s2 As Worksheet
Dim N As Long, i As Long, j As Long
Set s1 = Sheets("s1")
Set s2 = Sheets("s2")
N = s1.Cells(Rows.Count, "Y").End(xlUp).Row
j = 1
For i = 1 To N
If s1.Cells(i, "Y").Value = "No" Then
Else
s1.Cells(i, "Y").EntireRow.Copy s2.Cells(j, 1)
j = j + 1
End If
Next i
End Sub
我正在努力让床单打开,几乎就像文件路径不被识别一样,每个工作表的名称都是这样的:
商务列表-002-W-site.csv
有一个标签:
商务列表-002-W-site.csv
所以我还需要1)每次重命名工作表2)让宏只打开工作簿中唯一的工作表。
我想将所有工作簿中的所有数据复制到一个主数据库中。我确实试图添加我的宏并调整它,但根本无法让它运行。
答案 0 :(得分:0)
您需要定义工作簿(文件),而不仅仅是工作表(选项卡)。
Dim filePath as String
Dim sheetStart as String
Dim count as Integer
Dim sheetEnd as string
Dim thisSheet as Worksheet
Dim wb1 as Workbook
Dim ws1 as Worksheet
filePath = "/Users/myusername/Desktop/Tidy/folder/"
sheetStart = "business-listing-"
sheetEnd = "-w-site"
Set thisSheet as ThisWorkbook.Worksheets("Sheet1")
For count = 1 to 44 'the range of sheets you have
Set wb1 = Workbooks.Open(filePath & sheetStart & format(count, "000") & sheetEnd & ".csv")
Set ws1 = wb1.Worksheets(sheetStart & format(count, "000") & sheetEnd)
'move the ranges you want from ws1 to thisSheet
wb1.close
next count
每次代码循环时,它都会更改正在打开的文件名以及它要查找的工作表。
我假设你知道或者可以找到如何根据你提供的原始代码将范围从ws1复制到thisSheet的下一个可用行。
使用基于评论的改进代码进行编辑