简体   繁体   中英

Excel VBA copy range cells value formula format without clipboard

I need copy cells(value,formula,format) from one sheet to another sheet without clipboard involved. The code below does not work. How can I fix it? I do appreciate your help.

 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

I'm going to assume that this is actually a sub and not just a scrap of code. Operating off of that assumption, your function would pass in the two worksheets names (sFormat, sHoldings), your source row (iSourceRow), start column (iStartColumn), end column (iEndColumn), destination row (iDestRow) and destination column (iDestCol) as parameters. Set you source range (rSource) and destination range (rDest) and then work with them directly. This makes your code FAR more readable and you can watch those ranges to make sure that you're using the correct ranges. This way there's no intermediate variables to hold your values, formats and formulas.

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

Also, why not just use the clipboard?? Doing so would change your code to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM