0

我试图弄清楚如何将 Sub 的第一部分(对标题列表中的列进行操作)应用于其他类型的操作(原始设置为删除列表中的列)。

我的尝试几乎奏效了,它对列表应用了一些简单的格式:“RespID,Score”

但 Sub 仅适用于列表“RespID”中的第一项

我在 Sub 中唯一改变的是在底部之后的二分之二'~~> Act on columns

谢谢

Sub FormatRespIDScore360()
Dim WS As Worksheet
Dim ColList As String, ColArray() As String
Dim lastCol As Long, i As Long, j As Long
Dim boolFound As Boolean
Dim delCols As Range
Dim lastRow As Long

On Error GoTo Whoa

Application.ScreenUpdating = False

Set WS = Sheets("360")

ColList = "RespID, Score"

ColArray = Split(ColList, ",")

'~~> Get the last column
lastCol = WS.Cells.Find(What:=" ", After:=WS.Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
MatchCase:=False).Column

For i = 1 To lastCol
    boolFound = False
    '~~> Checking of the current cell value is present in the array
    For j = LBound(ColArray) To UBound(ColArray)
        If UCase(Trim(WS.Cells(1, i).Value)) = UCase(Trim(ColArray(j))) Then
            '~~> Match Found
            boolFound = True
            Exit For
        End If
    Next
   '~~> If not match not found
    If boolFound = True Then
        If delCols Is Nothing Then
            Set delCols = WS.Columns(i)
        Else
            Set delCols = Union(delCols, WS.Columns(i))
        End If
    End If
Next i

'~~> Act on columns
If Not delCols Is Nothing Then

    With Sheets(1)
        lastRow = .Cells(.Rows.Count, delCols.Column).End(xlUp).Row
    End With


    With Sheets(1).Range(delCols, WS.Cells(lastRow, delCols.Column))

            .NumberFormat = "0"
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False

        End With

    End If
LetsContinue:
Application.ScreenUpdating = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
4

1 回答 1

1

这找到了一种相当复杂的方法来做一些非常简单的事情!我的关键问题是:

With Sheets(1).Range(delCols, WS.Cells(lastRow, delCols.Column))

您实际上希望将格式应用于具有某些标题的列 - 您应该找出这些列是什么,并将格式应用于它们。从一个非常简单的例子开始:

Dim colToFormat as Range
set colToFormat = Range("A1", "B25");

With colToFormat
    .NumberFormat = "0"
End With

这应该是代码的本质。现在你需要弄清楚要替换什么"A1""B25"用什么。您已经知道标题 - 那么为什么不在工作表上查找它们。

Sub fmt()
    ColList = "RespID,Score"
    colarray = Split(ColList, ",")
    Set colToFormat = Nothing
    For Each heading In colarray
    Set headingFound = Range("A:A").Offset(0, ActiveSheet.Cells.Find(What:=heading, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False).Column - 2)

      If colToFormat Is Nothing Then Set colToFormat = headingFound Else Set colToFormat = Union(colToFormat, headingFound)
    Next
    MsgBox colToFormat.Address

End Sub

需要注意的三点:

  1. 中的“分数”之前没有空格ColList。否则,该Split函数将在名称前添加一个空格,除非列标题中有实际空格,否则将找不到它(实际上,这可能是您的根本问题)
  2. 我只是展示了使用 MsgBox 找到了正确的列 - 这与确定要格式化的单元格并不完全相同(但它可以帮助您完成大部分工作)。
  3. 您必须弄清楚要格式化的最后一个单元格是什么。如果你想格式化整列,那么With colToFormat应该可以。

我认为以上内容应该对您有所帮助。记住——保持你的代码简单,否则你不会理解你在三个月内做了什么。并且在使用别人的代码时要非常小心......

于 2013-09-01T18:48:59.723 回答