Choose Range() in VBA

Useful

Active Member
Joined
Mar 16, 2011
Messages
494
Hi Mr Excel!
I've creted the macro code below and i want to select range from "g" to "h" and apply the formula (from B23) to this range

Sub Proper()

Dim f As String
Dim g As Range
Dim h As Range

f = Range("B23").Value

Range("M60000").End(xlUp).Offset(1, -5).Value = g
Range("M60000").End(xlUp).Offset(20, -5).Value = h
Range(g, h).Select = f


End Sub

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
For instance:

Code:
Sub Proper()

    Range("M60000").End(xlUp).Offset(1, -6).Resize(20, 2).Value = Range("B23").Value

End Sub

Please spend time learning the basics of a programming language like VBA, then you will understand that a line like

Code:
Range("M60000").End(xlUp).Offset(1, -5).Value = g

operates from left to right: you fill a range with an (empty) variable g. Not the other way round: g is not a variable containing a range.
 
Upvote 0
try this modification

Code:
Sub Proper()
Dim f As String
Dim g As Range
Dim h As Range
f = Range("B23").Value
Set g = Range("M60000").End(xlUp).Offset(1, -5)
Set h = Range("M60000").End(xlUp).Offset(20, -5)
'Range(g, h).Select = f
Range(g, h).FormulaArray = f
End Sub

is this what you want.
 
Upvote 0
Thanks for your answer i'm newbie in VBA and i'm tring to learn a lot with your help
Yes this code is working thanks a lot!
 
Upvote 0
try this modification

Code:
Sub Proper()
Dim f As String
Dim g As Range
Dim h As Range
f = Range("B23").Value
Set g = Range("M60000").End(xlUp).Offset(1, -5)
Set h = Range("M60000").End(xlUp).Offset(20, -5)
'Range(g, h).Select = f
Range(g, h).FormulaArray = f
End Sub
is this what you want.

Thanks venkat1926 this was very helpfull too!
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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