Update the code to just change the format of a few columns instead of the whole sheet

An Quala

Board Regular
Joined
Mar 21, 2022
Messages
146
Office Version
  1. 2021
Platform
  1. Windows
Hello, what if I just to change the formats of 2 columns instead of updating for the whole sheet?

Columns D and F in "Sponsored Products Campaigns" and "Sponsored Brands Campaigns"

Columns D and E in "Sponsored Display Campaigns"

Only these columns not the whole sheets please,

Please help?

VBA Code:
Dim c As Range, va, x
     For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
        Set c = Worksheets(x).UsedRange
        If c.Address <> "$A$1" Then
        va = c.Value
        Worksheets(x).Cells.NumberFormat = "General"
        c = va
        End If
    Next
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this:
VBA Code:
Sub formatcells()
  Dim c As Range, va, x
  For Each x In Split("Sponsored Products Campaigns|Sponsored Brands Campaigns|Sponsored Display Campaigns", "|")
    Set c = Worksheets(x).UsedRange
    If c.Address <> "$A$1" Then
      va = c.Value
      Select Case x
        Case "Sponsored Products Campaigns", "Sponsored Brands Campaigns"
          Worksheets(x).Range("D:D, F:F").NumberFormat = "General"
        Case "Sponsored Display Campaigns"
          Worksheets(x).Range("D:E").NumberFormat = "General"
      End Select
      c = va
    End If
  Next
End Sub

Or like this to avoid typing the sheet names again:
VBA Code:
Sub formatcells2()
  Dim c As Range, va, arr
  Dim i As Long
  arr = Array("Sponsored Products Campaigns", "Sponsored Brands Campaigns", "Sponsored Display Campaigns")
  For i = 0 To UBound(arr)
    Set c = Worksheets(arr(i)).UsedRange
    If c.Address <> "$A$1" Then
      va = c.Value
      Select Case i
        Case 0, 1
          Worksheets(arr(i)).Range("D:D, F:F").NumberFormat = "General"
        Case 2
          Worksheets(arr(i)).Range("D:E").NumberFormat = "General"
      End Select
      c = va
    End If
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,326
Messages
6,124,267
Members
449,149
Latest member
mwdbActuary

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