2012-07-20 40 views
0

所以,我有一個宏,它通過工作表上的很多單元循環,併爲它們做些什麼。我如何在我選擇的單元格區域上操作宏?

下面的代碼http://pastie.org/4290581

Private Sub ReplaceFormulaWithValues() 
    Dim lastrow As Long, r1 As Long 
    Dim temp As String, arTemp 
    Dim temp2 As String 
    Dim temp3 As String 

    Dim letter 

    temp3 = "" 

     ' Get the last row in the worksheet 
    lastrow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row 

    For r1 = 1 To lastrow 

     For r2 = 0 To 10 
      letter = Chr(Asc("A") + r2) 
      letter = letter + LTrim(Str(r1)) 


      ' If A & B aren't blank, make the formula of cell C equal to A + B. 
      If Sheet1.Range(letter).Value <> "" And Mid(Sheet1.Range(letter).Formula, 1, 1) = "=" Then 
       If Asc(Mid(Sheet1.Range(letter).Formula, 2, 1)) >= 65 And Asc(Mid(Sheet1.Range(letter).Formula, 2, 1)) <= 90 Then 
        ' Get the formula for the current C cell 
        temp = Replace(Sheet1.Range(letter).Formula, "=", "") 

        'Create an array by splitting the formula on the + sign 
        arTemp = Split(temp, "+") 

        temp2 = Sheet1.Range(arTemp(0)).Value 

        For i = 1 To UBound(arTemp) 
         temp3 = Sheet1.Range(arTemp(i)).Value 
         temp2 = temp2 & "+" & temp3 

        Next i 

        Sheet1.Range(letter).Value = "=" & temp2 
       End If 

      End If 

     Next 
    Next 
End Sub 

This是它做什麼:

for instance, let's say that the formula of cell C2 is C2=A1+B1, where A1 = 10 and B1 = 20. 
I would like to change it so that the formula of cell C2 is C2=10+20. 
However, I don't want the formula displayed in the cell or anything. 

我的問題:我該如何設定,讓我第一次可以突出顯示/從中選擇一組細胞在工作表中,THEN激活宏,以便宏只能在該範圍內的每個單元格上工作?

+0

循環遍歷Application.Selection(http://msdn.microsoft.com/zh-cn/library/ff840834.aspx)中的單元格? – Govert 2012-07-20 17:24:59

+0

您是否試圖用文本版本替換公式(將任何單元格引用評估爲其值)? 「= A1 + B1」變成「C2 = 10 + 20」。如果是這樣,你是否想對你選擇的每個細胞做這件事? (提供它有一個公式,而不是一個值) – NickSlash 2012-07-20 17:40:52

回答

1

程序當前通過兩個for循環遍歷表中的所有單元格。這些循環可以修改爲僅循環使用Application.Selection的當前選擇。

所以,你的代碼看起來就像這樣:

For Each cell In Selection.Cells 


    ' If A & B aren't blank, make the formula of cell C equal to A + B. 
    If cell.Value <> "" And Mid(cell.Formula, 1, 1) = "=" Then 
      If Asc(Mid(cell.Formula, 2, 1)) >= 65 And Asc(Mid(cell.Formula, 2, 1)) <= 90 Then 
       ' Get the formula for the current C cell 
       temp = Replace(cell.Formula, "=", "") 

       'Create an array by splitting the formula on the + sign 
       arTemp = Split(temp, "+") 

       temp2 = ActiveSheet.Range(arTemp(0)).Value 

       For i = 1 To UBound(arTemp) 
        temp3 = ActiveSheet.Range(arTemp(i)).Value 
        temp2 = temp2 & "+" & temp3 

       Next i 

       cell.Value = "=" & temp2 
      End If 

     End If 

Next 

我也用ActiveSheet而不是Sheet1,這樣你可以在任何紙張上運行。

+0

謝謝!你的建議完美:) 雖然我寫了「在選擇每個單元格」,而不是「爲selection.cells中的每個單元格」。這有什麼區別嗎?我的代碼在這裏http://pastie.org/4291086 – 2012-07-20 18:29:13

+0

一個簡單的問題:我如何將工作表指定爲ActiveSheets?例如,來自其他工作簿的工作表?我認爲ActiveSheet是我選擇的工作表。如果ActiveSheet可能是我工作的工作表以外的工作表,那就太棒了。我猜我激活表單如下:Worksheets(「Sheet1」)。激活? – 2012-07-20 18:30:39

+0

ActiveSheet是您當前在當前工作簿中打開的工作表的應用程序常量。要使用來自不同工作區的工作表,您可以創建一個工作表參數(Dim ws As Worksheet),然後從另一本書中獲取工作表,例如設置ws = Application.Workbooks(「openWorkbookName」)。Worksheets(「sheetName) – 2012-07-20 18:35:35

1

我不會重寫代碼 - 但是總的來說,你可以通過一串細胞已選擇這樣的循環:

Sub ClearZeroCells() 

    Dim cell As Range 
    For Each cell In Selection 
     If cell = 0 Then cell.ClearContents 
     ' or you can put your own code here. 
    Next cell 

End Sub 
+0

謝謝!我遵循你的建議,它的工作原理很棒:) http://pastie.org/4291086 – 2012-07-20 18:28:22

0

我在做事情做了個去我覺得你試圖實現。

它(The Macro EvaluateSelection)應該查看選擇中包含公式(starts =)的每個單元格,並將其更改爲半評估公式。

Sub EvaluateTarget(ByVal Target As Range) 
Dim Result As String 
Dim Cell As Range 
Dim Precedent As Range 

Dim Formula As String 
Dim CellReference As String 
Dim CellFormula As String 

    Formula = Target.Formula 

    For Each Cell In Target.SpecialCells(xlCellTypeFormulas) 
     For Each Precedent In Cell.DirectPrecedents 
      CellReference = Replace(Precedent.Address, "$", "") 
      CellFormula = Replace(Precedent.Formula, "=", "") 
      Formula = Replace(Formula, CellReference, CellFormula) 
     Next Precedent 
    Next Cell 

    Target.Value = Replace(Target.Address, "$", "") & Formula 

End Sub 

Sub EvaluateSelection() 
Dim Cell As Range 

For Each Cell In Selection.Cells 
    If Left(Cell.Formula, 1) = "=" Then 
     EvaluateTarget Cell 
    End If 
Next Cell 
End Sub 

Reference - 找到它並從中工作。

我還沒有測試過這麼多,似乎適用於我的簡單示例。

相關問題