PC Review


Reply
Thread Tools Rate Thread

Create a Macro to Insert a number into a column based on criteria

 
 
blackmanofsteel40@gmail.com
Guest
Posts: n/a
 
      8th Aug 2007
Hello,

I so used to working with Access that I'm having trouble trying how to
write this simple macro I need:

I want to create a macro that says if column A has "016" then insert
"234" into Column B. I need to do mulitple of these, so that was just
an example. I'm in a time crunch at work. Thanks a million in
advance !!!!!


(E-Mail Removed)

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      8th Aug 2007
Here is one way using find...

Public Sub AddStuff()
Call FindStuff(Columns("A"), "016", "234")
Call FindStuff(Columns("A"), "This", "That")
End Sub

Private Sub FindStuff(ByVal rngToSearch As Range, ByVal strWhat As String, _
ByVal strValue As String)
Dim rngFound As Range
Dim rngFoundAll As Range
Dim strFirstAddress As String

Set rngFound = rngToSearch.Find(What:=strWhat, _
LookAt:=xlWhole, _
LookIn:=xlValues, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
Set rngFoundAll = rngFound
Do
Set rngFoundAll = Union(rngFound, rngFoundAll)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
rngFoundAll.Offset(0, 1).Value = strValue
End If
End Sub
--
HTH...

Jim Thomlinson


"(E-Mail Removed)" wrote:

> Hello,
>
> I so used to working with Access that I'm having trouble trying how to
> write this simple macro I need:
>
> I want to create a macro that says if column A has "016" then insert
> "234" into Column B. I need to do mulitple of these, so that was just
> an example. I'm in a time crunch at work. Thanks a million in
> advance !!!!!
>
>
> (E-Mail Removed)
>
>

 
Reply With Quote
 
blackmanofsteel40@gmail.com
Guest
Posts: n/a
 
      8th Aug 2007
I proably have overlooked it, but where is column B referenced. I want
to insert 234 on every line (row) in column B where column A's line
(row) equals 016. This occurs multiple times in the worksheet, not
just one instance. If it does that, please forgive me. Thanks again



Jim Thomlinson wrote:
> Here is one way using find...
>
> Public Sub AddStuff()
> Call FindStuff(Columns("A"), "016", "234")
> Call FindStuff(Columns("A"), "This", "That")
> End Sub
>
> Private Sub FindStuff(ByVal rngToSearch As Range, ByVal strWhat As String, _
> ByVal strValue As String)
> Dim rngFound As Range
> Dim rngFoundAll As Range
> Dim strFirstAddress As String
>
> Set rngFound = rngToSearch.Find(What:=strWhat, _
> LookAt:=xlWhole, _
> LookIn:=xlValues, _
> MatchCase:=False)
> If Not rngFound Is Nothing Then
> strFirstAddress = rngFound.Address
> Set rngFoundAll = rngFound
> Do
> Set rngFoundAll = Union(rngFound, rngFoundAll)
> Set rngFound = rngToSearch.FindNext(rngFound)
> Loop Until rngFound.Address = strFirstAddress
> rngFoundAll.Offset(0, 1).Value = strValue
> End If
> End Sub
> --
> HTH...
>
> Jim Thomlinson
>
>
> "(E-Mail Removed)" wrote:
>
> > Hello,
> >
> > I so used to working with Access that I'm having trouble trying how to
> > write this simple macro I need:
> >
> > I want to create a macro that says if column A has "016" then insert
> > "234" into Column B. I need to do mulitple of these, so that was just
> > an example. I'm in a time crunch at work. Thanks a million in
> > advance !!!!!
> >
> >
> > (E-Mail Removed)
> >
> >


 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      8th Aug 2007
Column B is the offset of one column from A. Here is the line that does that.
rngFoundAll.Offset(0, 1).Value = strValue
What this code does is it seaches Column A for all instances (rngFoundAll)
of "016" or whatever you set as the second argument (strWhat). It then places
"234" (strValue) one column to the right of the items that it found.

It works with multiple instances and is very efficient in how it searches...
--
HTH...

Jim Thomlinson


"(E-Mail Removed)" wrote:

> I proably have overlooked it, but where is column B referenced. I want
> to insert 234 on every line (row) in column B where column A's line
> (row) equals 016. This occurs multiple times in the worksheet, not
> just one instance. If it does that, please forgive me. Thanks again
>
>
>
> Jim Thomlinson wrote:
> > Here is one way using find...
> >
> > Public Sub AddStuff()
> > Call FindStuff(Columns("A"), "016", "234")
> > Call FindStuff(Columns("A"), "This", "That")
> > End Sub
> >
> > Private Sub FindStuff(ByVal rngToSearch As Range, ByVal strWhat As String, _
> > ByVal strValue As String)
> > Dim rngFound As Range
> > Dim rngFoundAll As Range
> > Dim strFirstAddress As String
> >
> > Set rngFound = rngToSearch.Find(What:=strWhat, _
> > LookAt:=xlWhole, _
> > LookIn:=xlValues, _
> > MatchCase:=False)
> > If Not rngFound Is Nothing Then
> > strFirstAddress = rngFound.Address
> > Set rngFoundAll = rngFound
> > Do
> > Set rngFoundAll = Union(rngFound, rngFoundAll)
> > Set rngFound = rngToSearch.FindNext(rngFound)
> > Loop Until rngFound.Address = strFirstAddress
> > rngFoundAll.Offset(0, 1).Value = strValue
> > End If
> > End Sub
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "(E-Mail Removed)" wrote:
> >
> > > Hello,
> > >
> > > I so used to working with Access that I'm having trouble trying how to
> > > write this simple macro I need:
> > >
> > > I want to create a macro that says if column A has "016" then insert
> > > "234" into Column B. I need to do mulitple of these, so that was just
> > > an example. I'm in a time crunch at work. Thanks a million in
> > > advance !!!!!
> > >
> > >
> > > (E-Mail Removed)
> > >
> > >

>
>

 
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 Help - Insert row/copy based on criteria Katerinia Microsoft Excel Programming 3 7th Apr 2010 12:08 AM
Add a number sequence based on criteria in another column DanaK Microsoft Excel Worksheet Functions 3 26th Sep 2009 07:13 AM
Based on Mulipple Criteria acrossed Column count last column LBitler Microsoft Excel Worksheet Functions 1 12th Feb 2009 06:58 PM
Insert number of rows based on criteria S Davis Microsoft Excel Programming 6 10th Jan 2009 05:04 AM
move contents of column C based on criteria related to column A =?Utf-8?B?RGVicmE=?= Microsoft Excel Misc 2 27th Dec 2005 10:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:29 AM.