Hi, I am new to VBA and need some help with some of the procedures I would like to automate.
I have two excel files to work on - employee and contract data.
Basically, I need to find employees who end their contracts in the current month that I open the file. (For example, if I open the file in August, I need to find employee data who are ending their contract in August). Then, I need to copy the selected data and paste them onto another file, called contract data.
I have completed both identifying and copy+paste, but it doesn't meet some of the requirements I want to apply in my file. I will put my code below.
The code below highlights rows of employee data whose employment contract expires within a month.
The code below is when I transfer a selected row to another file called Contract Data
I have included some questions in the code.
To explain them further, when I copy the data from the Employee file to the Contract File, I do not want to copy the exact same data multiple times.
There is a column that I made for a unique identifier(column A), so I guess there might be a way to do this with it, but I am still clueless about how to do so, as I am not familiar with VBA yet.
I also do not know how to select the highlighted data to copy them to the Contract file.
Most importantly, when copying data, I only want some data to be copied, not all. (For example, I want to copy the name of employees and their positions, but not their addresses or phone number).
When pasting data, I want to paste them onto their allocated cell area.
Can anyone please help me with my VBA? I am new to this and need help ASAP
I have two excel files to work on - employee and contract data.
Basically, I need to find employees who end their contracts in the current month that I open the file. (For example, if I open the file in August, I need to find employee data who are ending their contract in August). Then, I need to copy the selected data and paste them onto another file, called contract data.
I have completed both identifying and copy+paste, but it doesn't meet some of the requirements I want to apply in my file. I will put my code below.
The code below highlights rows of employee data whose employment contract expires within a month.
VBA Code:
Sub HighlightCalc()
Dim i As Range
For Each i In Range("K2:K65565") '// K is the column that contains contract expiry date
If IsDate(i) Then
If Month(i) = Month(Now) Then
Rows(i.Row).Interior.Color = RGB(255,255,91)
Rows(i.Row).Font.Bold = True
End If
End If
Next i
End Sub
The code below is when I transfer a selected row to another file called Contract Data
VBA Code:
Private Sub CommandButton1_Click()
Dim wb As Workbook
ThisWorkbook.Worksheets("Sheet1").Rows(2).Select '//Question 1: How do I select rows that I have previously highlighted above?
Selection.Copy
Set wb = Workbooks.Open("file address, didn't put the real address")
wb.Worksheets("Sheet1").Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Select
ActiveSheet.Paste. '//Question 2: Is there any way that I can only copy "new" data and paste them? Every time I run this Macro, the same data is pasted.
'//Any suggestions on copying data to other files without duplicates?
'//Question 3: Can I only copy selected cells and paste them onto the selected places?
ActiveWorkbook.Save
ActiveWorkbook.Close savechanges = True
Set wb = Nothing
ThisWorkbook.Worksheets("Sheet1").Activate
ThisWorkbook.Worksheets("Sheet1").Cells(1,1).Select
Application.CutCopyMode = False
I have included some questions in the code.
To explain them further, when I copy the data from the Employee file to the Contract File, I do not want to copy the exact same data multiple times.
There is a column that I made for a unique identifier(column A), so I guess there might be a way to do this with it, but I am still clueless about how to do so, as I am not familiar with VBA yet.
I also do not know how to select the highlighted data to copy them to the Contract file.
Most importantly, when copying data, I only want some data to be copied, not all. (For example, I want to copy the name of employees and their positions, but not their addresses or phone number).
When pasting data, I want to paste them onto their allocated cell area.
Can anyone please help me with my VBA? I am new to this and need help ASAP