Irregular Range shapes

MuchToLearn

New Member
Joined
Dec 15, 2008
Messages
31
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2010
Platform
  1. Windows
I know this is very simple problem for the people who provide helps here. I got a bit lost when I was practicing some VBA coding.
If say I want to fill cells A1:G1 & A2:E2 with values. At first, I put
VBA Code:
Sub FillRange1()
Dim RngCellUnit, RngRangToFill As Range
Set RngRangeToFill = Range("A1:G2")
For Each RngCellUnit In RngRangeToFill
  RngCellUnit.Value = 1
Next RngCellUnit
Range("F2:G2") = ""
End Sub
Then I though it’s kind of double handling with filling in values, then remove some of them, so I tried.
VBA Code:
Sub FillRange2()
Dim RngCellUnit, RngRangeToFill As Range
Set RngRangToFill = Range("A1:G1", "A2:E2")
For Each RngCellUnit In RngRangToFill
  RngCellUnit.Value = 1
Next RngCellUnit
End Sub
But not really work as I expected, the whole of range A1:G2 got filled. I don’t understand enough about the Excel VBA now to know why. So, this my first question.
Declare a separate range for A2:E2 adds to code too. Is there a simpler solution? That’s my second question.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You are using the two argument version of Range and it is returning the range A21:G1 and A2:E2 and all the cells inbetween.
Try the one argument version
VBA Code:
Set RngRangToFill = Range("A1:G1, A2:E2")
 
Upvote 0
Solution
Both ranges can be in the same quotes.

VBA Code:
Sub FillRange2()
    Dim RngCellUnit, RngRangeToFill As Range
    Set RngRangToFill = Range("A1:G1,A2:E2")
    For Each RngCellUnit In RngRangToFill.Cells
        RngCellUnit.Value = 1
    Next RngCellUnit
End Sub
 
Upvote 0
Wow, thanks guys. So, its simple matter placing “ ”s in this case. Haven’t had much luck finding a site link that explains & distinguishes the usage when I searched on the internet.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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