有人能帮我使用宏/VBA 吗,因为我的要求对我来说有点高级。
我有一个 Excel 工作簿,里面有 10 张数据。
我所拥有的宏基本上重新格式化并更改了工作表上的某些元素,它非常基础,但它完成了工作。
以前我只有 1 张工作表,但现在我有 10 张工作表,我需要宏循环遍历工作表并对每张工作表进行相同的更改,但前提是工作表的单元格 A1 中不包含值“UK”。如果工作表的单元格 A1 中包含 UK,我希望它跳过该工作表,不进行任何更改并转到下一张工作表。
举个例子,假设我想删除 H 列,并将文本颜色更改为红色。如何使用上面概述的规则来实现这一点?
我希望这是有道理的。
提前致谢
仅供参考,我正在使用 excel 2016
更新:
我记录的宏如下;
Sub Sort_Data()
'
' Sort_Data Macro
'
'
Sheets("Sheet2").Select
Columns("AG:AI").Select
Selection.Delete Shift:=xlToLeft
Columns("AE:AE").Select
Selection.Delete Shift:=xlToLeft
Columns("H:AA").Select
Range("AA1").Activate
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Cut
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Columns("J:M").Select
Selection.Cut
Columns("H:H").Select
Selection.Insert Shift:=xlToRight
Columns("B:B").Select
Selection.Copy
Columns("P:P").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("P:P").Select
Application.CutCopyMode = False
ExecuteExcel4Macro _
"FORMULA.REPLACE(""$"","""",2,1,FALSE,FALSE,,FALSE,FALSE,FALSE,FALSE)"
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-9]"
Selection.AutoFill Destination:=Range("Q2:Q6")
Range("Q2:Q6").Select
Columns("H:H").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("R:R").Select
Selection.Copy
Columns("H:H").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("Q:R").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("H1").Select
ExecuteExcel4Macro "PATTERNS(0,0,0,,2,2,0,0)"
Selection.Locked = True
Selection.FormulaHidden = False
ActiveCell.FormulaR1C1 = "Efficiency"
Columns("B:O").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
Cells.Select
Selection.Columns.AutoFit
Range("A1").Select
End Sub
我尝试使用这里解释的循环函数但失败了:
但我就是无法理解。我完全不知道如何实现“根据 A1 值跳过表格”
谢谢
答案1
这可以相当简单地完成,允许您只运行一次 VBA 代码执行,而不是基于每个工作表。
Public Sub AmendSheets()
Dim i As Integer
' Start a count to go from sheet 1 to the total number of sheets
For i = 1 To Application.Sheets.Count
' Select each sheet individually
Worksheets(i).Activate
' Check for UK to see if changes are required
If Range("A1").Value <> "UK" Then
' Put your changes in here
End If
Next i
End Sub
答案2
这是您需要的解决方案:
Public Function SheetReview()
Dim wkb As Workbook
Dim wks As Worksheet
Set wkb = ThisWorkbook 'This object references the workbook
wscount = wkb.Worksheets.Count 'Number of sheets in the workbook
For i = 1 To wscount ' Loop one by one the sheets
Set wks = wkb.Worksheets(i) 'Assign the current sheeet to object wks
If wks.Cells(1, 1) <> "UK" Then ' If cell A1 is not UK
With wks 'Let's work on the object wks
'**********************
'From here goes you code:
'Sheets("Sheet2").Select <- Now you don't need this. it's referenced in the object wks
Columns("AG:AI").Select
Selection.Delete Shift:=xlToLeft
.
.
.
End With
End If
Next i
End Function
Alt使用+打开 VBA / 宏F11,右键单击工作簿并插入新的模块。
将代码粘贴到右侧。