1

我目前正在将列名映射到 excel 中的变量,然后将公式中的变量用于新行,这是一个示例;

Dim posType as String

Cells.Find(What:="PositionType", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate


posType = ActiveCell.EntireColumn.Address(False, False)
posType = Left(posType, InStr(1, posType, ":") - 1)


Sheets("sheet1").Range("A1").Select

ActiveCell.End(xlToRight).Select

ActiveCell.Offset(0, 1).Select
Selection.FormulaR1C1 = "PTH Size"

 ' PTH SIZE
Cells.Find(What:="PTH SIZE", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate
    ActiveCell.Offset(1, 0).Select

Selection.Formula = _
  "=IF(" & posType & "2=""PTH""," & settlementDateQuantity & "2,0)"

pthSize = ActiveCell.EntireColumn.Address(False, False)
pthSize = Left(pthSize, InStr(1, pthSize, ":") - 1)
r = ActiveCell.Row

With Worksheets("sheet1").Range(pthSize & r)
    .AutoFill Destination:=Range(pthSize & r & ":" & pthSize & lastrow&)
End With

我导入的文件可以按其列的顺序每天更改,但它们使用相同的名称,目前我使用 find 方法获取列,然后将字母保存到字符串并使用我的公式中的引用使用查找和映射 100 列的过程非常缓慢......

我一直在尝试提出一种更好的方法来做到这一点,但想不出更快的方法,有人可以通过建议一种可能更快的方法来帮助我吗?

我也在考虑将我的项目转移到 EXCEL DNA,有人知道它会让我的宏运行多快吗?

谢谢

4

1 回答 1

1

您正在选择和移动工作簿,而不仅仅是做作业,这会大大减慢程序速度!!!

我进行了一些更改以使您朝着正确的方向前进-试试这个:(注意:它又快又脏,所以我可能在单元格引用中犯了一些错误,但它至少应该使您朝着更好的方向前进)

  Dim posType As String
  Dim SelectedCell As Range

  Set SelectedCell = Cells.Find(What:="PositionType", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
      :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
      False, SearchFormat:=False)

  posType = Mid(SelectedCell.Address, 2, InStr(2, SelectedCell.Address, "$") - 2)

  Set SelectedCell = Sheets("sheet1").Range("A1").End(xlToRight).Offset(0, 1)
  SelectedCell.Value = "PTH Size"

   ' PTH SIZE
  Set SelectedCell = Cells.Find(What:="PTH SIZE", After:=SelectedCell, LookIn:=xlFormulas, _
  LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
      False, SearchFormat:=False).Offset(1, 0)

  SelectedCell.Formula = "=IF(" & posType & "2=""PTH""," & settlementDateQuantity & "2,0)"


  pthSize = Mid(SelectedCell.Address, 2, InStr(2, SelectedCell.Address, "$") - 2)
  r = SelectedCell.Row

  Worksheets("sheet1").Range(pthSize & r).AutoFill Destination:=Range(pthSize & r & ":" & pthSize & lastrow&)

再次注意,这仍然可以大大提高效率,但这是一种更有效的做事方式......

于 2012-11-28T17:17:44.580 回答