2012-11-28 41 views
0

我目前映射列名在Excel中的变量,然后用我的公式,新行的变量,这里有一个例子例子;映射Excel列用VBA

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 

文件我输入可以从一天他们列的顺序改为一天,但他们allays使用相同的名称,目前我使用的查找方法来获取列然后保存信为字符串,并用我的公式中的参考,但是使用查找和映射100列的过程相当缓慢......

我一直在试图想出一个更好的方法来做到这一点,但不能想更快的话,任何人都可以帮助我通过建议一个可能更快的方式来做到这一点?

我也在考虑跨到EXCEL DNA移动我的项目,没有人知道它会快多少让我的宏运行?

感谢

+1

你设置'应用.ScreenUpdating'为false,然后再开始绘制映射?这样做可以真正加快速度。除此之外,我没有任何建议,但:-( – Treb

+0

是的和计算是关闭:(谢谢无论如何。 – EmberZ

+0

是列总是在连续的范围内像A:D,而不是A:D和G:我?如果是这样,你可以大概把范围转换成字典和搜索来代替。 – Zaider

回答

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&) 

再次,注意,这仍然可以做出显着更高效,但是这是在做事情更有效的方式......

+0

太好了,这已经加快了一点,其他建议值得赞赏,我理解的方法不是实际移动工作簿,所以即将开始工作。 – EmberZ