简体   繁体   English

Excel VBA - 循环工作表,复制单元格,参考第一个工作表

[英]Excel VBA - Loop through worksheets, copy cells, reference into first worksheet

I am setting up a planning workbook where my company would have individual sheets (all set the same way) planning individual projects.我正在设置一个计划工作簿,我的公司将有单独的工作表(所有设置都以相同的方式)计划单个项目。 These sheets would have certain cells (the same on each sheet) that would then be referenced by the main master schedule, a compilation of the high points of the subproject schedules.这些工作表将具有某些单元格(每张工作表上相同),然后将由主主进度表引用,这是子项目进度表的高点的汇编。 Is this possible?这可能吗? I am quite new at VBA coding so please be nice:)我是 VBA 编码的新手,所以请善待:)

I currently have a bit of code written to copy and paste into the last empty cell in Column B on the target sheet, but this code doesn't work anyway (havent figured out why yet).我目前编写了一些代码来复制并粘贴到目标工作表上 B 列中的最后一个空单元格中,但是这段代码无论如何都不起作用(还没有弄清楚为什么)。 But ideally I would like the cells to be referenced, not copy+pasted, to avoid miscommunication between sheets when things change.但理想情况下,我希望引用单元格,而不是复制+粘贴,以避免当事情发生变化时工作表之间的沟通不畅。 Code pasted below for reference, would also appreciate help fixing this in addition to the other referencing issue.粘贴在下面以供参考的代码,除了其他参考问题外,还将感谢帮助解决此问题。

Sub LoopAndInsert()

Dim ws As Worksheet
Dim target As Worksheet
    Set target = Worksheets("Global Schedule Gantt") 'sheet we're copying to
    
    For Each ws In ThisWorkbook.Worksheets  'loop through all worksheets
    
     If ws.Name <> target.Name Then   'if not the target sheet then...
         'copy range into the next blank row in column C
         ws.Range("CopyToGlobal").Copy target.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
     End If
     
Next ws
End Sub

See if this is what you're after.看看这是否是你所追求的。 I'm assuming you want the cells in the master sheet to simply reference (eg =Sheet1!$A$1 ) rather than just have the value.我假设您希望主表中的单元格仅引用(例如=Sheet1!$A$1 ),而不仅仅是具有值。

EDIT: Changed code upon new information.编辑:根据新信息更改代码。

Sub LoopAndInsert()

Application.ScreenUpdating = False 'I would set these 2 off with this.
Application.Calculation = xlCalculationManual

Dim ws As Worksheet, target As Worksheet
Dim lrow As Long, lrowMaster As Long, i As Long, j As Long

Set target = Worksheets("Global Schedule Gantt") 'sheet we're copying to
lrowMaster = target.Range("B" & Rows.Count).End(xlUp).Row
    
For Each ws In ThisWorkbook.Worksheets  'loop through all worksheets
    If ws.Name <> target.Name Then   'if not the target sheet then...
        For i = 14 To 42
            Select Case i
                Case 14, 15, 16, 18, 23, 25, 26, 29, 31, 32, 33, 35, 36, 41, 42
                lrowMaster = lrowMaster + 1 'Move down to next available row
                For j = 3 To 9
                    target.Cells(lrowMaster, j - 1).Formula = "='" & ws.Name & "'!" & ws.Cells(i, j).Address
                Next j
            End Select
        Next i
    End If
Next ws

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

Copy Cell References of a Non-Contiguous (Multi-Area) Range复制非连续(多区域)范围的单元格引用

  • Adjust the values in the constants section.调整常量部分中的值。

The Code编码

Option Explicit

Sub LoopAndInsert()
    
    Const dstName As String = "Global Schedule Gantt"
    Const dstCol As String = "B"
    Const srcRange As String = "CopyToGlobal"
    ' Or:
    'Const srcRange as String _
        = "C14:I16,C18:I18,C23:I23,C25:I26,C29:I29,C31:I33,C35:I36,C41:I42"
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    
    Dim cel As Range
    With wb.Worksheets(dstName)
        Set cel = .Cells(.Rows.Count, dstCol).End(xlUp).Offset(1)
    End With
    
    Dim src As Worksheet
    Dim sRng As Range
    Dim dRng As Range
    For Each src In wb.Worksheets
        If src.Name <> dstName Then
            For Each sRng In src.Range(srcRange).Areas
                Set dRng = cel.Resize(sRng.Rows.Count, sRng.Columns.Count)
                    dRng.Formula _
                        = "='" & src.Name & "'!" & sRng.Cells(1).Address(0, 0)
                Set cel = cel.Offset(sRng.Rows.Count)
            Next sRng
        End If
    Next src

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA不会循环通过工作表(排除工作表) - Excel VBA will not loop through worksheets (Exclude Worksheet) Excel VBA:遍历单元格并将值复制到另一个工作表 - Excel VBA: Loop through cells and copy values to another worksheet Excel vba将来自不同工作表的单元格复制并粘贴到新工作表 - Excel vba to copy and paste a cells from different worksheets to new worksheet Excel VBA工作表循环-如何在n个工作表之间移动 - Excel VBA Worksheet loop - How to move through n worksheets Excel VBA:查找数据,遍历多个工作表,复制特定范围的单元格 - Excel VBA: Find data, loop through multiple worksheets, copy specific range of cells 遍历行复制并粘贴到VBA Excel中的另一个工作表 - loop through row copy and paste to another worksheet in VBA Excel 遍历许多工作簿,仅遍历第一个和第二个工作表,然后将单元格复制/粘贴到工作簿中 - Loop through many workbooks, loop through only the first and second worksheets, then copy/paste cells into a workbook VBA excel从工作表中复制公式并将其粘贴到多个工作表中 - VBA excel to copy formula from a worksheet and paste to multiple worksheets VBA:如何按单元格复制并粘贴两行,在工作表之间的每个循环中循环嵌套 - VBA: How to Copy + Paste two rows by cells Cycled through a nested For Each Loop between Worksheets Excel VBA循环通过工作表失败 - excel vba loop through worksheets fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM