Copy VBA CommandButton to next sheet in Excel

hshaugen

New Member
Joined
Aug 26, 2019
Messages
1
New to VBA.

I have a 2 workbook Excel. Programmed successfully a CommandButton on sheet 2 ("PTID_Link_Log"). Now I want to move it to sheet 1 ("Assignment"). Now it only completes actions on sheet 1, nothing on sheet 2. How do I make the code work from sheet 1?

Code:
Private Sub CommandButton1_Click()
Dim PTID As Long
Dim LastRow As Long
Dim i As Long

Sheets("PTID_Link_Log").Activate
ActiveSheet.Range("A1:K3335").Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("D" & i).Value = "" Then
If Range("C" & i).Value <> "" Then
Range("D" & i).Value = Range("C" & i)
End If
End If
If Range("G" & i).Value = "" Then
If Range("F" & i).Value <> "" Then
Range("G" & i).Value = Range("F" & i)
End If
End If
If Range("J" & i).Value = "" Then
If Range("I" & i).Value <> "" Then
Range("J" & i).Value = Range("I" & i)
End If
End If
Next i

Sheets("Assignment").Activate
ActiveSheet.Range("A1:K2").Select
PTID = ActiveSheet.Range("A2").Value
If PTID <> 0 Then
ActiveSheet.Range("C2") = ""
ActiveSheet.Range("E2") = ""
ActiveSheet.Range("G2") = ""
ActiveSheet.Range("A2") = ""
End If
ActiveSheet.Range("A2").Select

End Sub

EndCode

Appreciate any guidance
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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