PenguinLog
New Member
- Joined
- Nov 1, 2022
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
I've searched on Google and through the Mr.Excel forums, but can't quite find what I'm after.
I have equipment that is stocked in my warehouse that we need to keep better track of and reduce some of the manual processes that have been in place. The first workbook (called "P2022") and worksheet (named "WH Stock") contains the records for the equipment (simplified):
The other workbook (called "Unit Inventory") and worksheet ("Inventory IN") also contains the same columns plus a few more that are relevant to that work sheet.
Once any information is entered into the "Customer" column for any row, I need to press a button that triggers two macros to:
1 -
Copy the row that contains any value in the "Customer" column in the current open workbook ("P2022") and worksheet ("WH Stock")
Open a closed workbook ("Unit Inventory")
Navigate to the correct worksheet ("Inventory IN")
Paste the row into this now open workbook/worksheet
Save the workbook
Close the workbook
Navigate back to the first workbook
2 -
Select the row with that contains any value in the "Customer" column
Hide this row
I pulled this code from the web for sheet-to-sheet copying, but I can't figure out how to add in the opening, pasting, saving, and closing of the second workbook or how to reference it:
Then the second macro is
I can get this to run perfectly fine within on workbook, but I don't know enough about coding in VBA to add the necessary lines to copy and paste to a closed workbook.
I have equipment that is stocked in my warehouse that we need to keep better track of and reduce some of the manual processes that have been in place. The first workbook (called "P2022") and worksheet (named "WH Stock") contains the records for the equipment (simplified):
Date | Model | Serial | Sold PO | Customer |
11/01/2022 | CBA38MV | 1522E67098 | 144555666 | Smith |
The other workbook (called "Unit Inventory") and worksheet ("Inventory IN") also contains the same columns plus a few more that are relevant to that work sheet.
Once any information is entered into the "Customer" column for any row, I need to press a button that triggers two macros to:
1 -
Copy the row that contains any value in the "Customer" column in the current open workbook ("P2022") and worksheet ("WH Stock")
Open a closed workbook ("Unit Inventory")
Navigate to the correct worksheet ("Inventory IN")
Paste the row into this now open workbook/worksheet
Save the workbook
Close the workbook
Navigate back to the first workbook
2 -
Select the row with that contains any value in the "Customer" column
Hide this row
I pulled this code from the web for sheet-to-sheet copying, but I can't figure out how to add in the opening, pasting, saving, and closing of the second workbook or how to reference it:
VBA Code:
Sub MoveStock()
'Copy rows with column P filled with a Y and paste to another worksheet
Dim xRg As Range
Dim xCell As Range
Dim I As Long
Dim J As Long
Dim K As Long
I = Worksheets("WH Stock").UsedRange.Rows.Count
'First workbook
J = Worksheets("Inventory IN").UsedRange.Rows.Count
'Second workbook
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Inventory IN").UsedRange) = 0 Then J = 0
End If
Set xRg = Worksheets("WH Stock").Range("P1:P" & I)
'Looking in the whole P column
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = "Y" Then
'I'd like to reference any value rather than a specific value. Previously this column was labeled as a Y/N, because I can't figure out how to add any value conditions.
xRg(K).EntireRow.Copy Destination:=Worksheets("Inventory IN").Range("A" & J + 1)
'Copy to the second workbook/worksheet
J = J + 1
End If
Next
Application.ScreenUpdating = True
End Sub
Then the second macro is
VBA Code:
Sub HideRows()
Dim cell As Range
For Each cell In ActiveWorkbook.ActiveSheet.Columns("P").Cells
If cell.Value = "Y" Then
'Again, the P column used to contain a Y/N title, which would tell the user to enter either a Y or N, which could be referenced for the function
cell.EntireRow.Hidden = True
End If
Next cell
End Sub
I can get this to run perfectly fine within on workbook, but I don't know enough about coding in VBA to add the necessary lines to copy and paste to a closed workbook.