简体   繁体   中英

Excel VBA, range.value = range.value, unexpected results

hopefully someone would be kind enough to point out why this isn't working. Basically via vba a new line is inserted @ the last row of a table (Row41), this pushes the last line down (creating a gap within the data) then the last line values are transferred up one row so the blank row is at the bottom.

Now the process works fine except for two of the cell values change randomly, below are the before and after

Before:

  • Cell(41,B) = 03/10/14
  • Cell(41,C) = 12345
  • Cell(41,E) = 3.00
  • Cell(41,F) = DD

After:

  • Cell(41,B) = 03/10/14
  • Cell(41,C) = 12345
  • Cell(41,E) = 41915
  • Cell(41,F) = 41915

I've double checked the set ranges and they are as they should be, any ideas? Oh for the code the Specific_Tbl variable is 2

    '[Capture table First/Last row number]
    int_FirstRow = .Cells(4, "AC").Offset(0, Specific_Tbl)
    int_LastRow = .Cells(6, "AC").Offset(0, Specific_Tbl)

    '[Insert Blank Row]
    .Range("A" & int_LastRow & ":Z" & int_LastRow).Insert shift:=xlDown

    '[Set Cell Ranges]
    Select Case Specific_Tbl
        Case 1
            '[Remerge Description]
            .Range(.Cells(int_LastRow, "E"), .Cells(int_LastRow, "H")).MergeCells = True
            Set rng_Tmp1 = .Range("B" & int_LastRow & ":C" & int_LastRow & ",E" & int_LastRow & ":J" & int_LastRow)
            Set rng_Tmp2 = .Range("B" & int_LastRow + 1 & ":C" & int_LastRow + 1 & ",E" & int_LastRow + 1 & ":J" & int_LastRow + 1)
        Case 2, 3
            Set rng_Tmp1 = .Range("B" & int_LastRow & ":C" & int_LastRow & ",E" & int_LastRow & ":F" & int_LastRow)
            Set rng_Tmp2 = .Range("B" & int_LastRow + 1 & ":C" & int_LastRow + 1 & ",E" & int_LastRow + 1 & ":F" & int_LastRow + 1)
    End Select

    '[Transfer values and clear]
    rng_Tmp1.Value = rng_Tmp2.Value
    rng_Tmp2.ClearContents

Unfortunately I never discovered why excel vba was unable to deal with the split range like I believed it would. A workaround was done by adding more range variables to deal with each side of the split range.

    '[Capture table First/Last row number]
    int_FirstRow = .Cells(4, "AC").Offset(0, Specific_Tbl)
    int_LastRow = .Cells(6, "AC").Offset(0, Specific_Tbl)

    '[Insert Blank Row]
    .Range("A" & int_LastRow & ":Z" & int_LastRow).Insert shift:=xlDown

    '[Set Cell Ranges]
    Select Case Specific_Tbl
        Case 1
            '[Remerge Description]
            .Range(.Cells(int_LastRow, "E"), .Cells(int_LastRow, "H")).MergeCells = True
            Set rng_Tmp1 = .Range("B" & int_LastRow & ":C" & int_LastRow)
            Set rng_Tmp2 = .Range("B" & int_LastRow + 1 & ":C" & int_LastRow + 1)
            Set rng_Tmp3 = .Range("E" & int_LastRow & ":J" & int_LastRow)
            Set rng_Tmp4 = .Range("E" & int_LastRow + 1 & ":J" & int_LastRow + 1)
        Case 2, 3
            Set rng_Tmp1 = .Range("B" & int_LastRow & ":C" & int_LastRow)
            Set rng_Tmp2 = .Range("B" & int_LastRow + 1 & ":C" & int_LastRow + 1)
            Set rng_Tmp3 = .Range("E" & int_LastRow & ":F" & int_LastRow)
            Set rng_Tmp4 = .Range("E" & int_LastRow + 1 & ":F" & int_LastRow + 1)
    End Select

    '[Transfer values and clear]
    rng_Tmp1.Value = rng_Tmp2.Value
    rng_Tmp3.Value = rng_Tmp4.Value
    rng_Tmp2.ClearContents
    rng_Tmp4.ClearContents

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