als nog niet open, map openen met pad in cel

Status
Niet open voor verdere reacties.

marc651

Gebruiker
Lid geworden
2 dec 2012
Berichten
175
Hallo,

Ik heb de volgende code, die prima werkt.

Code:
Private Sub CommandButton1_Click()
   Dim a As String
    Application.ScreenUpdating = False
    a = Range("G1").Value
    jv = Sheets(3).Range("E6:G127")
    With Workbooks.Open(a)
    Set jv2 = Sheets(2).Range("E6:I127")
    For i = 3 To UBound(jv)
    x = Application.Match(jv(i, 1), jv2.Columns(1), 0)
    y = Application.Match(jv(i, 3), jv2.Rows(1), 0)
    If IsNumeric(c) And IsNumeric(y) Then jv2.Cells(x, y) = jv2.Cells(x, y) & jv(1, 3) & "|"
    Next
    End With
    Application.ScreenUpdating = True
End Sub

Ik krijg echter een melding als de map al open is.
Is dat aan te passen in de code zodat hij kijkt of de map al open is, zo niet dan opent hij hem.
 
Is het bestand dan door jezelf of iemand anders geopend?
En wat is de foutcode en melding?
 
Laatst bewerkt:
Met een extra functie kun je controleren of je het bestand geopend hebt of niet. Kun je gelijk je With..End with gebruiken waarvoor het bedoeld is :)
Code:
Function IsWorkBookOpen(Name As String) As Boolean
Dim xWb As Workbook
    On Error Resume Next
    Set xWb = Application.Workbooks.Item(Name)
    IsWorkBookOpen = (Not xWb Is Nothing)
End Function

Code:
Private Sub CommandButton1_Click()
Dim a As String
Dim wb As Workbook
    Application.ScreenUpdating = False
    a = Range("G1").Value
    If IsWorkBookOpen(a) = False Then
        Set wb = Workbooks.Open(a)
    Else
        Set wb = Workbooks(a)
    End If
    With wb
        jv = .Sheets(3).Range("E6:G127")
        Set jv2 = .Sheets(2).Range("E6:I127")
        For i = 3 To UBound(jv)
            x = Application.Match(jv(i, 1), jv2.Columns(1), 0)
            y = Application.Match(jv(i, 3), jv2.Rows(1), 0)
            If IsNumeric(c) And IsNumeric(y) Then jv2.Cells(x, y) = jv2.Cells(x, y) & jv(1, 3) & "|"
        Next
    End With
    Application.ScreenUpdating = True

End Sub
 
Mij ljkt het gebruik van getobject eenvoudiger.
 
Waar komt de "c" vandaan?

Code:
If IsNumeric([COLOR="#FF0000"]c[/COLOR])
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan