I have software for a scale that uploads and downloads data to it. The data can be transferred to an Excel spreadsheet, which I've been using to create Macros to pretty the information up for the end user. I've been adding data to access to try and make this process simple enough for the user to do themselves, but I have only been learning code via recorded macros in Excel, google, and what I remember from a class I took over a decade ago. I have a simple form that will (when I get it right) let the user change the animal number for every PLU in the spreadsheet. There is a frame with 5 option buttons (cow, hog, sheep, lamb, & goat), a text box for animal number, and a command button to change the Excel file. The command button executes the code below. I have tried many different variations and gotten many different errors, but I can't get it right. There is probably a good handful of broken things in this, so I apologize in advance. Currently, my error is "Compile Error: Method or data member not found". Any help would be super appreciated.
Private Sub cmdModify_Click()
Dim X As Integer
Dim strAnimalNumber As String
Dim PLUPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
'Get animal number from form text box
txtAnimalNumber.SetFocus
strAnimalNumber = txtAnimalNumber.Text
'Set path based on frame option value
If fraMeatType.Value = 1 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Cow.xlsx"
ElseIf fraMeatType.Value = 2 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Hog.xlsx"
ElseIf fraMeatType.Value = 3 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Lamb.xlsx"
ElseIf fraMeatType.Value = 4 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Sheep.xlsx"
ElseIf fraMeatType.Value = 5 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Goat.xlsx"
End If
'Open Excel and correct workbook
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(PLUPath)
'Show workbook
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets(1)
'Count rows
numrows = XlSheet.Range("A2", XlSheet.Range("A2").End(xlDown)).Rows.Count
'Focus needs to be here for loop start
XlSheet.Range("B2").Select
For X = 1 To numrows
XlSheet.ActiveCell.Text = strAnimalNumber 'Set animal number on PLU
XlSheet.ActiveCell.Offset(1, 0).Select 'Go to next cell down
Next
'Save workbook and quit Excel
Xl.ActiveWorkbook.Save
Xl.ActiveWorkbook.Close
Xl.Quit
End Sub
Private Sub cmdModify_Click()
Dim X As Integer
Dim strAnimalNumber As String
Dim PLUPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
'Get animal number from form text box
txtAnimalNumber.SetFocus
strAnimalNumber = txtAnimalNumber.Text
'Set path based on frame option value
If fraMeatType.Value = 1 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Cow.xlsx"
ElseIf fraMeatType.Value = 2 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Hog.xlsx"
ElseIf fraMeatType.Value = 3 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Lamb.xlsx"
ElseIf fraMeatType.Value = 4 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Sheep.xlsx"
ElseIf fraMeatType.Value = 5 Then
PLUPath = CurrentProject.Path & "\GSY\Access\Practice\Goat.xlsx"
End If
'Open Excel and correct workbook
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(PLUPath)
'Show workbook
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets(1)
'Count rows
numrows = XlSheet.Range("A2", XlSheet.Range("A2").End(xlDown)).Rows.Count
'Focus needs to be here for loop start
XlSheet.Range("B2").Select
For X = 1 To numrows
XlSheet.ActiveCell.Text = strAnimalNumber 'Set animal number on PLU
XlSheet.ActiveCell.Offset(1, 0).Select 'Go to next cell down
Next
'Save workbook and quit Excel
Xl.ActiveWorkbook.Save
Xl.ActiveWorkbook.Close
Xl.Quit
End Sub