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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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,214,605
Messages
6,120,476
Members
448,967
Latest member
visheshkotha

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