Excel VBA复制范围单元格值公式格式(不带剪贴板)

Excel VBA复制范围单元格值公式格式(不带剪贴板),excel,vba,excel-formula,Excel,Vba,Excel Formula,我需要将单元格(值、公式、格式)从一张纸复制到另一张纸,而不涉及剪贴板。下面的代码不起作用。我怎样才能修好它?我非常感谢你的帮助 Dim v_value As Variant Dim v_formula As Variant Dim v_format As Variant With Sheets(FORMATSSHEET) v_value = .Range(.Cells(iSourcePositionRow, iStartPositionColumn

我需要将单元格(值、公式、格式)从一张纸复制到另一张纸,而不涉及剪贴板。下面的代码不起作用。我怎样才能修好它?我非常感谢你的帮助

 Dim v_value As Variant    
 Dim v_formula As Variant    
 Dim v_format As Variant

 With Sheets(FORMATSSHEET)    
      v_value = .Range(.Cells(iSourcePositionRow, iStartPositionColumn), .Cells(iSourcePositionRow + 1, iEndPositionColumn)).Value2    
      v_format = .Range(.Cells(iSourcePositionRow, iStartPositionColumn), .Cells(iSourcePositionRow + 1, iEndPositionColumn)).NumberFormat    
      v_formula = .Range(.Cells(iSourcePositionRow, iStartPositionColumn), .Cells(iSourcePositionRow + 1, iEndPositionColumn)).FormulaR1C1    
 End With

 With Sheets(HOLDINGSSHEET)
     .Range(.Cells(iDestinationPositionRow, iDestinationPositionCol), .Cells(iDestinationPositionRow, iDestinationPositionCol)).Offset(-5, 0).Value2 = v_value
     .Range(.Cells(iDestinationPositionRow, iDestinationPositionCol), .Cells(iDestinationPositionRow, iDestinationPositionCol)).Offset(-5, 0).NumberFormat = v_format
     .Range(.Cells(iDestinationPositionRow, iDestinationPositionCol), .Cells(iDestinationPositionRow, iDestinationPositionCol)).Formula = v_formula

 End With

Sheets(HOLDINGSSHEET).Select

我将假设这实际上是一个sub,而不仅仅是一段代码。在这种假设下,函数将传入两个工作表名称(sFormat、sHoldings)、源行(iSourceRow)、起始列(iStartColumn)、结束列(iEndColumn)、目标行(iDestRow)和目标列(iDestCol)作为参数。设置源范围(rSource)和目标范围(rDest),然后直接使用它们。这使您的代码更具可读性,您可以查看这些范围,以确保使用正确的范围。这样就没有中间变量来保存值、格式和公式

Sub CopyStuff( _
    sFormat As String, _
    sHoldings As String, _
    iSourceRow As Integer, _
    iStartColumn As Integer, _
    iEndColumn As Integer, _
    iDestRow As Integer, _
    iDestCol As Integer)

    Dim rSource As Range, rDest As Range

    rDest = Sheets(sHoldings).Range(Sheets(sHoldings).Cells(iDestRow, iDestCol), Sheets(sHoldings).Cells(iDestRow, iDestCol)).Offset(-5, 0)
    rSource = Sheets(sFormat).Range(Sheets(sFormat).Cells(iSourceRow, iStartColumn), Sheets(sFormat).Cells(iSourceRow + 1, iEndColumn))

    rDest.Value2 = rSource.Value2
    rDest.NumberFormat = rSource.NumberFormat
    rDest.Formula = rSource.Formula

    shtHoldings.Select
End Sub
还有,为什么不直接使用剪贴板??这样做会将您的代码更改为:

Sub CopyStuff( _
        sFormat As String, _
        sHoldings As String, _
        iSourceRow As Integer, _
        iStartColumn As Integer, _
        iEndColumn As Integer, _
        iDestRow As Integer, _
        iDestCol As Integer)

        Dim rSource As Range, rDest As Range

        rDest = Sheets(sHoldings).Range(Sheets(sHoldings).Cells(iDestRow, iDestCol), Sheets(sHoldings).Cells(iDestRow, iDestCol)).Offset(-5, 0)
        rSource = Sheets(sFormat).Range(Sheets(sFormat).Cells(iSourceRow, iStartColumn), Sheets(sFormat).Cells(iSourceRow + 1, iEndColumn))

        rSource.Copy rDest

        shtHoldings.Select
    End Sub

它做什么而不是工作?如果出现错误,请描述错误。顺便说一句,您正在阅读
formula1c1
,但随后将其设置为
Formula
,我认为问题在于没有将工作表名称括在引号中。当您可以只使用
Range1.NumberFormat=Range2.NumberFormat
时,为什么要传递中间值?很长的变量名可能隐藏了一些解决方案。