Hello,
I have opened a workbook and stored it in a variable ("MTFwb"). I am using .Sheets.Range to set the value of global variables from values in the opened workbook. This works great most of the time. But for some variables this only works when the workbook is open prior to running the macro. I have added the code, and highlighted the code that dont work properly. (I have left out the errorhandler, but have checked that no errors occour).
I have tested that matchRad2RNA and matchRad2DNA have the correct value. But the value for mtf_konsRNA, mtf_280RNA, mtf230RNA, mtf_konsDNA, mtf_280DNA and mtf_230DNA is always set to "0".
I find it weird that .Sheets.Range works perfectly fine just a few lines above where the problem occour. It is also confusing that the code works perfectly fine if the workbook I open as "MTFwb" is open prior to running the macro.
Anyone have any idea as to why this is the case?
Thank you so much in advance!
I have opened a workbook and stored it in a variable ("MTFwb"). I am using .Sheets.Range to set the value of global variables from values in the opened workbook. This works great most of the time. But for some variables this only works when the workbook is open prior to running the macro. I have added the code, and highlighted the code that dont work properly. (I have left out the errorhandler, but have checked that no errors occour).
I have tested that matchRad2RNA and matchRad2DNA have the correct value. But the value for mtf_konsRNA, mtf_280RNA, mtf230RNA, mtf_konsDNA, mtf_280DNA and mtf_230DNA is always set to "0".
I find it weird that .Sheets.Range works perfectly fine just a few lines above where the problem occour. It is also confusing that the code works perfectly fine if the workbook I open as "MTFwb" is open prior to running the macro.
Anyone have any idea as to why this is the case?
Thank you so much in advance!
VBA Code:
Function mtf_f()
Dim MTFwb As Workbook, bane As String, matchRadRNA As Variant, matchRadDNA As Variant, matchRad2RNA As Variant, matchRad2DNA As Variant
Set myWb = ThisWorkbook
On Error GoTo ErrorHandler:
' Use Application.Workbooks.Open to open the workbook in a hidden state
Application.ScreenUpdating = False
Application.DisplayAlerts = False
bane = myWb.Sheets(1).Range("A3")
Set MTFwb = Workbooks.Open("K:\Sensitivt\Klinikk\20-17947 UtprovendeDiagnostikk_OU\2. Patient\IPD-" & Right(bane, 4) & "\IPD-" & Right(bane, 4) & "_Material Transit Form InPreD NGS_2025.xlsx", ReadOnly:=True, UpdateLinks:=False)
'Set the worksheet and range to search
Set ws = MTFwb.Sheets("MTF")
Set rad = ws.Range("I:I")
Set rad2 = ws.Range("A:A")
'Finner IPD fra MTF
mtf_IPD = MTFwb.Sheets(1).Range("A10")
'Bruker workingID for RNA og DNA til å finne radene de ligger i
matchRadRNA = Application.Match(myWb.Sheets(1).Range("D3"), rad, 0)
matchRadDNA = Application.Match(myWb.Sheets(1).Range("E3"), rad, 0)
matchRad2RNA = Application.Match(myWb.Sheets(1).Range("D3"), rad2, 0)
matchRad2DNA = Application.Match(myWb.Sheets(1).Range("E3"), rad2, 0)
'Bruker radene funnet over til å sette workid og sampleID
If matchRadRNA <> 0 Then
mtf_wrkIDRNA = MTFwb.Sheets(1).Range("I" & matchRadRNA)
mtf_smplIDRNA = MTFwb.Sheets(1).Range("J" & matchRadRNA)
mtf_prepRNA = MTFwb.Sheets(1).Range("B" & matchRadRNA)
End If
If matchRadDNA <> 0 Then
mtf_wrkIDDNA = MTFwb.Sheets(1).Range("I" & matchRadDNA)
mtf_smplIDDNA = MTFwb.Sheets(1).Range("J" & matchRadDNA)
mtf_prepDNA = MTFwb.Sheets(1).Range("B" & matchRadDNA)
End If
'Bruker radene funnet tidligere til å sjekke om TC% er fylt inn for RNA og DNA, returnere sann eller usann
If MTFwb.Sheets(1).Range("C" & matchRadRNA) <> "" Then
If MTFwb.Sheets(1).Range("C" & matchRadDNA) <> "" Then
mtf_TC = True
Else: mtf_TC = False
End If
Else: mtf_TC = False
End If
'Bruker radene funnet til å returnere konsentrasjoner og renhet
[B] If matchRad2RNA <> 0 Then
mtf_konsRNA = MTFwb.Sheets(1).Range("E" & matchRad2RNA)
Debug.Print MTFwb.Sheets(1).Range("E47")
mtf_280RNA = MTFwb.Sheets(1).Range("F" & matchRad2RNA)
mtf_230RNA = MTFwb.Sheets(1).Range("G" & matchRad2RNA)
End If
If matchRad2DNA <> 0 Then
mtf_konsDNA = MTFwb.Sheets(1).Range("E" & matchRad2DNA)
mtf_280DNA = MTFwb.Sheets(1).Range("F" & matchRad2DNA)
mtf_230DNA = MTFwb.Sheets(1).Range("G" & matchRad2DNA)
End If[/B]
'Close the workbook
MTFwb.Close False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Function