7

を使用して列の値に一重引用符を追加しようとしていますChr(39)が、2番目の引用符しか取得できません。
たとえば、 「値」が必要ですが、「値」を取得しています

しかし、それを使用して二重引用符を使用すると、機能し、 「値」Chr(34)が得られます

Sub AddQuote()
Dim myCell As Range
    For Each myCell In Selection
        If myCell.Value <> "" Then
            myCell.Value = Chr(39) & myCell.Value & Chr(39)
        End If
    Next myCell
End Sub
4

3 に答える 3

6

別の一重引用符を追加するだけです。

Sub AddQuote()
Dim myCell As Range
    For Each myCell In Selection
        If myCell.Value <> "" Then
            myCell.Value = Chr(39) & Chr(39) & myCell.Value & Chr(39)
        End If
    Next myCell
End Sub
于 2012-07-14T20:36:38.787 に答える
4

私はこの構文を好みます。各セルをループする代わりに、選択範囲全体を配列に読み込み、配列を操作してから、配列の内容をワークシートにダンプします。ワークシートの最大2回のタッチ。

Sub AddQuote()

  Dim i As Long, j As Long
  Dim inputData As Variant
  Dim outputData As Variant

  ' only act on a range
  If TypeName(Selection) = "Range" Then
    ' assign selection value to a Variant array
    inputData = Selection.Value
    ' create an output array of the same size
    ReDim outputData(LBound(inputData) To UBound(inputData), _
                     LBound(inputData, 2) To UBound(inputData, 2))

    ' loop through all array dimensions
    For i = LBound(inputData) To UBound(inputData)
      For j = LBound(inputData, 2) To UBound(inputData, 2)
        ' array element will be = Empty if cell was empty
        If Not IsEmpty(inputData(i, j)) Then
          outputData(i, j) = Chr(39) & Chr(39) & inputData(i, j) & Chr(39)
        End If
      Next j
    Next i

    Selection.Value = outputData
  End If

End Sub

また、複数列の選択も考慮されます。

于 2012-07-13T19:12:33.307 に答える
2

最初の引用符'は、セルがテキストとして扱われることを意味します。

したがって、LHSで2つの一重引用符を使用し、正しく表示するには右側に1つ使用する必要があります。

A1 =''test'およびA2 =の場合に注意してください''abc'

その後、あなたが期待する=A1&A2ようにあなたを与えるでしょう'test''abc'

于 2012-07-13T18:39:54.850 に答える