Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50139

Replace Values on Workbook_Close / Open

$
0
0
Hi everyone

My operation team needs to fill out a form on a monthly basis which will then be uploaded to a system.

The problem is that the cols need to be in a particular order and the first field needs a particular code (unkown to the ops guys)

So, what I am trying to achieve is, that when the workbook is open it should display "ABC" or "XYZ" in col A.
But when the workbook is closed these values should change to "123" (instead of "ABC") or "789" (instead of "XYZ")

The tricky part is, that when the workbook is opened it should again show the values relevant to the op. team (i.e. "ABC" or "XYZ")

I have a separate tab (LookUps) where both values are defined.
Each value is unique, so "ABC" is always "123" and "XYZ" is always "789" (and vice versa)

I am pretty new to VBA, but so far I got

Code:


Private Sub Workbook_Open(ByVal sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
 Application.StatusBar = False
  Dim objSh As Worksheet
  Dim Rng As Range
  Dim vntRet As Variant
  Application.ScreenUpdating = False
  If Not Intersect(Target, sh.Range("A1:A5000")) Is Nothing Then
    Set objSh = Sheets("LookUps")
    For Each Rng In Intersect(Target, sh.Range("A1:A5000"))
      If Rng = "" Then
        Range(Cells(Rng.Row, 2), Cells(Rng.Row, 2)).Value = xlNone
      Else
        vntRet = Application.Match(Rng, objSh.Columns(2), 0)
        If IsNumeric(vntRet) Then
          Range(Cells(Rng.Row, 2), Cells(Rng.Row, 2)).Value = _
            objSh.Cells(vntRet, 1)

        Else
   
        End If
      End If
    Next
  End If
 
  Set Rng = Nothing
  Set objSh = Nothing

  Application.ScreenUpdating = True
  Application.StatusBar = True
End Sub
End Sub
___________________________________________________________________

Private Sub Workbook_Close(ByVal sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
 Application.StatusBar = False
  Dim objSh As Worksheet
  Dim Rng As Range
  Dim vntRet As Variant
  Application.ScreenUpdating = False
  If Not Intersect(Target, sh.Range("A1:B5000")) Is Nothing Then
    Set objSh = Sheets("LookUps")
    For Each Rng In Intersect(Target, sh.Range("A1:B5000"))
      If Rng = "" Then
        Range(Cells(Rng.Row, 2), Cells(Rng.Row, 2)).Value = xlNone
      Else
        vntRet = Application.Match(Rng, objSh.Columns(1), 0)
        If IsNumeric(vntRet) Then
          Range(Cells(Rng.Row, 2), Cells(Rng.Row, 2)).Value = _
            objSh.Cells(vntRet, 3)

        Else
   
        End If
      End If
    Next
  End If
 
  Set Rng = Nothing
  Set objSh = Nothing

  Application.ScreenUpdating = True
  Application.StatusBar = True
End Sub

But VBA does not seem to allow "(ByVal sh As Object, ByVal Target As Range)" for Workbook_Open/Close command.

Could someone help me out, please?

I tried the code on a "Workbook_SheetChange" where it works without any issues.

Thanks

FD

Viewing all articles
Browse latest Browse all 50139

Trending Articles