简体   繁体   English

Excel-VBA条件格式

[英]Excel - VBA conditional formatting

I am creating a spreadsheet that will serve as an electronic order form. 我正在创建一个电子表格,它将用作电子订单。 Some of the cells require specific types of values. 一些单元格需要特定类型的值。 For example, for a few column, only certain numbers are valid. 例如,对于几列,仅某些数字有效。 In another column, In total, there are 17 columns that will need conditional formatting applied. 在另一列中,总共有17列需要应用条件格式。

I was able to do what I need using the conditional formatting, but since I want to apply these rules to many spreadsheets, I think this needs to be kind of converted into VBA code in order to make it a macro. 我可以使用条件格式来执行所需的操作,但是由于我想将这些规则应用于许多电子表格,因此我认为需要将其转换为VBA代码以使其成为宏。

First of all, is this possible? 首先,这可能吗? And if so, how would I go about doing it? 如果是这样,我将如何去做呢? I've tried the "record macro" function, but it doesn't seem to capture what I'm doing exactly. 我已经尝试过“记录宏”功能,但是它似乎无法准确地捕捉到我在做什么。 Can someone point me in the right direction in how I can get this working? 有人能指出我正确的工作方向吗? Ideally, I would like to have one macro run all these conditional formats at once. 理想情况下,我想让一个宏一次运行所有这些条件格式。

You should be able to record the macro in the first s/sheet you use then use this going forward. 您应该能够在所使用的第一张纸/页中记录该宏,然后继续使用。 Here's the potential issue, when recording the macro you may be being too specific, look at the code see if its pointing to a specific workbook, or sheet as you may not be using the same names for other documents, keep the first doc open when doing on other ones. 这是潜在的问题,在记录宏时,您可能过于具体,请查看代码,看看它是否指向特定的工作簿或工作表,因为您可能未在其他文档中使用相同的名称,请在打开第一个文档时做其他的。 Make sure macro is not pointing to a specific worksheet, replace this code, with info such as activeworksheets.etc... 确保宏未指向特定的工作表,请使用诸如activeworksheets.etc之类的信息替换此代码。

The run on open ones, bring up the macro with alt f8, 在开放的代码上运行,用alt f8调出宏,

FYI..This is a work around, but wont involve you having to call objects and loop through sheets with your code, essentially a bit cagey but will work if you need to sort without someone doing for you, or you could research a bit more 仅供参考。这是一种变通方法,但是不会涉及到必须调用对象并用代码循环遍历工作表,本质上有点笼统,但是如果您需要在没有其他人为您做的情况下进行排序就可以工作,或者您可以进行更多研究

Yes it is possible. 对的,这是可能的。 The way you would go about doing it depends on whether the validation rules are the same or varies for each spreadsheet. 您执行此操作的方式取决于每个电子表格的验证规则是相同还是不同。 If same, then one large macro should work (record all the steps then loop through the sheets, or use the macro repeatedly). 如果相同,则应使用一个大宏(记录所有步骤,然后遍历工作表,或重复使用宏)。

If each sheet is unique in its validation needs, then you should consider making many small macros, and then combining into bigger macros based on the patterns you see in your spreadsheets. 如果每张纸的验证需求都是唯一的,那么您应该考虑制作许多小宏,然后根据您在电子表格中看到的模式组合成更大的宏。

For example, here are four small macros that apply formats, conditional formatting, and validation rule: (these are just examples. You would record your own macros) 例如,以下是四个应用格式,条件格式和验证规则的小宏:(这些只是示例。您将记录自己的宏)

'
Sub formatInteger()
    '(record a macro that applies integer formatting to the selection)
End Sub

'apply to selection: data type is date
Sub formatDate()
    '(record a macro that applies date formatting to the selection)
End Sub

'apply to selection: highlight duplicates
Sub conditionalFormat_Duplicates()
     '(record a macro that applies integer formatting to the selection)
End Sub

'apply to selection: integers must be -100 to 100
Sub dataValidation_IntegersWithRange()
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
        Operator:=xlBetween, Formula1:="-100", Formula2:="100"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

These macros were made via recorder, and each macro affects selected cells... So from here, either assign hotkeys and use them manually. 这些宏是通过记录器创建的,每个宏都会影响选定的单元格...因此,从这里开始,要么分配热键,要么手动使用它们。 Or, use them to build bigger macros as such: 或者,使用它们来构建更大的宏,例如:

Sub validationCombination1()
    Columns("A:A").Select
    formatDate

    Columns("B:B").Select
    formatInteger
End Sub

Sub validationCombination2()
    Columns("A:A").Select
    formatDate
    conditionalFormat_Duplicates

    Columns("B:B").Select
    formatInteger
    dataValidation_IntegersWithRange
End Sub

Again, this is just one hypothetical approach based on the information you provided. 同样,这只是基于您提供的信息的一种假设方法。 To receive better recommendations, please provide the data you are working with or the code you are trying to implement 为了获得更好的建议,请提供您正在使用的数据或您尝试实现的代码

a "dirty" way to get your goal would be: 一种实现目标的“肮脏”方法是:

Sub DirtyCopyFormat()

  Const sheet1 As String = "SourceSheet"
  Dim sheet2 As String

  sheet2 = ActiveSheet.Name
  'copy the source with all formating and values
  Worksheets(sheet1).Copy , Worksheets(sheet2)
  'overwrite the old values with the new ones / change "A1:BZ999" to your needs
  ActiveSheet.Range("A1:BZ999").Value = Worksheets(sheet2).Range("A1:BZ999").Value

  'this code if you dont need the old sheed
  Application.DisplayAlerts = False
  Worksheets(sheet2).Delete
  Application.DisplayAlerts = True
  'end

  'this code if you want to keep it as [name-old]
  Worksheets(sheet2).Name = sheet2 & "-old"
  'end

  'rename new sheet
  ActiveSheet.Name = sheet2

End Sub

There sure are better ways to do it... however... you should get wat you want 当然有更好的方法可以做到...但是...您应该得到想要的水

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM