Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2006

Tip: Looking for answers? Try searching our database.

pulling data from another sheet

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AD108 - 22 May 2006 07:44 GMT
I am in the very early stages of learning programming.  Any help would be
greatly appreciated.
I am attempting to pull data from cells on another sheet, by using matching
reference numbers.

Basically how I am attempting to do this is to loop through all the item
numbers on the sheet.
The first attempt is match a 12 digit UPC number, and if that doesn't return
a match on the data sheet, then it should check a second number, which
happens to be 6 digits.  I was able to get the first part to work, but then
when I tried to add the condition (When the 12 digit code is not found,
check the 6 digit one, my code is failing.

Code is as follows...

Sub ExtractData()

Dim rngSub As Range
Dim Cell As Range
Dim x As Range
Dim i As Integer
Dim y As String
On Error Resume Next
Application.ScreenUpdating = False

For i = 2 To ActiveWorkbook.Sheets.Count - 1
'makes loop skip data sheet

        Set rngSub =
Sheets(i).Range("N:N").SpecialCells(xlCellTypeConstants, 23)
'loop through all cells
        For Each Cell In rngSub
'add the dash as the data sheet has dashes in the numbers
           y = Left(Cell, 6) & "-" & Right(Cell, 6)
           Debug.Print y
           Set x = Sheets(1).Cells.Find(What:=y, LookAt:=xlPart)
           Cell.Offset(0, 4).Value = x.Offset(0, 17).Value
           Debug.Print x.Offset(0, 17).Value

'if 12 digit number does not match, attempt to find matching 5 digit number
                 If
Application.WorksheetFunction.CountBlank(Sheets(i).Cell.Offset(0, 4)) = 1
Then
                    y = Left(Cell.Offset(0, -11), 3) & "-" &
Right(Cell.Offset(0, -11), 3)
                    Debug.Print y
                    Set x = Sheets(1).Cells.Find _
                    (What:=y, LookAt:=xlWhole)
                    Cell.Offset(0, 4).Value = x.Offset(0, 15)

        Next Cell
MsgBox Sheets(i).Name
Application.ScreenUpdating = True
Next i

End Sub

Thanks in advance,

Ad108
Bob Phillips - 22 May 2006 09:09 GMT
Maybe try this (I haven't tested it, don't understand the data)

Sub ExtractData()

Dim rngSub As Range
Dim Cell As Range
Dim x As Range
Dim i As Integer
Dim y As String
On Error Resume Next
Application.ScreenUpdating = False

   For i = 2 To ActiveWorkbook.Sheets.Count - 1
   'makes loop skip data sheet

       Set rngSub =
Sheets(i).Range("N:N").SpecialCells(xlCellTypeConstants, 23)
       'loop through all cells
        For Each Cell In rngSub
           'add the dash as the data sheet has dashes in the numbers
           y = Left(Cell, 6) & "-" & Right(Cell, 6)
           Debug.Print y
           Set x = Sheets(1).Cells.Find(What:=y, LookAt:=xlPart)
           If Not x Is Nothing Then
               Cell.Offset(0, 4).Value = x.Offset(0, 17).Value
               Debug.Print x.Offset(0, 17).Value
           Else
               'if 12 digit number does not match,
               'attempt to find matching 5 digit number
               y = Left(Cell.Offset(0, -11), 3) & "-" &
Right(Cell.Offset(0, -11), 3)
               Debug.Print y
               Set x = Sheets(1).Cells.Find _
                    (What:=y, LookAt:=xlWhole)
               If Not x Is Nothing Then _
                   Cell.Offset(0, 4).Value = x.Offset(0, 15)
           End If
       Next Cell
       MsgBox Sheets(i).Name
       Application.ScreenUpdating = True
   Next i

End Sub

BTW, best not to use constants when there is a declared constant available
(Specialcells value).

Signature

HTH

Bob Phillips

(remove xxx from email address if mailing direct)

> I am in the very early stages of learning programming.  Any help would be
> greatly appreciated.
[quoted text clipped - 56 lines]
>
> Ad108
AD108 - 22 May 2006 12:33 GMT
Thanks a alot,

That solved the problem

> Maybe try this (I haven't tested it, don't understand the data)
>
[quoted text clipped - 114 lines]
> >
> > Ad108
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.