0

首先从源工作簿中获取非空单元格的范围。然后在目标工作簿中选择相似范围的单元格。如何做到这一点?

完整代码::

~~~~~~~~~~~

Public Sub ConvertTo_K()

Dim rng1 As Range 

Dim rng2 As Range 

Set rng1 = Workbooks("Source.xls").Worksheets("Source").Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp).Resize(, 1)) 

Set rng2 = Workbooks("Destination.xls").Worksheets("Destination").Range(rng1.Address)

rng2.Value = Round(rng1.Value / 1000, 2)

 'At this point, an error message of Type Mismatch pops up (Due to different ranges of rng1 and rng2). Do i have to use a loop? How to do that? 

End Sub
4

2 回答 2

0

如果你的rng1工作正常,你可以这样做:

set rng2 = Workbooks("Destination.xls").Worksheets("Destination").Range(rng1.address)
于 2013-09-25T21:48:12.387 回答
0

Round(rng1.Value / 1000, 2)将不起作用,因为 rng1 是范围的集合,可以通过rng1.Cells(1).value

rng1.Value无效。

您可以遍历 rng2 的每个单元格并应用圆形公式。

Public Sub ConvertTo_K()

    Dim rng1 As Range
    Dim rng2 As Range
    Dim RoundRange As Range
    Dim rngVal As Double

    Dim SourceWkb As Workbook
    Set SourceWkb = Workbooks("Source.xls")

    Dim SourceSht As Worksheet
    Set SourceSht = SourceWkb.Worksheets("Source")

    With SourceSht
     Set rng1 = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp).Resize(, 1))
    End With

    Dim DestinWkb As Workbook
    Set DestinWkb = ThisWorkbook  'Workbooks("Destination.xls")

    Dim DestinSht As Worksheet
    Set DestinSht = DestinWkb.Worksheets("Destination")

    With DestinSht
          Set rng2 = .Range(rng1.Address)
    End With

     'rng2.Value = Round(rng1.Value / 1000, 2) This wont work

     rng1.Copy rng2

     For Each cell In rng2
        cell.Value = Round(cell / 1000, 2)
     Next
End Sub
于 2013-09-26T05:59:35.097 回答