【问题标题】:Excel VBA Change Row Height based on Find ValueExcel VBA根据查找值更改行高
【发布时间】:2017-09-21 23:39:27
【问题描述】:

我有一个有 2 张纸的工作簿。第一个有 2 列数据。第二个是格式化的时间表。我想在计划中的第一列数据中找到值:

代入第二列的数据 将具有新值的单元格的行高增加 5。 将 Schedule 中所有单元格的字体更改为 Calibri。

这是我目前所拥有的,但它不起作用:

Public Sub FindReplace()
    Dim AllCells As Range
    Dim myList As Range
    Dim myRange As Range
    Dim myHeight As Double

    Set AllCells = Sheets("Sheet 1").Cells
    AllCells.Font.Name = "Calibri"

    Set myList = Sheets("FindReplace").Range("A1:C200")
    Set myRange = Sheets("Sheet 1").Cells

    For Each cel In myList.Columns(1).Cells
        myRange.Select
        Selection.Find(What:=cel.Value, LookIn:=xlFormulas, LookAt:=xlWhole).Activate
        ActiveCell.RowHeight = myHeight
        myHeight = myHeight + 5
        Selection.RowHeight = myHeight

        myRange.Replace cel.Value, cel.Offset(0, 2), LookAt:=xlWhole     
    Next cel
End Sub

请帮忙

【问题讨论】:

  • 在模块顶部写上Option Explicit。然后转到Debug->Compile 并编辑您的代码,直到它正在编译。然后阅读:stackoverflow.com/documentation/excel-vba/1107/…
  • 旁注:您应该始终使用Worksheets,而不是Sheets,因为工作表集合还包含图表等,例如。 Sheets("A Cart").Range 将失败。
  • “不工作” - 一个常见且无用的短语。请更具体 - 您是否收到错误消息,代码是否运行但未执行您想要的操作,等等。

标签: vba excel find


【解决方案1】:

试试这个:

Option Explicit

Public Sub FindReplace()

Dim myList, myRange, CelA, celB As Range

Set myRange = Sheets("T3").Cells
myRange.Font.Name = "Calibri"
Set myList = Sheets("T2").Range("A1:c200")
For Each CelA In myList.Columns(1).Cells
    If CelA <> "" Then
        Set celB = myRange.Cells.Find(What:=CelA.Value, LookIn:=xlFormulas, LookAt:=xlWhole)
        If Not celB Is Nothing Then
            If celB.RowHeight < 410 Then celB.RowHeight = celB.RowHeight + 5
            myRange.Replace CelA.Value, CelA.Offset(0, 2), LookAt:=xlWhole
        End If
    End If
Next CelA
End Sub

【讨论】:

  • 对不起,我得到了同样的运行时错误“91”“对象变量或块变量未设置在“如果 celB.RowHeight
  • 你用的是哪个版本的excel?
  • 最新,Office365
  • 谢谢你 Massimo Griffani。现在可以了。您能否深入了解成功的关键是什么?
  • 运行时错误是由于 CelB 是 Nothing 的事实,因为没有找到具有所需值的单元格。因此,只有当单元存在时,才必须进行操作。另外,在我的测试中,我已经设置不找空值了(CellA > "")
【解决方案2】:

这样的事情可能会奏效:

Option Explicit

Public Sub FindReplace()

    Dim AllCells        As Range
    Dim myList          As Range
    Dim myRange         As Range
    Dim myHeight        As Double
    Dim cel             'not declared in your code, but its a good idea to do it

    Set AllCells = Sheets("T3").Cells
    AllCells.Font.Name = "Calibri"

    Set myList = Sheets("T2").Range("A1:C200")
    Set myRange = Sheets("T3").Cells

    For Each cel In myList.Columns(1).Cells
        myRange.Parent.Activate
        myRange.Select
        'Selection.Find(What:=cel.Value, LookIn:=xlFormulas, LookAt:=xlWhole).Activate

        myHeight = myHeight + 5

        If myHeight < 410 Then
            'Selection.RowHeight = myHeight
            ActiveCell.RowHeight = myHeight
        End If
        myRange.Replace cel.Value, cel.Offset(0, 2), LookAt:=xlWhole
    Next cel
End Sub

改变了什么?

  • 工作表的名称。
  • cel 元素的活动工作表使用myRange.Parent.Activate 激活
  • 引入了 410 高度的条件。
  • 顶部显式选项

一般来说,代码质量不高,因为它使用了SelectActivate,出于性能和调试原因,这违反了最佳实践。

【讨论】:

  • 感谢您的及时回复。但我得到了错误。 “运行时错误'91':对象变量或未设置块变量。当我选择“调试”时,它指向“Selection.Find(What:=cel.Value, LookIn:=xlFormulas, LookAt:=xlWhole ).Activate"。你有什么建议
  • 删除该行再试一次
  • 宏在没有该行的情况下运行,但它将第二个电子表格中每一行的高度更改为 405!你说我在使用不好的做法。你能从头开始为此写一个更好的脚本吗?我对 VBA 有点陌生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多