Hello,
I have two workbooks: 1) called "Source" and 2) "Trend". I want to put a command button in Trend that copies data from Source and pastes it into Trend.
The workbook Source is always open on a computer downstairs. Users record data on it throughout the day.
The workbook Trend is on my computer and below is the code I have written so far. Basically there is a column in the Source workbook where the user must select from 3 options "Vertical", "Angle", or "n/a". I try to use those 3 selections to group the data into the Trend workbook (using the If-Then-ElseIf statement).
There are no errors. The Source workbook opens and asks me if I want to save. I assume that means the code looped all the way through the For-Next loop and got to the end where the last line of code before End Sub is Workbooks("Source.xlsm").Close. However I don't see the data in the Trend workbook. I think the problem might be that I'm not Activating the Trend workbook and "Sheet1" in the trend workbook correctly with the line of code I use:
I assumed that "ThisWorkbook.ActiveSheet" would be the workbook and worksheet which the command button is on (aka "Sheet1" in the Trend workbook) but maybe I'm wrong?
I'm not very experienced with VBA and the code I have above was mostly taken from examples out there on the web. Now I need some help with the details I think.
Thank you. I look forward to hearing your responses.
-Nick
I have two workbooks: 1) called "Source" and 2) "Trend". I want to put a command button in Trend that copies data from Source and pastes it into Trend.
The workbook Source is always open on a computer downstairs. Users record data on it throughout the day.
The workbook Trend is on my computer and below is the code I have written so far. Basically there is a column in the Source workbook where the user must select from 3 options "Vertical", "Angle", or "n/a". I try to use those 3 selections to group the data into the Trend workbook (using the If-Then-ElseIf statement).
Code:
Private Sub CommandButton1_Click()
Dim LastRow As Integer, i As Integer, eRow As Integer
Workbooks.Open Filename:="File Path\Source.xlsm"
LastRow = Worksheets("ACP").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 5) = "Vertical" Then
Range(Cells(i, 1)).Select
Range(Cells(1, 6)).Select
Range(Cells(i, 11), Cells(i, 12)).Select
Selection.Copy
eRow = ThisWorkbook.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(eRow, 2).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ElseIf Cells(i, 5) = "Angle" Then
Range(Cells(i, 1)).Select
Range(Cells(1, 6)).Select
Range(Cells(i, 11), Cells(i, 12)).Select
Selection.Copy
eRow = ThisWorkbook.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(eRow, 2).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ElseIf Cells(i, 5) = "n/a" Then
Range(Cells(i, 1)).Select
Range(Cells(1, 6)).Select
Range(Cells(i, 11), Cells(i, 12)).Select
Selection.Copy
eRow = ThisWorkbook.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(eRow, 2).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next i
Workbooks("Source.xlsm").Close
End Sub
There are no errors. The Source workbook opens and asks me if I want to save. I assume that means the code looped all the way through the For-Next loop and got to the end where the last line of code before End Sub is Workbooks("Source.xlsm").Close. However I don't see the data in the Trend workbook. I think the problem might be that I'm not Activating the Trend workbook and "Sheet1" in the trend workbook correctly with the line of code I use:
Code:
eRow = ThisWorkbook.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
I assumed that "ThisWorkbook.ActiveSheet" would be the workbook and worksheet which the command button is on (aka "Sheet1" in the Trend workbook) but maybe I'm wrong?
I'm not very experienced with VBA and the code I have above was mostly taken from examples out there on the web. Now I need some help with the details I think.
Thank you. I look forward to hearing your responses.
-Nick