excel - VBA - 在循环中设置计数器以复制/粘贴到主表中的下一列
问题描述
道歉,因为这可能不是最技术性的问题。
我一直在尝试使用此代码尝试将指定范围(“de:e130”)复制并粘贴到主表中的列中,并为每个后续文件复制并粘贴到下一列中。我设法得到下面似乎打开的文件和粘贴到一个列中,而不是粘贴到后续列中。我认为需要在结构中集成一个循环来告诉复制/粘贴粘贴到主表中的下一个后续列中。
有点卡住,任何帮助将不胜感激。下面的当前代码
Sub LoopThroughFolder()
Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range
Dim a As Integer
Set Wb = ThisWorkbook
MyDir = "L:\Research\Research project\potato\"
MyFile = Dir(MyDir & "*.xlsx")
ChDir MyDir
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While MyFile <> ""
Workbooks.Open (MyFile), UpdateLinks:=False
With Worksheets("Sheet Guidelines")
Rws = .Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range("D1:E130")
Rng.Copy Wb.Worksheets("Sheet1").Cells(1, 1).End(xlUp).Offset(1, 1)
ActiveWorkbook.Close True
End With
MyFile = Dir()
Loop
End Sub
解决方案
Sub LoopThroughFolder()
Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook, Wbtmp As Workbook, Sh As Worksheet
Dim Rws As Long, Rng As Range
Dim a As Integer, Clcnt As Long
Set Wb = ThisWorkbook
Clcnt = 1
MyDir = "L:\Research\Research project\potato\"
MyFile = Dir(MyDir & "*.xlsx")
ChDir MyDir
Application.ScreenUpdating = False
Do While MyFile <> ""
Set Wbtmp = Workbooks.Open(MyFile, True)
For Each Sh In Wbtmp.Sheets
If Sh.Name = "Sheet Guidelines" Then
Sh.Range("D1:E130").Copy Wb.Sheets("Sheet1").Cells(1, Clcnt)
Clcnt = Clcnt + 2
End If
Next Sh
Wbtmp.Saved = True 'Assuming you DONT want to save the temporary workbook (no change to it so no point saving ?
Wbtmp.Close
MyFile = Dir()
Loop
Application.ScreenUpdating = True 'If you turn the screenupdating off, you want to turn it back on at some point
End Sub
推荐阅读
- android - Arranging ConstraintLayout Flow References From Bottom To Top And Right To Left
- javascript - Vuetify 浮动操作按钮或 FAB 更改位置或在路由器更改时跳转(进入和退出)
- python - openpyxl删除隐藏列
- python - TypeError: dtype=datetime64[ns] 和日期之间的无效比较
- java - 来自同一个 Maven 项目的不同工件
- javascript - 使用javascript / node js在地图功能中出现意外输出
- imodeljs - 如何在一个视口中渲染来自多个 imodel 的合并 imodel?
- c# - 在 ASP.NET Core 站点中实现 Blazor - 组件不在视图中呈现
- c# - 将在 foreach 循环中生成的 Azure Cosmos Db SQL-API 语句转换为参数化语句
- google-classroom - 403 当前用户在为 Google 课堂 api (.Net) 创建课程时无法创建课程 [禁止]