excel vba取代范围细胞

时间:2018-03-20 11:38:51

标签: excel vba excel-vba

此代码已经正常工作,但我想要"普通" &安培; "信"要有活力。喜欢指向特定的范围细胞。我已经尝试过了,但它并没有成功。

基本上是sReplace = Replace(sReplace,Range" A1"," Range" B1:B10")

    Private Sub CommandButton1_Click()

    Dim sFind As String
    Dim sReplace As String
    Dim iFileNum As Integer
    Dim FilePath As String
    Dim newFileName As String

    FilePath = "C:\Users\new\Plain.prn"

    iFileNum = FreeFile
    Open FilePath For Input As iFileNum

    Do Until EOF(iFileNum)
    Line Input #iFileNum, sFind
    sReplace = sReplace & sFind & vbCrLf
    Loop
    Close iFileNum

    sReplace = Replace(sReplace, "plain", "Letter")

    'SaveAs txtfile
    newFileName = Replace(FilePath, ".", "_edit.")
    iFileNum = FreeFile
    Open newFileName For Output As iFileNum

    Print #iFileNum, sReplace
    Close iFileNum
    End Sub

2 个答案:

答案 0 :(得分:1)

这是如何使值指向给定工作表中的给定范围:

With Worksheets("NameOfTheWorksheet")
    sReplace = Replace(sReplace, .Range("A1"), .Range("A2"))
End With

答案 1 :(得分:0)

这可能就是你所要求的:

将范围B1:B10中与sReplace匹配的值替换为范围A1

中的值
Range("B1:B10").Replace What:=sReplace, Replacement:=Range("A1"), LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False