简体   繁体   English

为多个工作表创建VBA,并使用公式将粘贴复制到新列中

[英]Create VBA for multiple sheet and copy paste in new column with formula

I have an excel with multiple sheets and would like to copy or better say want to extend the last column every month. 我有一个具有多个工作表的excel,并且想要复制或更好地说要每月扩展最后一列。

Eg:- I have a sheet with sheet named sheet1,sheet2,sheet3,sheet4,sheet5...every sheet at the end of the month has formulas.Once a month is over I would like to add a new column with new month and copying the existing formula to the new column.Let say I have last month Jan and I need VBA to add new column with month as Feb and copy all the formula to the new column. 例如:-我有一个工作表,其工作表名为sheet1,sheet2,sheet3,sheet4,sheet5 ...每个月底的工作表都有公式。将现有公式复制到新列中。假设我上个月是Jan,我需要VBA添加月份为Feb的新列并将所有公式复制到新列中。

Sometimes I also need to copy multiple column (eg:-Column CJ) and replicate the next 8 column with new month and formula. 有时我还需要复制多列(例如:-CJ列),并用新的月份和公式复制下8列。

Tried with recording macro but the issue is it doesn't create a new column for every month it just copy paste it in same column rather than creating a new one for every month 尝试过使用录制宏,但问题是它不会为每个月创建一个新列,而只是将其复制粘贴到同一列中,而不是每个月都创建一个新列

It is difficult to understand the problem without seeing the formulas. 不看公式就很难理解问题。

It sounds like you could start by using the AutoFill. 听起来您可以通过使用自动填充来开始。 You could do this manually by selecting the range you want to copy and dragging the cross in the bottom right corner. 您可以通过选择要复制的范围并拖动右下角的十字来手动完成此操作。 This will update the month automatically. 这将自动更新月份。

You can achieve this with VBA, such as: 您可以使用VBA实现此目的,例如:

Public Sub copyRange()
    Dim rngSource As Range
    Dim rngDestination As Range

    rngSource = ActiveSheet.Range("A1:A20")
    rngDestination = ActiveSheet.Range("B1:B20")

    rngSource.AutoFill Destination:=rngDestination
End Sub

Either way, I can't tell how to reset the formulae for the new months without seeing the cell code. 无论哪种方式,在看不到单元格代码的情况下,我都无法告诉您如何重置新月份的公式。

UPDATE: To AutoFill multiple columns on multiple tabs 更新:自动填充多个选项卡上的多个列

Public Sub copySpecifiedColumns()
    copyRanges InputBox("How many columns do you wish to copy?", "Copy Columns", "1")
End Sub
Private Sub copyRanges(copyCols As Byte)
    Dim ws As Worksheet, lastCol As Integer, lastRow As Integer
    Dim rngSource As Range, rngDestination As Range
    Dim sheetList As Variant
    sheetList = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")

    For Each ws In ThisWorkbook.Sheets
        If (UBound(Filter(sheetList, ws.Name)) > -1) Then
            lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
            lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
            Set rngSource = ws.Range(ws.Cells(1, lastCol - copyCols + 1), _
                ws.Cells(lastRow, lastCol))
            Set rngDestination = ws.Range(ws.Cells(1, lastCol - copyCols + 1), _
                ws.Cells(lastRow, lastCol + copyCols))

            rngSource.AutoFill rngDestination
        End If
    Next ws
End Sub

I Agree it's a bit difficult to understand what you are trying to achieve here. 我同意,要在此处了解要实现的目标有点困难。 From what I understand if you want to make a copy of last column in the next column in each sheet and change the 1st cell of that column to the month at the time. 据我了解,是否要在每张纸的下一列中复制最后一列,并将该列的第一个单元格更改为当时的月份。 This code can help. 此代码可以提供帮助。

Sub copy_col()


Dim lColumn As Long
For Each Sheet In Worksheets

lColumn = Sheet.Cells(1, Columns.Count).End(xlToLeft).Column
Sheet.Columns(lColumn).Copy Sheet.Columns(lColumn + 1)
Sheet.Cells(1, lColumn + 1).Value = Month(Now())

Next Sheet
End Sub

If this is not what you want then please explain your problem more briefly. 如果这不是您想要的,请简要说明您的问题。

Thanks 谢谢

Extend List and Update Formula 扩展列表和更新公式

Usage 用法

ExtendList 5, "Sheet1", "Sheet3" ExtendList 5,“ Sheet1”,“ Sheet3”

Where 1. 5, is the Column to Duplicate to the next empty Column 2. "Sheet1" is the sheet referenced in the original formula 3. "Sheet3" is the replace sheet name 其中1、5是要复制到下一个空列的列2。“ Sheet1”是原始公式3中引用的工作表。“ Sheet3”是替换工作表的名称

Original Formula 原始配方

=Sheet1!$A10 = Sheet1!$ A10

New Formula 新配方

=Sheet3!$A10 = Sheet3!$ A10

 Sub ExtendList(SourceColumn As Long, OriginalSheetName As String, NewSheetName As String) On Error Resume Next Dim newColumnNumber As Integer Worksheets(NewSheetName).Name = NewSheetName If Err.Number <> 0 Then Err.Clear Exit Sub End If On Error GoTo 0 newColumnNumber = Range(Cells(1, Columns.Count), Cells(Rows.Count, Columns.Count)).End(xlToLeft).Offset(, 1).Column Columns(SourceColumn).Copy Columns(newColumnNumber) Columns(newColumnNumber).Replace What:=OriginalSheetName, Replacement:=NewSheetName, lookat:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=False End Sub 

This will only work if the column reference is absolute: 仅当列引用是绝对的时,这才起作用:

Correct 正确

$A1 or $A$2 $ A1或$ A $ 2

Incorrect 不正确的

A1 or A$1 A1或A $ 1

暂无
暂无

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

相关问题 VBA 用于过滤的宏,从列中复制指定值并创建然后粘贴到具有该列名称的新工作表中 - VBA Macro to filter, copy the specified value from a column and create then paste in a new sheet with that column name 复制并粘贴到另一个工作表Excel vba中的新列 - Copy and paste to new column in another sheet Excel vba 使用 VBA 将列复制并粘贴到新工作表 - Using VBA to Copy and Paste Columns to New sheet VBA:循环从主表中复制某些列,创建新表并粘贴 - VBA: loop to copy certain columns from main sheet, create a new sheet, and paste VBA 根据每张表中的 A 列过滤器复制多个工作表并创建新工作簿 - VBA Copy multiple sheets based on column A filter in each sheet and create new workbook 如何使用vba将多列数据从一张工作表复制并粘贴到另一张工作表 - How to copy and paste multiple column data from one sheet to another sheet using vba VBA /复制同一行和上一列中的最大列数和日期,然后粘贴到新表中 - VBA / Copy the Max of column and the the date in the same row and previous column and paste into new sheet VBA创建,重命名并粘贴到新表中 - VBA to create, rename and paste into a new sheet VBA 从表 1 复制列并将粘贴转置到表 2 中的行 - VBA copy column from sheet 1 and transpose paste into row in sheet 2 vba代码以查找特定值并在该列的后续单元格中复制值并粘贴到新工作表中 - vba code to find a specific value and copy value in subsequent cell in the column and paste in a new sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM