excel中用vba让指定范围单元格一个一个随机变得完全透明代码打在哪里
时间: 2024-01-26 13:03:56 浏览: 156
您可以在Excel的Visual Basic Editor中打开您要编辑的工作簿,然后在工具栏中选择插入->模块,将下面的代码复制到模块中:
```
Sub RandomTransparency()
Dim rng As Range
For Each rng In Selection
rng.Interior.ColorIndex = xlNone
rng.Interior.Pattern = xlPatternNone
rng.Font.ColorIndex = xlColorIndexNone
rng.Font.ThemeColor = xlThemeColorLight1
rng.Font.TintAndShade = 0
rng.Font.Bold = False
rng.Font.Italic = False
rng.Font.Underline = xlUnderlineStyleNone
rng.Font.Strikethrough = False
rng.Font.FontStyle = "Regular"
rng.Font.Size = 11
rng.Font.ThemeFont = xlThemeFontMinor
rng.Select
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, rng.Left, rng.Top, rng.Width, rng.Height).TextFrame.Characters.Text = rng.Value
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame.Characters.Font.Size = 0.1
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Line.Visible = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Fill.Visible = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.VerticalAnchor = msoAnchorMiddle
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Fill.Visible = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Bold = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Italic = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Underline = msoFalse
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Size = 0.1
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Color.RGB = RGB(255, 255, 255)
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Name = "Arial"
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Characters.Text = rng.Value
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.PresetShape = msoTextEffectShapeRound1
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.Alignment = msoTextEffectAlignmentCentered
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.FontSize = 1
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.FontBold = msoFalse
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.FontItalic = msoFalse
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.FontUnderline = msoFalse
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.FontName = "+mn-lt"
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.FontColor.RGB = RGB(255, 255, 255)
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.Perspective = msoTextEffectPerspectiveNone
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextEffect.TransformText = msoTextEffectTransformSlantUp
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.AutoSize = msoAutoSizeShapeToFitText
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.VerticalAnchor = msoAnchorMiddle
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Fill.Visible = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Bold = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Italic = False
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Underline = msoFalse
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Size = 0.1
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Color.RGB = RGB(255, 255, 255)
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Font.Name = "Arial"
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).TextFrame2.TextRange.Characters.Text = rng.Value
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Select
Selection.ShapeRange.Fill.Transparency = WorksheetFunction.RandBetween(50, 100)
Next rng
End Sub
```
然后,您需要选择要随机透明化的单元格范围,然后按下快捷键Alt+F8,选择RandomTransparency并点击运行即可。
阅读全文