简体   繁体   中英

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.

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.

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...

The run on open ones, bring up the macro with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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