Excel VBA: input values in array of non contiguous cells

Ellie3457

New Member
Joined
Jan 11, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I am new to VBA and trying to improve the way I write code because I currently use a combination of recording macro and some other things that I learnt and would like to be more efficient.

I have the following code but it is not working as intended.

VBA Code:
    Sheets("Main").Select
    Range("B2:B2, G2:G2, H2:H2").Value = Array("Address", "City", "Country")
    Range("B2:B2, G2:G2, H2:H2").Select
    With Selection.Font
        .Color = -16776961
        .TintAndShade = 0
    End With
    Range("B:B, G:G, H:H").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Instead of getting the value in the mentioned cells, the word address is repeated in the mentioned cells.

I was also wondering if there is a way to select multiple sheets at the same time and apply the same command to all of them.

The example below does not work

VBA Code:
    Sheets("Main","Report","Data").Select
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove


I would appreciate any help/advice.

Thanks in advance

Ellie
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Is it?
VBA Code:
Option Explicit
Sub test()
Dim sp As Variant, cell, k&
Dim ws As Worksheet
sp = Array("Address", "City", "Country")
For Each ws In Sheets ' loop thru every sheet
    k = 0
    For Each cell In Range("B2,G2,H2") ' loop thru each item in Array
        k = k + 1
        With cell
            .Value = sp(k - 1)
            .Font.Color = -16776961
            .Font.TintAndShade = 0
        End With
        With cell.EntireColumn.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Next
k = 0
Next
End Sub
 
Upvote 0
I think you'll need to assign those values one cell at a time. Also, there's no need to do any selecting. Try the following . . .

VBA Code:
    With Sheets("Main")
        .Range("B2").Value = "Address"
        .Range("G2").Value = "City"
        .Range("H2").Value = "Country"
        With .Range("B2, G2, H2").Font
            .Color = -16776961
            .TintAndShade = 0
        End With
        With Range("B:B, G:G, H:H").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End With
 
Upvote 0
Sorry, I understood that there were two separate questions. If this is not the case, please disregard my post.
 
Upvote 0
For the multiple sheet situation, you can construct a Sheets object. to access the .FillAcrossSheets method

VBA Code:
Dim MySheets as Sheets

ThisWorkbook.Sheets("Sheet1").Range("A1").Value = "hello"

Set MySheets = ThisWorkbook.WorkSheets(Array("Sheet1", "Sheet3", "Sheet7")

MySheets.FillAcrossSheets Sheets("Sheet1").Range("A1")
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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