2012-04-12 99 views
1

我有一個相當大的Excel csv文件,我需要將""添加到每個單元格的開始和結尾處。添加「開始和」到每個單元格數據的末尾

細胞含有混合文本無論是

  • 數字
  • 文本
  • 鏈接

等等,所有這一切,我需要引號添加到每個單元的開始和結束。

我該怎麼做?

回答

1

我從Excel show leading zero in formula bar適應了這個代碼

它使用變體數組爲速度的效率,僅更新電子表格的UsedRange

  • 按下Alt & F11一起去VBE
  • 插入模塊
  • 拷貝和下面
  • 按下Alt代碼& F11一起粘貼到回到Excel
  • 從Developer選項卡運行宏

如果您願意,可以調整爲忽略空白單元 - 讓我知道

Sub AddStrings() 
Dim rng1 As Range 
Dim rngArea As Range 
Dim strRep1 As String 
Dim strRep2 As String 
Dim lngRow As Long 
Dim lngCol As Long 
Dim lngCalc As Long 
Dim X() 


strRep1 = """" 
strRep2 = """" 

ActiveSheet.UsedRange 
Set rng1 = ActiveSheet.UsedRange 

'Speed up the code by turning off screenupdating and setting calculation to manual 
'Disable any code events that may occur when writing to cells 
With Application 
    lngCalc = .Calculation 
    .ScreenUpdating = False 
    .Calculation = xlCalculationManual 
    .EnableEvents = False 
End With 

'Test each area in the user selected range 

'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on 
For Each rngArea In rng1.Areas 
    'The most common outcome is used for the True outcome to optimise code speed 
    If rngArea.Cells.Count > 1 Then 
     'If there is more than once cell then set the variant array to the dimensions of the range area 
     'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks 
     X = rngArea.Value2 
     For lngRow = 1 To rngArea.Rows.Count 
      For lngCol = 1 To rngArea.Columns.Count 
       'replace the leading zeroes 
       X(lngRow, lngCol) = strRep1 & X(lngRow, lngCol) & strRep2 
      Next lngCol 
     Next lngRow 
     'Dump the updated array sans leading zeroes back over the initial range 
     rngArea.Value2 = X 
    Else 
     'caters for a single cell range area. No variant array required 
     rngArea.Value = strRep1 & rngArea.Value2 & strRep2 
    End If 
Next rngArea 

'cleanup the Application settings 
With Application 
    .ScreenUpdating = True 
    .Calculation = lngCalc 
    .EnableEvents = True 
End With 

End Sub 
1

手:

  • 改變第一小區的價值=""""&A1&""""
  • 拖動該單元格的格式,以整版

或VB宏

相關問題