I wrote this program in VBA that opens another excel sheet that the user can enter the name of, and then the program is supposed to open the user specified sheet, find the needed data, and copy and paste it into the specified locations on the new sheet. The program opens the correct sheet, but does not copy any data to the new sheet and I can’t figure out why. Any help would be appreciated!
Sub Automated_Fill()
'
' Automated_Fill Macro
'
'
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' copy data from customer to target workbook
' Grocery Data
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheetGroSel As Worksheet
Set sourceSheetGroSel = customerWorkbook.Worksheets(2)
targetSheet.Range("C9").Value = sourceSheetGroSel.Range("J8").Value
targetSheet.Range("C17").Value = sourceSheetGroSel.Range("K8").Value
Dim sourceSheetGroLoad As Worksheet
Set sourceSheetGroLoad = customerWorkbook.Worksheets(3)
targetSheet.Range("C10").Value = sourceSheetGroLoad.Range("J5").Value
targetSheet.Range("C11").Value = 0
targetSheet.Range("C18").Value = sourceSheetGroLoad.Range("K5").Value
targetSheet.Range("C19").Value = 0
Dim sourceSheetGroLTO As Worksheet
Set sourceSheetGroLTO = customerWorkbook.Worksheets(4)
targetSheet.Range("C12").Value = sourceSheetGroLTO.Range("J12").Value - sourceSheetGroLTO.Range("J8").Value - sourceSheetGroLTO.Range("J9").Value
targetSheet.Range("C13").Value = 0
targetSheet.Range("C20").Value = sourceSheetGroLTO.Range("K12").Value
targetSheet.Range("C21").Value = 0
targetSheet.Range("G12").Value = sourceSheetGroLTO.Range("J8").Value + sourceSheetGroLTO.Range("J9").Value
Dim sourceSheetGroRec As Worksheet
Set sourceSheetGroRec = customerWorkbook.Worksheets(5)
targetSheet.Range("G13").Value = sourceSheetGroRec.Range("J5").Value
targetSheet.Range("G21").Value = sourceSheetGroRec.Range("K5").Value
' Perishable Data
Dim sourceSheetPerSel As Worksheet
Set sourceSheetPerSel = customerWorkbook.Worksheets(6)
targetSheet.Range("G99").Value = sourceSheetPerSel.Range("J10").Value
targetSheet.Range("G107").Value = sourceSheetPerSel.Range("K10").Value
Dim sourceSheetPerLoad As Worksheet
Set sourceSheetPerLoad = customerWorkbook.Worksheets(7)
targetSheet.Range("G100").Value = sourceSheetPerLoad.Range("J5").Value
targetSheet.Range("G108").Value = sourceSheetPerLoad.Range("J5").Value
Dim sourceSheetPerLTO As Worksheet
Set sourceSheetPerLTO = customerWorkbook.Worksheets(9)
targetSheet.Range("C102").Value = sourceSheetPerLTO.Range("J10").Value sourceSheetPerLTO.Range("J7").Value
targetSheet.Range("G102").Value = sourceSheetPerLTO.Range("J7").Value
targetSheet.Range("C110").Value = sourceSheetPerLTO.Range("K10").Value
Dim sourceSheetPerRec As Worksheet
Set sourceSheetPerRec = customerWorkbook.Worksheets(8)
targetSheet.Range("C103").Value = sourceSheetPerRec.Range("J5").Value
targetSheet.Range("C111").Value = sourceSheetPerRec.Range("K5").Value
' Freezer Data
Dim sourceSheetFrzSel As Worksheet
Set sourceSheetFrzSel = customerWorkbook.Worksheets(10)
targetSheet.Range("C144").Value = sourceSheetFrzSel.Range("J6").Value
targetSheet.Range("C152").Value = sourceSheetFrzSel.Range("K6").Value
Dim sourceSheetFrzLTO As Worksheet
Set sourceSheetFrzLTO = customerWorkbook.Worksheets(11)
targetSheet.Range("C147").Value = sourceSheetFrzLTO.Range("J5").Value
targetSheet.Range("G147").Value = sourceSheetFrzLTO.Range("J4").Value
targetSheet.Range("C155").Value = sourceSheetFrzLTO.Range("J5").Value
targetSheet.Range("G155").Value = sourceSheetFrzLTO.Range("J4").Value
Dim sourceSheetFrzRec As Worksheet
Set sourceSheetFrzRec = customerWorkbook.Worksheets(12)
targetSheet.Range("G148").Value = sourceSheetFrzRec.Range("J5").Value
targetSheet.Range("G156").Value = sourceSheetFrzRec.Range("K5").Value
' Close customer workbook
customerWorkbook.Close
End Sub
6
Try setting the targetWorkbook to thisWorkbook instead of ActiveWorkbook if this subroutine is in the targetworkbook. If this is some add-in that uses the current ActiveWorkbook and that is necessary, is the user always using worksheets(1) or will they possibly have multiple worksheets they’re copying data to, in which case, you could set the worksheet to targetWorkbook.ActiveSheet or you could always create a new sheet.
Set targetWorksheet = targetWorkbook.Worksheets.Add(after:=targetWorkbook.worksheets(targetWorkbook.Worksheets.Count))