2016-10-27 5 views
0
Sub AddNameNewSheet1() 
    Dim wsToCopy As Worksheet, wsNew As Worksheet 
    Dim Newname As String 
    Newname = InputBox("Number for new worksheet?") 
    Set wsToCopy = ThisWorkbook.Sheets("Sheet1") 
    Set wsNew = ThisWorkbook.Sheets.Add 
    If Newname <> "" Then 
     wsNew.Name = Newname 
    End If 
    wsToCopy.Cells.Copy wsNew.Cells 
    Dim cell As Range 
    Dim bIsNumeric As Boolean 
    Dim testFormula As String 
    bIsNumeric = False 

    For Each cell In wsNew.Range("A1:M40") 
     If cell.HasFormula() = True Then 
      If bIsNumeric Then 
       If testFormula = CStr(cell.Formula) Then 
        cell.Value = "<" 
       Else 
        testFormula = cell.Formula 
        cell.Value = "F" 
       End If 
      Else 
      testFormula = cell.Formula 
      cell.Value = "F" 
      End If 
      bIsNumeric = True 

     ElseIf IsNumeric(cell) = True Then 
      bIsNumeric = False 
      If Len(cell) > 0 Then 
       cell.Value = "N" 
      End If 

     Else 
      bIsNumeric = False 
      cell.Value = "L" 

     End If 
    Next cell 
End Sub 

式に適用された列と行を抽出したい。たとえば、式が= SUM(A10:F10)の場合は、 です。次に、A10とF10の両方が必要な場合は、それを見つける方法があります。マクロを使用してエクセルで抽出するカラムの範囲

私の実際の目的は、列と行の値のない式を見つけることです。ありがとうございます。

答えて

0

あなたは公式からA10とF10を取得したい場合は、あなたがこれを使用することができ、strRangeにあなたの範囲を渡す:

Sub Extract_Ranges_From_Formula() 

Dim strRange As String 
Dim rCell As Range 
Dim cellValue As String 
Dim openingParen As Integer 
Dim closingParen As Integer 
Dim colonParam As Integer 
Dim FirstValue As String 
Dim SecondValue As String 


strRange = "C2:C3" 

For Each rCell In Range(strRange) 

    cellValue = rCell.Formula 

    openingParen = InStr(cellValue, "(") 
    colonParam = InStr(cellValue, ":") 
    closingParen = InStr(cellValue, ")") 


    FirstValue = Mid(cellValue, openingParen + 1, colonParam - openingParen - 1) 
    SecondValue = Mid(cellValue, colonParam + 1, closingParen - colonParam - 1) 

    Debug.Print FirstValue 
    Debug.Print SecondValue 

Next rCell 

End Sub 

それは2つの返された値のDebug.Printを行います。

+0

しかし、式の修正パターンはありません。あらゆる種類の数式から任意の数の範囲が必要です。 –

+0

'rCell.Formula'文字列を繰り返してすべての範囲を取り除く必要があります。検索している数式は表示されていません。 **範囲なしの数式を探しているならば、同様の考え方でそれを行うことができます。 –

+0

ありがとうございました。私は、それを知る唯一の方法だと思っていますが、範囲を削除する方法はあります。数式は静的ではないこともあります。範囲を取り除く考えがあれば、私に提案してください。私は範囲なしの式だけを欲しい –

関連する問題