将粘贴范围复制到 sheet 中,其中包含值

Copy&paste range into a sheet with values in it

我一直在研究一个 VBA 宏,它可以将值从两个 sheet 复制到另一个。我创建了一个时间 sheet ,我在其中粘贴一个 sheet 的值并使用它们,因为我必须粘贴它们的地方有更多或更少的列,我不能弄乱 sheet 我正在复制的地方。一切正常,直到我粘贴。我粘贴的 sheet 里面有数据,一些公式,以及我需要知道的东西。

这是我的代码,抱歉太大了

Sub Actualiza_Cartera()

    Dim i, r, erow  As Integer
    Dim myUnion As Range
    Dim myCell As Object
    ReDim arrayNombre(1 To 4)

    arrayNombre(1) = "Forward EUR Fisico"
    arrayNombre(2) = "Forward EUR Comp"
    arrayNombre(3) = "Forward CNH"
    arrayNombre(4) = "Forward PEN"

'    Application.DisplayAlerts = False 'this is commented till this works
'    Application.ScreenUpdating = False

'    Limpia contenido sheet POR PLAZO
    Worksheets("Final_Sheet").Select
    last_col = Cells(7, Columns.Count).End(xlToLeft).Column     'Get last column
    last_row = Cells(Rows.Count, 2).End(xlUp).Row               'Get last row
    Range(Cells(7, 1), Cells(last_row, last_col - 1)).Select    'Seleccionar tabla
    Selection.ClearContents                                     'Borrar el contenido
    
'   crea página temp
    Sheets.Add.Name = "temp"

'   COPIA SID
'   activa, selecciona y copia tabla de Vigentes_SID
    Worksheets("Vigentes_SID").Select
    last_row = Cells(Rows.Count, 2).End(xlUp).Row                   'Get last row
    last_col = Cells(2, Columns.Count).End(xlToLeft).Column         'Get last column
    Range(Cells(2, 1), Cells(last_row, last_col)).Select            'Select entire table
    Selection.Copy                                                  'copia seleccion
    Sheets("temp").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False                                   'pega a temporal
    Application.CutCopyMode = False

'   comienza a formatear datos
    Range("E:F").Columns.Delete 'borra columnas que no se usarán

'    Reordena las columnas según el formato
    Columns("G:G").Cut
    Columns("C:C").Insert Shift:=xlToRight
    Columns("G:G").Cut
    Columns("F:F").Insert Shift:=xlToRight

'    Inserta columnas faltantes para la planilla de risk
    Columns(6).EntireColumn.Insert
    Columns(6).ClearFormats
'    Inserta columnas faltantes para la planilla de risk
    Columns(7).EntireColumn.Insert
    Columns(7).ClearFormats

'    Cambiar formato de columnas
    Columns("D:E").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("H:H, J:J").Select
    Selection.NumberFormat = "#,##0_ ;[Red]-#,##0 "
    Range("I:I").Select
    Selection.NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "

'    Rellena columnas agregadas en blanco
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "USD"
    LastRow = Range("E" & Rows.Count).End(xlUp).Row
    Range("F1").AutoFill Destination:=Range("F1:F" & LastRow), Type:=xlFillCopy

    Range("G1").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[1]<0,""V"",""C"")"
    LastRow = Range("H" & Rows.Count).End(xlUp).Row
    Range("G1").AutoFill Destination:=Range("G1:G" & LastRow), Type:=xlFillCopy

    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=RC[2]-TODAY()"
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    Range("C1").AutoFill Destination:=Range("C1:C" & LastRow), Type:=xlFillCopy

'    Copia los datos llamados por la función a valores para no perderlos al eliminar columnas
    Range("G1").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

    Range("C1").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

'   Loop para seleccionar y eliminar corredora y facility
    For r = Sheets("temp").UsedRange.Rows.Count To 1 Step -1
        If Cells(r, "B") = "CORREDORA" Or InStr(Cells(r, "B").Value, "FACILITY") Then
'            Sheets("temp").Rows(r).EntireRow.Delete
             If Not myUnion Is Nothing Then
                 Set myUnion = Union(myUnion, Rows(r).EntireRow)
             Else
                 Set myUnion = Rows(r).EntireRow
             End If
        End If
    Next

    If myUnion Is Nothing Then
        ActiveSheet.Cells(1, 1).Select
    Else
        myUnion.Select
    End If
    Selection.Delete

