modifying code to get last value based on sheet name from file to another

Mussala

Board Regular
Joined
Sep 28, 2022
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
I need to adapt this code by add sheet name in D4 . the stock file will be many sheets and the last value will be in column H for all of sheets. if I write the sheet name in D4 then should get last value from column H based on matching with sheet name.
VBA Code:
Sub populate_last_value()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim sPath As String
  
  sPath = "D:\Users\MUSSS\Desktop\REPORT\"
  
  Set wb1 = Workbooks.Open(sPath & "OUTPUT.xlsm")
  Set wb2 = Workbooks.Open(sPath & "STOCK.xlsm")
  
  wb1.Sheets("SUMMARY").Range("B4").Value = wb2.Sheets("SH").Range("H" & Rows.Count).End(3).Value

  wb2.Close TRUE
End Sub
thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet where you will enter the sheet name in D4 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter the sheet name in D4 and press the RETURN key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("D4")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim wb1 As Workbook, wb2 As Workbook, sPath As String
    sPath = "D:\Users\MUSSS\Desktop\REPORT\"
    Set wb1 = Workbooks.Open(sPath & "OUTPUT.xlsm")
    Set wb2 = Workbooks.Open(sPath & "STOCK.xlsm")
    wb1.Sheets("SUMMARY").Range("B4").Value = wb2.Sheets(Target.Value).Range("H" & Rows.Count).End(3).Value
    wb2.Close True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello,
Copy and paste this macro into the worksheet code module.
can you do in standard module ,please?
also I want fixing error subscript out of range if the D4 is empty
thanks
 
Upvote 0
Try:
VBA Code:
Sub CopyValue()
    Application.ScreenUpdating = False
    Dim WB As Workbook, wb1 As Workbook, wb2 As Workbook, sPath As String
    sPath = "D:\Users\MUSSS\Desktop\REPORT\"
    Set WB = ThisWorkbook
    Set wb1 = Workbooks.Open(sPath & "OUTPUT.xlsm")
    Set wb2 = Workbooks.Open(sPath & "STOCK.xlsm")
    wb1.Sheets("SUMMARY").Range("B4").Value = wb2.Sheets(WB.Range("D4").Value).Range("H" & Rows.Count).End(3).Value
    wb2.Close True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This line of code closes the file so I'm not sure why it stays open.
VBA Code:
wb2.Close True
this reason makes me I surprised despite of you indicate the line should close , it's really strange !:rolleyes:
any way thank you so much for your help .:)
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top