Excel Vba 双向复制/粘贴表格

问题描述 投票:0回答:0

enter image description here我想在Excel中写一个vba代码来合并多个工作表。结合部分工作正常。但在那之后,我想更改工作表“组合”中的值并将值推回其他工作表。有什么技巧可以实现吗?

目前这是我的脚本,但是子 PushData 工作得不是很好..(这是将值推回原始表的部分) 想象一下,我在 Excel 中的工作表名称“Doors;Casework;Floors;..”在此代码“sheet1;sheet2;sheet3;..”中。

谢谢!

选项显式

子 CombineSheets() 将 wb 调暗为工作簿 将 ws 调暗为工作表,wsCombined 作为工作表 Dim i As Long,j As Long,k As Long,lastrow As Long

Set wb = ThisWorkbook
Set wsCombined = wb.Sheets("Combine")

'Clear the contents of the combined sheet
wsCombined.Cells.Clear

'Initialize last row variable to 0

lastrow = 0

'Loop through each sheet to be combined

对于 k = 1 到 3 设置 ws = wb.Sheets("Sheet" & k)

'Copy the data from the current sheet to the combined sheet
For i = 1 To ws.UsedRange.Rows.Count
    For j = 1 To ws.UsedRange.Columns.Count
        wsCombined.Cells(lastrow + i, j).Value = ws.Cells(i, j).Value
    Next j
Next i

'Update the last row variable to the last row of the current sheet

lastrow = wsCombined.UsedRange.Rows.Count

lastrow = lastrow + 2 ' Add 2 for the empty rows between sheets
Next k

MsgBox "Sheets combined successfully!"

结束子

子 PushData() 将 wb 调暗为工作簿 将 ws 调暗为工作表,wsCombined 作为工作表 Dim i As Long,j As Long,k As Long,lastrow As Long

Set wb = ThisWorkbook
Set wsCombined = wb.Sheets("Combine")

'Initialize last row variable to 0

lastrow = 0

'Loop through each sheet that was combined
For k = 1 To 3
    Set ws = wb.Sheets("Sheet" & k)
    
    'Copy the data from the combined sheet back to the original sheet
    For i = 1 To ws.UsedRange.Rows.Count
        For j = 1 To ws.UsedRange.Columns.Count
        
            ws.Cells(i, j).Value = wsCombined.Cells(lastrow + i, j).Value
        Next j
    Next i
    lastrow = ws.UsedRange.Rows.Count + 2
Next k


MsgBox "Data pushed back successfully!"

结束子


excel vba
© www.soinside.com 2019 - 2024. All rights reserved.