Selection Range

G2K

Active Member
Joined
May 29, 2009
Messages
355
Hi All.

i need to check values in the selection and perform some calculation accoringly

Selection can be vertical as -
A1 P
A2 P
A3 P
A4 P
A5 P
A6 P
A7 P
A8 P
A9 P
A10 P

or Horiz. as -

A1 A2 A3 A4 A5 A6 A7
CL CL CL CL CL CL CL

i want to set range accordingly and do the calculation using for loop.

Pls help.....

thanks in advance
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Hi, Select range Horizontal or Vertical, Run code.
The Msgbox will show the address and value of each cell in range.
Modify code for your purpose.
Code:
[COLOR=navy]Sub[/COLOR] MG07Oct19
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Selection
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    MsgBox Dn.Address & " / " & Dn.Value
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
You can also just refer to the Selection
Code:
Sub Something()
Dim cl as Range
For Each cl in Selection
  'some code
Next cl
End Sub

lenze
 

G2K

Active Member
Joined
May 29, 2009
Messages
355
thanks for your reply.

but i am using spreadsheet contorl here and it generate an error -

'Run Time Error '13'
Type Mismatch

Regards
 

G2K

Active Member
Joined
May 29, 2009
Messages
355
Dim Rng As Range
Dim cl As Range
Call dbConnect
Set Rng = Spreadsheet1.Selection' Generate Error - " Type Mismatch"

For Each cl In Rng
Sql = "Update tblAttendance Set Status = 'CL' where EmpId = " & Spreadsheet1.Cells(Spreadsheet1.ActiveCell.Row, 1).Value & " and WorkingDate = #" & VBA.Format(Spreadsheet1.Cells(1, Spreadsheet1.ActiveCell.Column).Value, "dd - mmm - yyyy") & "#"

Con.BeginTrans
Con.Execute Sql
Con.CommitTrans

Next cl

Set Rng = Spreadsheet1.Selection' Generate Error - " Type Mismatch"
For Each cl In Rng - " Type Mismatch"
where Spreadsheet1 is Spreadsheet Object.

thanks
 
Last edited:

G2K

Active Member
Joined
May 29, 2009
Messages
355
thnaks Mick/Lenze

finally i got the solution -

Dim Rng As Range
Dim i As Integer
Set Rng = Spreadsheet1.Selection
For i = 1 To Rng.Count

Spreadsheet1.Selection(i).Activate
'Code
Next i

thanks a ton for your precious time and suggestion
 

Watch MrExcel Video

Forum statistics

Threads
1,123,346
Messages
5,601,087
Members
414,426
Latest member
fraru

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
Top