2017-07-28 53 views
1

我十年來一直沒有在Excel中使用VBA,但希望找到一種編碼解決方案來更改20K單元格中的一堆文本。我的工作讓我手動做,但我知道必須有一個Comp Sci解決方案。試圖在Excel中更改單元格中的文本

我已經對我需要更改的列進行了排序。並且有成千上萬的重複項需要更改,如下面的示例所示

示例:1001.tif更改爲1001_1.tif,如果它是第一個副本,或者如果它是該值的第一個文本,則將其保留。

我下面的代碼根本不起作用,但應該是它的框架。

Sub Button1_Click() 
    Dim n As Integer 
    n = 1 
    Dim test As String 
    test = Cells(1, D).Text 
    Dim active As String 
    active = Cells(2, D).Text 

    For i = 2 To 12 
    active = Cells(i, D) 
     If active = test Then 
     Cells(i, D).Text = Microsoft.VisualBasic.Strings.Left(test, 4) + "_" + n + ".tif" 
     n = n + 1 
    Else 
     test = active 
    End If 
Next i 

Cells(1, a).Text = "Done" 

End Sub 

任何提示?

+0

使用'.Value2'而不是'.Text' – Tom

+0

謝謝,但是,我試過了,它仍然給我相同的運行時錯誤'1004'應用程序定義或對象定義的錯誤 –

+0

看看下面的答案。你需要改變更多,然後只是 – Tom

回答

0

如果你有一些反感VBA,這裏是同樣的方法作爲當前VBA答案的在單元格公式的實現。假定D是文件名列:

= IF(COUNTIF($ D $ 2:D2,D2)> 1,MID(D2,1,FIND(「。」,D2)-1)&「_」 & COUNTIF($ D $ 2:D2,D2)&「.tif」,D2)也會這樣做。

一些解釋: COUNTIF()公式計算從列頂部($ D $ 2)到當前行的字符串出現次數。 MID/FIND公式將出現次數和下劃線放置在之前的位置。在文件名中。 FIND獲取。的位置,MID在點之前將字符串剪掉,然後公式的其餘部分將附加數字和文件擴展名。這將適用於長度超過4個字符的數字。

+0

這個作品真的很不錯!謝謝!! –

1

使用COUNTIF():

Sub Button1_click() 
Dim i As Long 
Dim j As Long 
For i = 2 To 12 
    With ActiveSheet 
     j = Application.WorksheetFunction.CountIf(.Range(.Cells(1, 4), .Cells(i - 1, 4)), Left(.Cells(i, 4), Len(.Cells(i, 4)) - 4) & "*") 
     If j > 0 Then 
      .Cells(i, 4).Value = Replace(.Cells(i, 4), ".tif", "_" & j & ".tif") 
     End If 
    End With 
Next i 

打開此:

enter image description here

分爲:

enter image description here

+0

這正是我想要做的,但我得到一個運行時錯誤'5':無效的過程調用或參數。當我點擊調試時,它突出顯示了j =應用程序的行 –

+0

您是使用Excel還是Visual Studio? –

+0

我正在使用excel 2016 –

0

看一看我下面所做的更改。我認爲這是你想要完成的。

Sub Button1_Click() 
    Dim n As Integer 
    Dim test As String, active As String 

    n = 1 

    For i = 2 To 12 
     active = Cells(i, D).Value2 
     If active = test Then 
      Cells(i, "D").Value2 = Left(test, 4) & "_" & n & ".tif" 
      n = n + 1 
     Else 
      test = active 
     End If 
    Next i 

    Cells(1, "A").Value2 = "Done" 
End Sub 
0

我根據您的意思做了一些假設,對代碼進行了一些更改。希望這會有所幫助。

Sub Button1_Click() 
    Dim n As Integer 
    Dim test As String 
    Dim active As String 

    n = 1 

    For i = 2 To 12 
     active = Cells(i, 4) 
     test = Cells(i - 1, 4) 
     If active = test Then 
      Cells(i, 4).Value = VBA.Left(test, 4) & "_" & n & ".tif" 
      n = n + 1 
     Else 
      test = active 
      n = 1 
     End If 
    Next i 

    Cells(1, 1).Value = "Done" 

End Sub 
+0

這段代碼的問題是在循環開始時,測試復位等於上面的活動行將被測試,所以它會輸出1001.tif 1001_1.tif 1001.tif。謝謝你。重新設置n是else語句絕對是我忽略的一些東西。 –

0

此代碼將按照您所述進行操作。只需對所有相同的文件進行排序並運行宏。我添加了rg來計算過去是動態的行,但您可以根據需要更改它。

Sub test() 
    Dim n As Integer 
    Dim rg As Integer 
    rg = ActiveSheet.UsedRange.Rows.Count 
    n = 1 
    Dim test As String 
    test = Cells(1, 4).Text 
    Dim active As String 

    For i = 2 To rg 
    active = Cells(i, 4) 
     If active = test Then 
     Cells(i, 4).Value = Left(test, 4) & "_" & n & ".tif" 
     n = n + 1 
    End If 
Next i 

MsgBox "Done" 
End Sub 
相關問題