麻烦搞清楚如何用特定文本替换范围中的单元格 - Excel VBScript

问题描述:

因为我确信这很容易做到,而且事实上我已经设法创建了其他功能,对于这个项目来说有点复杂。麻烦搞清楚如何用特定文本替换范围中的单元格 - Excel VBScript

无论如何,我正在尝试做什么。我有一个工作表(库存数据),并在第1列中列出了一个公司名称,对于所有行都是一样的。即1900行左右的行中的每一行在第一个单元格中都有公司名称。

现在,虽然每个应用程序的数据总是相同的,但行数也会改变。

所以,我需要一个函数,它将首先确定最后一行数据在范围内,然后将每条记录的第一列中的所有单元格更改为name_company。公司名称总是相同的,所以我可以静态分配它们。这是我有什么不起作用。

我能够让它以另一种方式工作,但它会一直替换文本到工作表的最后一行,超出数据停止的位置。

谢谢!

Sub changeCompany() 'Changes company name as pulled from Agemni into proper ETA format 

Dim myCell As Range 
Dim RngToChange As Range 'The range of cells that need to be changed 
Dim LastRow As Long 'Declare variable to help determine the last row on a variable length worksheet 
Dim i As Integer 

With Worksheets("inventory-data") 'set the range to change 
    Set RngToChange = .Columns(1) 
End With 

LastRow = Worksheets("inventory-data").UsedRange.Rows.Count 'Have Excel determine what the last row is. 

For i = LastRow To 1 Step -1 
    RngToChange.Cells.Value = "name_company" 
Next i 

末次

我总是有更多的成功与[SomeCellOrRange]CurrentRegion .Rows.Count e.g:

Range("A1").CurrentRegion.Rows.Count 

UsedRange查找任何使用的细胞,不限于连续的平板状的块。它有时还需要您在删除某些行之后重新保存工作簿,然后才能正确缩小工作簿。

+0

好的,我觉得它比我想象的要容易得多!我做了以下工作,使其完美运作。 子changeCompany()“的变化的公司名称 昏暗RngToChange如范围”需要被改变 随着工作表(‘库存数据’)的细胞的范围“设置为改变 集RngToChange = .Columns(范围1) 尾随着 RngToChange.Replace什么:= “acshomeentertainment”,改为:= “dish_acs”,_ 注视:= xlPart,SearchOrder:= xlByRows,MatchCase:=假SearchFormat:= _ 假,ReplaceFormat: =假 End Sub –