将文本插入Excel中所有单元格的开头

时间:2018-01-07 18:44:56

标签: excel vb.net

我有一个excel文件,其中我有一个带有数值的填充列A,我想将文本“dt”添加到数值之前的值。当然,文本dt将添加到存在值的整个列中。不幸的是,我的代码中的某些内容无效

Imports System
Imports System.IO
Imports Excel
Imports System.IO.FileStream
Imports Microsoft.Office.Interop.Excel


Public Class Form1

Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles   Button5.Click

Dim oExcel As New Microsoft.Office.Interop.Excel.Application

Dim oBook As Microsoft.Office.Interop.Excel.Workbook

Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet

oBook = oExcel.Workbooks.Open("C:\Users\PC\Desktop\1.xlsx")

oSheet = oBook.Worksheets(1)


Dim rg21 = oSheet.Columns("A:A")

rg21.Select()
rg21.NumberFormat = "@"

rg21 = "dt" & rg21

oBook.Save()

oExcel.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)

System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)

oBook = Nothing

oExcel = Nothing


End Sub
End Class

2 个答案:

答案 0 :(得分:2)

我会使用循环。在Excel VBA中:

Sub dural()
    Dim r As Range
    Set r = Columns(1).Cells.SpecialCells(2)
    For Each rr In r
        rr.Value = "dt" & rr.Value
    Next rr
End Sub

将其改编为VB.Net。

答案 1 :(得分:1)

只需在PendingIntent playPausePendingIntent = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY_PAUSE); views.setOnClickPendingIntent(R.id.btnPlay, playPausePendingIntent); views.setImageViewResource(R.id.btnPlay, icon); 中循环遍历单元格。 这是循环的慢速版本,只要您遍历A列中的所有1.000.000+单元格:

rg21

如果您想要更快,那么仅使用第一列中的值循环通过单元格将更容易。就像来自 @Gary的学生的例子一样:

For Each rg In rg21.Cells
    If Len(rg) Then
        rg = "dt" & rg
    End If
Next rg