ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VB-macro - Copy from another workook (https://www.excelbanter.com/excel-programming/342654-vbulletin-macro-copy-another-workook.html)

Johannes B.

VB-macro - Copy from another workook
 
I have this code to copy certain values from one sheet to another:

Private Sub worksheet_activate()
Dim rAogB As Range
Set rAogB = Worksheets("Sheet2").Range("aogb")
Dim skrivA As Range
Set skrivA = Range("A1")
Dim j As Integer
j = 1
For i = 1 To rAogB.Height
If rAogB(i, 2) = "a" Then
skrivA(j, 1) = rAogB(i, 1)
skrivA(j, 2) = rAogB(i, 2)
j = j + 1
End If
Next i
End Sub

But now my values are located in another workbook (and not only in another
worksheet). How do I refeer to this workbook?

Bob Phillips[_6_]

VB-macro - Copy from another workook
 
Private Sub worksheet_activate()
Dim rAogB As Range
Dim wb As Workbook

Set wb = Workbooks("wbname.xls")
Set rAogB = wb.Worksheets("Sheet2").Range("aogb")
Dim skrivA As Range
Set skrivA = Range("A1")
Dim j As Integer
j = 1
For i = 1 To rAogB.Height
If rAogB(i, 2) = "a" Then
skrivA(j, 1) = rAogB(i, 1)
skrivA(j, 2) = rAogB(i, 2)
j = j + 1
End If
Next i
End Sub


--
HTH

Bob Phillips

"Johannes B." wrote in message
...
I have this code to copy certain values from one sheet to another:

Private Sub worksheet_activate()
Dim rAogB As Range
Set rAogB = Worksheets("Sheet2").Range("aogb")
Dim skrivA As Range
Set skrivA = Range("A1")
Dim j As Integer
j = 1
For i = 1 To rAogB.Height
If rAogB(i, 2) = "a" Then
skrivA(j, 1) = rAogB(i, 1)
skrivA(j, 2) = rAogB(i, 2)
j = j + 1
End If
Next i
End Sub

But now my values are located in another workbook (and not only in another
worksheet). How do I refeer to this workbook?





All times are GMT +1. The time now is 02:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com