'    copia datos de temp a por plazo
    last_row = Cells(Rows.Count, 2).End(xlUp).Row           'Get last row
    last_col = Cells(1, Columns.Count).End(xlToLeft).Column 'Get last column
    Range(Cells(1, 1), Cells(last_row, last_col)).Select    'seleccionar tabla
    Selection.Copy
    Sheets("Final_Sheet").Select
    Range("A7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Set myUnion = Nothing
    Sheets("temp").Select
    Cells.Clear

'   COPIA FORWARD

'   Loop copia y pega desde hojas Forward
    For i = LBound(arrayNombre) To UBound(arrayNombre)
        Worksheets(arrayNombre(i)).Select
        For r = Sheets(arrayNombre(i)).UsedRange.Rows.Count To 1 Step -1
            If IsDate(Cells(r, "A").Value) = True Then
    '           Sheets("temp").Rows(r).EntireRow.Delete
                If Not myUnion Is Nothing Then
                    Set myUnion = Union(myUnion, Rows(r).EntireRow)
                Else
                    Set myUnion = Rows(r).EntireRow
                End If
            End If
        Next

        If myUnion Is Nothing Then
            Cells(1, 1).Select
        Else
            myUnion.Select
            Selection.Copy
            Worksheets("temp").Select
            last_row = Sheets("temp").Cells(Rows.Count, 1).End(xlUp).Row
            If last_row = 1 Then
                Sheets("temp").Cells(last_row, 1).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
                last_col_aux = Sheets("temp").Cells(3, Columns.Count).End(xlToLeft).Column
                last_row_aux = Sheets("temp").Cells(Rows.Count, 2).End(xlUp).Row
                Sheets("temp").Range(Cells(last_row, last_col_aux + 1), Cells(last_row_aux, last_col_aux + 1)) = arrayNombre(i)
                Columns("S:S").Select
                Range(Selection, Selection.End(xlToRight)).Select
                Selection.Clear
            Else
                Sheets("temp").Cells(last_row + 1, 1).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
                last_col_aux = Sheets("temp").Cells(2, Columns.Count).End(xlToLeft).Column
                last_row_aux = Sheets("temp").Cells(Rows.Count, 2).End(xlUp).Row
                Sheets("temp").Range(Cells(last_row + 1, last_col_aux), Cells(last_row_aux, last_col_aux + 1)).Value = arrayNombre(i)
                Columns("S:S").Select
                Range(Selection, Selection.End(xlToRight)).Select
                Selection.Clear
            End If
            Set myUnion = Nothing
        End If
    Next i

'   Pone valor moneda
    Range("S1").Select
    ActiveCell.FormulaR1C1 = "=IF(OR(RC[-1]=""Forward EUR Fisico"",RC[-1]=""Forward EUR Comp""),""EUR"",IF(RC[-1]=""Forward CNH"",""CNH"",IF(RC[-1]=""Forward PEN"",""PEN"",""ERROR"")))"
    LastRow = Range("R" & Rows.Count).End(xlUp).Row
    Range("S1").AutoFill Destination:=Range("S1:S" & LastRow), Type:=xlFillCopy

    Range("S1").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

    Range("R:R").Columns.Delete

'   Reordenamiento de columnas
    Columns("J:J").Cut                      'folio
    Columns("A:A").Insert Shift:=xlToRight

    Columns("M:M").Cut                      'cliente
    Columns("B:B").Insert Shift:=xlToRight

    Columns("O:O").Cut                      'plazo
    Columns("C:C").Insert Shift:=xlToRight

    Columns("R:R").Cut                      'moneds
    Columns("F:F").Insert Shift:=xlToRight

    Range("G:G, K:K, M:P, R:S").Columns.Delete             'delete columnas no usadas

'   unifica precio
    Range("K:K").Columns.Insert Shift:=xlToRigh
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""",RC[-2],RC[-1])"
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    Range("K1").AutoFill Destination:=Range("K1:K" & LastRow), Type:=xlFillCopy

    Range("K1").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

    Range("I:J").Columns.Delete

'   da formato a las fechas
    Columns("D:E").Select
    Selection.NumberFormat = "m/d/yyyy"


'   rellena con 1 si no tiene folilo
    Range("K:K").Columns.Insert Shift:=xlToRigh
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-10]="""",1,RC[-10])"
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    Range("K1").AutoFill Destination:=Range("K1:K" & LastRow), Type:=xlFillCopy

    Range("K1").Select
    Range(Selection, Selection.End(xlDown)).Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

'   Reordenamiento de columnas
    Columns("K:K").Cut                      'folio
    Columns("A:A").Insert Shift:=xlToRight
    Range("B:B").Columns.Delete

'   selecciona y copia de temp "forward"
    last_row = Cells(Rows.Count, 1).End(xlUp).Row           'Get last row
    last_col = Cells(1, Columns.Count).End(xlToLeft).Column 'Get last column
    Range(Cells(1, 1), Cells(last_row, last_col)).Select    'Select entire table
    Selection.Copy                                          'copia seleccion

    Worksheets("Final_Sheet").Select
    last_row = Sheets("Final_Sheet").Cells(Rows.Count, 1).End(xlUp).Row
    Sheets("Final_Sheet").Cells(last_row + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False                           'pega a temporal
    Application.CutCopyMode = False
    
    Sheets("temp").Delete

End Sub

我的问题是第 111 行到第 119 行之间的复制和粘贴(复制第一个 sheet),加上第一个数据下面复制第二个 sheet 的值的第 238 到 248 行。 我正在复制的数据从 sheet Final_Sheet 中的“A”列到“K”列,在 sheet 的“L”列中,我有一些公式(颜色为绿色我的图像),问题是如果“M”或“N”列为空,复制后一切正常,但是如果我在复制后将其他公式放在“M”或“N”列上,即在鲑鱼范围内颜色,删除“L”列中的所有公式或值。有什么办法可以避免这种情况发生?

我的代码中有一些注释,但它们是西班牙语的,祝您愉快,抱歉打扰了

我试着复制, 并且您的范围选择似乎在列大小上有所不同,因此其中一些在带有公式的列上确实与 Final_Sheet 重叠。 如果没有看到您正在处理的原始数据,很难提出建议...... 这可能是因为原始数据有一些不为空的空单元格,代码 .End(xlToLeft).Column 选择的比您预期的要多。

如果原始数据文件总是采用相同的格式,最好指定要从中复制数据的列,而不要使用 .End(xlToLeft).Column 盲目选择它们。 或者,如果您经常将它们命名为相同的名称,则按 Header 列名称。

另一方面,- 这样一个简单的任务在 Excel Power Query 上可能更加可靠和可重复。