查找/替换仅限于一列但有许多工作表

时间:2018-03-27 18:04:45

标签: excel vba find

我首先要说的是,我所知道的唯一一个VBA是操纵录制的宏的试错。我是一名注册会计师,试图以艰难的方式学习VBA(并且希望我去学校学习计算机编程!)。

我有大量工作簿和多个工作表。 G列中突出显示为黄色的单元格需要以特定方式格式化,以便将文件正确导入基于Web的程序。它们需要保持黄色突出显示,右/底对齐,以及mm / dd / yyyy的自定义格式。我记录了一个执行查找/替换的宏,尝试用突出显示的黄色,底部/右对齐,自定义格式mm / dd / yyyy替换列G中的所有黄色突出显示的单元格,但它不仅仅将替换限制为G列我也不知道如何在完成之前让宏遍历所有工作表。帮助?!

这是我从基本的宏录制中得到的:

Sub Macro2()
'
' Macro2 Macro
'

'
    Columns("G:G").Select
    Range("G:G").Activate
    With Application.FindFormat.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Application.ReplaceFormat.Clear
    Application.ReplaceFormat.NumberFormat = "mm/dd/yyyy"
    With Application.ReplaceFormat
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
    End With
    With Application.ReplaceFormat.Font
        .Subscript = False
        .TintAndShade = 0
    End With
    With Application.ReplaceFormat.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
End Sub

已添加的帖子:请参阅我试图重新格式化的典型表格的屏幕截图。同样,我只需要担心更改黄色突出显示的单元格的格式,但我仍然无法将查找/替换限制为G列... [1]:[https://i.stack.imgur.com/wRu30.jpg] < / p>

3 个答案:

答案 0 :(得分:2)

这里有一些似乎按照你描述的方式执行的代码。我在代码中放置了很多Sub reformat() Dim sh As Worksheet, r As Range, cell As Range Set sh = ActiveSheet Set r = sh.Range("G1") r.Select If r.Offset(1, 0) <> "" Then Set r = sh.Range(r, r.End(xlDown)) r.Select For Each cell In r With cell .Select If .Interior.Color = 65535 Then .HorizontalAlignment = xlRight .VerticalAlignment = xlBottom .NumberFormat = "mm/dd/yyyy" End If End With Next For Each sh In ThisWorkbook.Worksheets 'place the above code in this loop if you want 'to apply the above to all worksheets in the workbook 'also remove the set sh=ActiveSheet line Next sh End Sub 语句,以便您可以通过逐步学习它来了解它的工作原理,但是一旦理解就应该删除所有这些语句。此外,我在底部有一些注释掉的代码,您可以使用它来循环遍历多个工作表。动画gif显示了我编写的示例上运行的代码。如果您有疑问,请告诉我。

{{1}}

enter image description here

答案 1 :(得分:1)

循环显示活动工作簿中的每个工作表,然后单元格颜色上的AutoFilter将更改应用于可见单元格。

sub yellowSpecial()
    dim w as long

    with activeworkbook
        for w=1 to .worksheets.count
            with worksheets(w)
                if .autofiltermode then .autofiltermode = false
                with .range(.cells(1, "G"), .cells(.rows.count, "G").end(xlup))
                    .autofilter field:=1, criteria1:=vbyellow, operator:=xlFilterCellColor
                    with .resize(.rows.count-1, .columns.count).offset(1,0)
                        if cbool(application.subtotal(103,.cells)) then
                            with .specialcells(xlcelltypevisible)
                                .HorizontalAlignment = xlRight
                                .VerticalAlignment = xlBottom
                                .numberformat = "mm/dd/yyyy"
                            end with
                        end if
                    end with
                end with
                if .autofiltermode then .autofiltermode = false
            end with
        next w
    end with
end sub

答案 2 :(得分:1)

如果您正在尝试理解录制的代码,首先要做的是摆脱所有添加但无法执行任何操作的无关冗长的代码。无论您是否需要,录制的代码都涵盖了操作的所有方面。

这是仅使用所需内容重写原文。

Sub yellowSpecialReplace()
    Dim w As Long

    Application.DisplayAlerts = False

    With Application.FindFormat
        .Clear
        .Interior.Color = 65535
    End With
    With Application.ReplaceFormat
        .Clear
        .NumberFormat = "mm/dd/yyyy"
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
    End With

    With ActiveWorkbook
        For w = 1 To .Worksheets.Count
            With Worksheets(w).Columns("G:G")
                .Cells.Replace What:=vbNullString, Replacement:=vbNullString, _
                               LookAt:=xlPart, SearchOrder:=xlByRows, _
                               SearchFormat:=True, ReplaceFormat:=True
            End With
        Next w
    End With

    Application.DisplayAlerts = True
End Sub