Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default select different workbook in vba?

i would like this code to point to workbook "test1" instead of ActiveWorkbook.

does this code need rewriten? everything i have tried has not worked. i am
very much a novice at vba. this code works well if the information remains in
the ActiveWorkbook, but for security it needs to be moved.

Private Sub OldPriceLookup()

With ActiveSheet

If .Range("B17").Value = "n/a" Then

' clear out the old price
.Range(sOldPriceCol).Value = ""

ElseIf .Range("B17").Value = "A02" Then

' A02 = 29
' set the old price (only need to change the column Number "29")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 29, False)

ElseIf .Range("B17").Value = "A03" Then

' A03 = 30
' set the old price (only need to change the column Number "30")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 30, False)

it does have an end, just not shown.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,942
Default select different workbook in vba?

hi
try entering this line at the start of your code..
Workbooks("test1.xls").activate
then at the end of your code it might be a good idea to go back to the
workbook that has the code.

Regards
FSt1

"pleasehelp" wrote:

i would like this code to point to workbook "test1" instead of ActiveWorkbook.

does this code need rewriten? everything i have tried has not worked. i am
very much a novice at vba. this code works well if the information remains in
the ActiveWorkbook, but for security it needs to be moved.

Private Sub OldPriceLookup()

With ActiveSheet

If .Range("B17").Value = "n/a" Then

' clear out the old price
.Range(sOldPriceCol).Value = ""

ElseIf .Range("B17").Value = "A02" Then

' A02 = 29
' set the old price (only need to change the column Number "29")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 29, False)

ElseIf .Range("B17").Value = "A03" Then

' A03 = 30
' set the old price (only need to change the column Number "30")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 30, False)

it does have an end, just not shown.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default select different workbook in vba?

First, watch your typing.

The range("B19") in this line may not be on the sheet you want.
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,


I'm guessing that you wanted the B19 on the activesheet. If that's true, then
qualify that Range() with a leading dot:

.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(.Range("B19").Value,


That means this belongs to the object in the previous "with" statement--in this
case that's the activesheet.

And you may find that using a limited range (not all the rows and columns) makes
things work a bit nicer...

Option Explicit
Private Sub OldPriceLookup()

Dim wkbk As Workbook
Dim LookUpRng As Range

'include the extension if the workbook has been saved
'and it has to be already open, too
Set wkbk = Workbooks("test1.xls")

With wkbk.workSheets("casing")
'why use all the columns if you're only retrieving
'from column 30
Set LookUpRng = .Range("A1").Resize(1, 30).EntireColumn
End With

With ActiveSheet
If .Range("B17").Value = "n/a" Then
' clear out the old price
.Range(sOldPriceCol).Value = ""
ElseIf .Range("B17").Value = "A02" Then
' A02 = 29
' set the old price (only need to change the column Number "29")
.Range(sOldPriceCol).Value _
= Application.VLookup(.Range("B19").Value, _
LookUpRng, 29, False)
ElseIf .Range("B17").Value = "A03" Then
' A03 = 30
' set the old price (only need to change the column Number "30")
.Range(sOldPriceCol).Value _
= Application.VLookup(.Range("B19").Value, _
LookUpRng, 30, False)

===============
It kind of looks like you could check the last two characters and use that as
the column into that lookup range.

If that's true...



Option Explicit
Private Sub OldPriceLookup()

Dim wks As Worksheet
Dim LookUpRng As Range
Dim WhichCol As Long
Dim res As Variant
Dim myVal As Variant

'I really want to refer to the worksheet--not the workbook.
Set wks = Workbooks("test1.xls").Worksheets("Casing")

With wks
'just a single column now
Set LookUpRng = .Range("A1").EntireColumn
End With

With ActiveSheet
If .Range("B17").Value = "n/a" Then
' clear out the old price
.Range(soldpricecol).Value = ""
ElseIf .Range("b17").Value Like "A##" Then
WhichCol = CLng(Right(.Range("B17").Value, 2))
If WhichCol .Columns.Count Then
'too far out!
Beep 'and a msgbox???
myVal = "Invalid Number" 'or "" to clear the cell???
Else
res = Application.Match(.Range("b19").Value, LookUpRng, 0)
If IsError(res) Then
myVal = "Not found!"
Else
myVal = wks.Columns(WhichCol).Cells(1).Offset(res - 1)
End If
End if
.Range(soldpricecol).Value = myVal
End If
End With
End Sub


pleasehelp wrote:

i would like this code to point to workbook "test1" instead of ActiveWorkbook.

does this code need rewriten? everything i have tried has not worked. i am
very much a novice at vba. this code works well if the information remains in
the ActiveWorkbook, but for security it needs to be moved.

Private Sub OldPriceLookup()

With ActiveSheet

If .Range("B17").Value = "n/a" Then

' clear out the old price
.Range(sOldPriceCol).Value = ""

ElseIf .Range("B17").Value = "A02" Then

' A02 = 29
' set the old price (only need to change the column Number "29")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 29, False)

ElseIf .Range("B17").Value = "A03" Then

' A03 = 30
' set the old price (only need to change the column Number "30")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 30, False)

it does have an end, just not shown.


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default select different workbook in vba?

If you can determine the column to bring back based on the value in that cell,
then make a change.

WhichCol = CLng(Right(.Range("B17").Value, 2))
becomes
WhichCol = CLng(Right(.Range("B17").Value, 2)) + 27

(I forgot to add that 27 so that 2--29, 3--30, ....)

pleasehelp wrote:

i would like this code to point to workbook "test1" instead of ActiveWorkbook.

does this code need rewriten? everything i have tried has not worked. i am
very much a novice at vba. this code works well if the information remains in
the ActiveWorkbook, but for security it needs to be moved.

Private Sub OldPriceLookup()

With ActiveSheet

If .Range("B17").Value = "n/a" Then

' clear out the old price
.Range(sOldPriceCol).Value = ""

ElseIf .Range("B17").Value = "A02" Then

' A02 = 29
' set the old price (only need to change the column Number "29")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 29, False)

ElseIf .Range("B17").Value = "A03" Then

' A03 = 30
' set the old price (only need to change the column Number "30")
.Range(sOldPriceCol).Value =
WorksheetFunction.VLookup(Range("B19").Value,
ActiveWorkbook.Sheets("casing").Range("1:65536"), 30, False)

it does have an end, just not shown.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select sheet tabs in workbook & save to separate workbook files stratocaster Excel Worksheet Functions 2 March 1st 06 03:35 PM
workbook select Nigel Excel Discussion (Misc queries) 3 May 17th 05 08:45 PM
select workbook Tom Excel Programming 6 July 9th 04 02:14 PM
Sheets select method fails when workbook is opened by another workbook Mike Excel Programming 2 June 8th 04 04:17 AM
Select other workbook to select data in a macro. T Tromp Excel Programming 2 September 19th 03 01:43 PM


All times are GMT +1. The time now is 04:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"