Replace a lot of cells by something shorter

Hyakkivn

Board Regular
Joined
Jul 28, 2021
Messages
81
Office Version
  1. 2010
Platform
  1. Windows
VBA Code:
Dim ws As Worksheet, cell As Range
Set ws = ActiveSheet
Set nd = ws.Range("B10:B30")
For Each cell In nd
If cell.Value = ws.Range("L10") Or cell.Value = ws.Range("L11") Or cell.Value = ws.Range("L12") Or cell.Value = ws.Range("L13") Or cell.Value = ws.Range("L14") Or cell.Value = ws.Range("L15") Or cell.Value = ws.Range("L16") Or cell.Value = ws.Range("L17") _
Or cell.Value = ws.Range("L18") Or cell.Value = ws.Range("L19") Or cell.Value = ws.Range("L20") Or cell.Value = ws.Range("L21") Or cell.Value = ws.Range("L22") Or cell.Value = ws.Range("L23") _
Or cell.Value = ws.Range("L24") Or cell.Value = ws.Range("L25") Or cell.Value = ws.Range("L26") Or cell.Value = ws.Range("L27") Or cell.Value = ws.Range("L28") Or cell.Value = ws.Range("L29") _
Or cell.Value = ws.Range("L30") Or cell.Value = ws.Range("L31") Or cell.Value = ws.Range("L32") Or cell.Value = ws.Range("L33") Then
    cells(cell.Row, 7).NumberFormat = "#,##0.00"
    Else
    cells(cell.Row, 7).NumberFormat = "General"
End If
Above is my code for auto formating base on cell value, and it works. I have a question: Is there any way to replace those "L" Cells with something shorter like
VBA Code:
ws.range("L" & i) for i = 10 to 33
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You could use:

Code:
If application.countif(ws.range("L10:L33"), cell.value) > 0 then
 
Upvote 0
Solution
You could use:

Code:
If application.countif(ws.range("L10:L33"), cell.value) > 0 then
Thank you for fast reply. That's more brilliant way. You are such a big help !!!
 
Upvote 0

Forum statistics

Threads
1,215,276
Messages
6,124,006
Members
449,137
Latest member
abdahsankhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top