在行数上使用Excel PasteSpecial(分割)

问题描述 投票:0回答:2

我有一个电子表格,如下:

  • A:G列(名称,路径,大小,类型,DateCreated,DateLastModified,DateLast访问)

  • 标题使用行1:7

  • ColumnC包含以KB为单位的文件大小
  • 从选定文件夹填充主VBA
  • Sub在ColumnD的左侧插入2列
  • 现在的列:名称,路径,以KB为单位的大小,以MB为单位的大小,以GB为单位的大小,类型,创建日期,访问日期的最后修改时间,访问日期的最后日期

我有一个要调用的Sub,它将ColumnC循环到并在ColumnD和ColumnE中插入一个除数

循环工作,但是要花一些时间,所以想粘贴PasteSpecial,其中ColumnD = ColumnC / 1000000和ColumnE = ColumnC / 1000000000

每个文件夹将具有不同数量的文件。要运行转换通话,我正在使用myRows = Cells(Rows.Count, 3).End(xlUp).Row - 7(其中7是标题行的扣除)“

我想将ColumnC的myRows粘贴到ColumnD和ColumnE,但是如上所述,每个文件夹的myRows都不同。我没有找到(但仍在搜索)将除数放在哪里,该除数将用于ColumnD和ColumnE的计算:

PasteSpecial Operation:=xlPastSpecialOperationDivide

我的电话代码是:

Sub Convert_MB_GB()

    Dim myRows As Long
    Dim x As Long
    Dim var As Double
    Dim Num1 As Long
    Dim Num2 As Long
    Num1 = 1000000 'Divisor for KB to MB
    Num2 = 1000000000 'Divisor for KB to GB

        myRows = Cells(Rows.Count, 3).End(xlUp).Row - 7 'Count rows and subtract 7-row header

        Range("D:E").Insert 'Insert 2 columns to the left of Column D
        [D7].Value = "Size in MB" 'Add title to column
        [E7].Value = "Size in GB" 'Add title to column

        Range("C8").Select 'Starting Cell

        For x = 1 To myRows
            var = Application.ActiveCell.Value
            ActiveCell.Offset(0, 1).Select
                ActiveCell.Value = var / Num1
            ActiveCell.Offset(0, 1).Select
                ActiveCell.Value = var / Num2
            ActiveCell.Offset(1, 0).Select
                ActiveCell.Offset(0, -2).Select
        Next

 Range("D:D").NumberFormat = "0.0" 'Configure Column D to 1 decimal point
 Range("E:E").NumberFormat = "0.0" 'Configure Column E to 1 decimal point

End Sub

我有一个电子表格,如下:A:G列(名称,路径,大小,类型,DateCreated,DateLastModified,DateLast访问的标题)使用第1行:7 ColumnC包含以KB为单位的文件大小,主要是VBA填充...

excel vba paste
2个回答
1
投票

你是这个意思吗?

Sub x()

Dim myRows As Long

myRows = Cells(Rows.Count, 3).End(xlUp).Row - 7

With Cells(1, 1000)
    .Value = 1000000 'some out of the way unused cell
    Range("D8").Resize(myRows).Value = Range("C8").Resize(myRows).Value
    .Copy
    Range("D8").Resize(myRows).PasteSpecial operation:=xlPasteSpecialOperationDivide
    .ClearContents
End With

End Sub

0
投票

您可能不需要PasteSpecial,应该避免使用Select。您的循环可以简化为:

© www.soinside.com 2019 - 2024. All rights reserved.