PC Review


Reply
Thread Tools Rate Thread

Applying a macro to a range of cells

 
 
Nick Gilbert
Guest
Posts: n/a
 
      15th Jun 2007
Hi,

I've written a macro which creates URLs from part numbers in the active
cell using a part number in the cell immediately left. However it only
works if I select a single cell. As soon as I select multiple cells, the
macro seems to have no effect.

Can somebody please tell me how I modify it so it works if I select an
entire column:

Sub Macro2()
Dim leftCellValue
leftCellValue = ActiveCell.Offset(0, -1).Value
If Left(leftCellValue, 2) = "92" Then

ActiveCell.Hyperlinks.Add Anchor:=Selection,
Address:="http://website/product.aspx?part=" & ActiveCell.Offset(0,
-1).Value
ActiveCell.Value = "view"
End If
End Sub

Thanks,

Nick....
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UG9wcyBKYWNrc29u?=
Guest
Posts: n/a
 
      15th Jun 2007
I would use a "Do - Loop until Activecell.Offset(0, -1) = "" " with
Activecell.Offset(1,0) just before "Loop..."
--
Pops Jackson


"Nick Gilbert" wrote:

> Hi,
>
> I've written a macro which creates URLs from part numbers in the active
> cell using a part number in the cell immediately left. However it only
> works if I select a single cell. As soon as I select multiple cells, the
> macro seems to have no effect.
>
> Can somebody please tell me how I modify it so it works if I select an
> entire column:
>
> Sub Macro2()
> Dim leftCellValue
> leftCellValue = ActiveCell.Offset(0, -1).Value
> If Left(leftCellValue, 2) = "92" Then
>
> ActiveCell.Hyperlinks.Add Anchor:=Selection,
> Address:="http://website/product.aspx?part=" & ActiveCell.Offset(0,
> -1).Value
> ActiveCell.Value = "view"
> End If
> End Sub
>
> Thanks,
>
> Nick....
>

 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      15th Jun 2007
here's one way

Sub Macro2()
Dim leftCellValue As Long
Dim cell As Range
Dim lastrow As Long, i As Long
Dim ws As Worksheet

Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "B").End(xlUp).Row

For Each cell In ws.Range("B2:B" & lastrow)
leftCellValue = Left(cell.Value, 2)
If leftCellValue = "92" Then
cell.Offset(0, 1).Hyperlinks.Add Anchor:=cell.Offset(0, 1), _
Address:="http://website/product.aspx?part=" & cell.Value
cell.Offset(0, 1).Value = "view"
End If
Next
End Sub

--


Gary


"Nick Gilbert" <(E-Mail Removed)> wrote in message
news:u%(E-Mail Removed)...
> Hi,
>
> I've written a macro which creates URLs from part numbers in the active cell
> using a part number in the cell immediately left. However it only works if I
> select a single cell. As soon as I select multiple cells, the macro seems to
> have no effect.
>
> Can somebody please tell me how I modify it so it works if I select an entire
> column:
>
> Sub Macro2()
> Dim leftCellValue
> leftCellValue = ActiveCell.Offset(0, -1).Value
> If Left(leftCellValue, 2) = "92" Then
>
> ActiveCell.Hyperlinks.Add Anchor:=Selection,
> Address:="http://website/product.aspx?part=" & ActiveCell.Offset(0, -1).Value
> ActiveCell.Value = "view"
> End If
> End Sub
>
> Thanks,
>
> Nick....



 
Reply With Quote
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      15th Jun 2007
Try this:

Sub Macro2()
Dim r As Range
Dim myRange As Range
Dim leftCellValue

Set myRange = Selection
For Each r In myRange
If r.Column >= 2 Then
leftCellValue = r.Offset(0, -1).Value
If Left(leftCellValue, 2) = "92" Then
r.Hyperlinks.Add Anchor:=r, _
Address:="http://website/product.aspx?part=" & r.Offset(0,
-1).Value
r.Value = "view"
End If
End If
Next r

End Sub



HTH,
Barb Reinhardt

"Nick Gilbert" wrote:

> Hi,
>
> I've written a macro which creates URLs from part numbers in the active
> cell using a part number in the cell immediately left. However it only
> works if I select a single cell. As soon as I select multiple cells, the
> macro seems to have no effect.
>
> Can somebody please tell me how I modify it so it works if I select an
> entire column:
>
> Sub Macro2()
> Dim leftCellValue
> leftCellValue = ActiveCell.Offset(0, -1).Value
> If Left(leftCellValue, 2) = "92" Then
>
> ActiveCell.Hyperlinks.Add Anchor:=Selection,
> Address:="http://website/product.aspx?part=" & ActiveCell.Offset(0,
> -1).Value
> ActiveCell.Value = "view"
> End If
> End Sub
>
> Thanks,
>
> Nick....
>

 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      15th Jun 2007
forgot to mention you need to change the range and sheetname to what you're
using

--


Gary


"Gary Keramidas" <GKeramidasATmsn.com> wrote in message
news:(E-Mail Removed)...
> here's one way
>
> Sub Macro2()
> Dim leftCellValue As Long
> Dim cell As Range
> Dim lastrow As Long, i As Long
> Dim ws As Worksheet
>
> Set ws = Worksheets("Sheet1")
> lastrow = ws.Cells(Rows.Count, "B").End(xlUp).Row
>
> For Each cell In ws.Range("B2:B" & lastrow)
> leftCellValue = Left(cell.Value, 2)
> If leftCellValue = "92" Then
> cell.Offset(0, 1).Hyperlinks.Add Anchor:=cell.Offset(0, 1), _
> Address:="http://website/product.aspx?part=" & cell.Value
> cell.Offset(0, 1).Value = "view"
> End If
> Next
> End Sub
>
> --
>
>
> Gary
>
>
> "Nick Gilbert" <(E-Mail Removed)> wrote in message
> news:u%(E-Mail Removed)...
>> Hi,
>>
>> I've written a macro which creates URLs from part numbers in the active cell
>> using a part number in the cell immediately left. However it only works if I
>> select a single cell. As soon as I select multiple cells, the macro seems to
>> have no effect.
>>
>> Can somebody please tell me how I modify it so it works if I select an entire
>> column:
>>
>> Sub Macro2()
>> Dim leftCellValue
>> leftCellValue = ActiveCell.Offset(0, -1).Value
>> If Left(leftCellValue, 2) = "92" Then
>>
>> ActiveCell.Hyperlinks.Add Anchor:=Selection,
>> Address:="http://website/product.aspx?part=" & ActiveCell.Offset(0, -1).Value
>> ActiveCell.Value = "view"
>> End If
>> End Sub
>>
>> Thanks,
>>
>> Nick....

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro not applying to entire range =?Utf-8?B?YmVhcnNmYW4=?= Microsoft Excel Programming 1 10th Oct 2006 07:10 PM
applying math operation across range of cells yb Microsoft Excel Programming 3 20th May 2006 07:04 AM
applying a macro for a specific field to a range of fields Craig Microsoft Excel Programming 3 12th Oct 2005 05:27 AM
Applying formula to only NON-EMPTY cells in range =?Utf-8?B?VGFzaQ==?= Microsoft Excel Misc 5 29th Mar 2005 10:48 PM
Copying and applying formula to range of cells =?Utf-8?B?TWlrZSBSaWNo?= Microsoft Excel New Users 5 21st Jun 2004 03:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:18 PM.