Codehelp:寻找列,并格式化单元格

时间:2012-12-16 19:25:58

标签: vba excel-vba excel-2010 excel

我的macrocode有点问题,需要你的建议。这是我的基本宏代码:

Option Explicit

Sub NurZumUeben()

'oberste Zeile löschen, fixieren und linksbündig ausrichten
Rows("1:1").Select
Selection.Delete Shift:=xlUp
With ActiveWindow
   .SplitColumn = 0
   .SplitRow = 1
End With
ActiveWindow.FreezePanes = True

'Jede zweite Zeile schattieren
Application.ScreenUpdating = False
Dim Zeile, ZeilenNr As Integer
With ActiveSheet.UsedRange.Rows
   .Interior.ColorIndex = xlNone
   .Borders.ColorIndex = xlNone
End With
ZeilenNr = 2
For Zeile = 2 To ActiveSheet.UsedRange.Rows.Count
    With Rows(Zeile)
        If .Hidden = False Then
            If ZeilenNr Mod 2 = 0 Then
                .Interior.ColorIndex = 15
                .Borders.Weight = xlThin
                .Borders.ColorIndex = 16
                ZeilenNr = ZeilenNr + 1
            Else
                ZeilenNr = ZeilenNr + 1
            End If
        End If
    End With
Next Zeile
Application.ScreenUpdating = True


'oberste Zeile einfärben
Rows("1:1").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With




'Spalte_suchen&formatieren
Dim iLeSpa     As Integer
Dim iSpalte    As Integer
Dim bGefunden  As Boolean

iLeSpa = IIf(IsEmpty(Cells(1, Columns.Count)), Cells(1, _
  Columns.Count).End(xlToLeft).Column, Columns.Count)

For iSpalte = 1 To iLeSpa
   If Cells(1, iSpalte).Value = "click_thru_pct" Then
     bGefunden = True
     Exit For
  End If
Next iSpalte

If bGefunden Then
  With Range(Cells(2, iSpalte), Cells(5000, iSpalte))
     .Replace What:="%", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows
     Range("K1") = 100
     Range("K1").Copy
     .PasteSpecial Paste:=xlPasteAll, Operation:=xlDivide
     .NumberFormat = "0.00%"
     Range("K1").Clear
  End With
Else
  MsgBox "Die Überschrift  ""click_thru_pct""  wurde nicht gefunden.", _
     48, "   Hinweis für " & Application.UserName
End If

End Sub

感谢所有能提供帮助的人。不幸的是,我得到的最终格式并不完全

以下是结果:example

我不想为整个列着色,只想为顶行。此外,较低的空白字段与丑陋的0.00%不断格式化。

此外,我注意到在第一行着色后,字段K1可见。遗憾的是,这对我来说是不切实际的,因为这些Excel文档在行中也可以有所不同。

以下是您可以在必要时对其进行测试的文档。 example

非常感谢

3 个答案:

答案 0 :(得分:1)

更改模块化函数以计算for循环变量。我认为没有为此使用单独的变量的目的。改变这个:

ZeilenNr = 2
    For Zeile = 2 To ActiveSheet.UsedRange.Rows.Count
        With Rows(Zeile)
            If .Hidden = False Then
                If ZeilenNr Mod 2 = 0 Then
                    .Interior.ColorIndex = 15
                    .Borders.Weight = xlThin
                    .Borders.ColorIndex = 16
                    ZeilenNr = ZeilenNr + 1
                Else
                    ZeilenNr = ZeilenNr + 1
                End If
            End If
        End With
    Next Zeile

对此:

    For Zeile = 2 To ActiveSheet.UsedRange.Rows.Count
        With Rows(Zeile)
            If .Hidden = False Then
                If Zeile Mod 2 = 0 Then
                    .Interior.ColorIndex = 15
                    .Borders.Weight = xlThin
                    .Borders.ColorIndex = 16
                End If
            End If
        End With
    Next Zeile
如果我在这里遗漏了什么,我道歉。另外,我无法查看您提供的示例,因为该网站需要登录而且不是英文版。对不起。

答案 1 :(得分:0)

在现有代码中

  1. 5000

  2. 替换ActiveSheet.UsedRange.Rows.Count
  3. Range("K1").Clear

  4. 替换Range("K1").ClearContents

答案 2 :(得分:0)

而不是For Zeile = 2 To ActiveSheet.UsedRange.Rows.Count,您可以使用

For Zeile = 2 To ActiveSheet.Range("A1").CurrentRegion.Rows.Count-1

.UsedRange并非始终正确重置。您的样本似乎是.CurrentRegion

的合适人选