Copying Data over from one workbook to another issue

dreen

Board Regular
Joined
Nov 20, 2019
Messages
52
I have two workbooks for my code, one is a database (The "Changes" sheet in my example) and the other is an input sheet "(Codename "Sheet1" as my code is being written inside this workbook where Sheet1 is). I am trying to have the code check if the value of certain cells in column "K' of Sheet 1 are equal to something (<>""), and if they are, I am trying to filter the "Changes" sheet to filter out which row in column A has the value of cell "H4" from Sheet1, so that I can copy and paste the value of "K30" from Sheet1 (from the workbook where the code is being written) into cell "F6" [cells (1,6)] of the other workbook (the "Changes" sheet)

VBA Code:
'Module 8: Sends the requested changes over to the "Changes" sheet

Sub SendChanges()

Set Cd = Workbooks.Open("\\FILEPATH\Changes_Database_IRR_20-2S_New.xlsm")
Set Changes = Cd.Sheets("Changes")

Changes.Activate
ActiveSheet.Unprotect "Swrf"
Sheet1.Unprotect "Swrf"

'Only executes this macro if the the new/change requested value in column "K" of the Operator sheet (Codename Sheet1) has a numerical value present
If Sheet1.Range("K30").Value <> "" Then

'Filters the Changes_Database for the part name & process (the key) which is in cell "H4" of the Operator sheet (Codename Sheet1)
Changes.Range("A1").AutoFilter Field:=1, Criteria1:=Sheet1.Range("H4")
 
'Copies the changed content in cell "K30" from the Operator Sheet (Codename Sheet1)
Sheet1.Range("K30").Copy

'Finds the row in the Changes_Database ("Changes" sheet) that has matched all filters and;
'Pastes the value of cell "K30" (From Codename "Sheet1" or Operator sheet) into the matching parameter cell in the Changes_Database,which is in column 6 in this case
Changes.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 6).PasteSpecial xlPasteValues

'Removes all filters and shows all data'
Changes.ShowAllData

End If

ActiveSheet.Protect "Swrf"
            ActiveWorkbook.Save
            ActiveWorkbook.Close SaveChanges:=True
Sheet1.Protect "Swrf"

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Cells(1,6) is F1 not F6
Are you trying to put the value from K30 in F6? In which case what's the point of the autofilter?
Or are you trying to put the value of K30 into col F of the first visible row?
 
Upvote 0
Cells(1,6) is F1 not F6
Are you trying to put the value from K30 in F6? In which case what's the point of the autofilter?
Or are you trying to put the value of K30 into col F of the first visible row?

From my understanding, cells(1,6) is Row 1, Column 6. and yes exactly, I am trying to put the value of K30 into col F of the first visible row
 
Upvote 0
From my understanding, cells(1,6) is Row 1, Column 6
This is totally correct, which means F1, not F6 as you said (F6 is row 6, column 6)

That said, in what way is your code not working?
 
Upvote 0
I have heading in row 1, for example A1 has "Key" in it, which is where I am filtering "H4" from. Would that mean that I have to paste K30 it into F2 (row 2, column 6) as it will become visible underneath my headers? and my code is not pasting any values, both workbooks are just remaining opened (this is a module).
 
Upvote 0
You're code should put the value from K30 into the first visible row after the filter has been applied.

Step through your code using F8 until you get to this line
Code:
Changes.ShowAllData
then look at the sheet, do you have any visible rows, below the header?
If yes, does that first visible row contain the value of K30?
 
Upvote 0
I just made two test workbooks, and I put in same code into test 1, and here is what is happening.

VBA Code:
Private Sub CommandButton1_Click()
'Module 8: Sends the requested changes over to the "Mytest2" sheet

Set Cd = Workbooks.Open("\\schaeffler.com\stratford\DATA\NEA-FBA-P\Projects\SetupSheets\Databases\Test 2.xlsm")
Set Changes = Cd.Sheets("Mytest2")

Changes.Activate
ActiveSheet.Unprotect "Swarf"
Sheet1.Unprotect "Swarf"

'Only executes this macro if the the new/change requested value in column "K" of the Operator sheet (Codename "Sheet1") has a numerical value present
If Sheet1.Range("K2").Value <> "" Then

'Filters the Changes_Database for the part name & process (the key) which is in cell "H4" of the Operator sheet
Changes.Range("A1").AutoFilter Field:=1, Criteria1:=Sheet1.Range("H4")
 
'Copies the changed content in cell "K30" from the Operator Sheet
Sheet1.Range("K2").Copy

'Finds the row in the Changes_Database that has matched all filters and;
'Pastes the value of cell "K30" into the matching parameter cell in the Changes_Database,which is in column 6 in this case
Changes.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 6).PasteSpecial xlPasteValues

'Removes all filters and shows all data'
Changes.ShowAllData


Changes.Protect "Swarf"
            ActiveWorkbook.Save
            ActiveWorkbook.Close SaveChanges:=True

End If

End Sub

Test 1.JPG

Test 2.JPG
 
Upvote 0
Can you please answer my questions?
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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