If-and condition

stunik94

New Member
Joined
Sep 21, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to select a specific sheet to paste my information to based of the information in 2 cells.
if cell 1 is A and cell 2 is A the information should be pasted in sheet 1 of workbook 2
if cell 1 is a and cell 2 is B the information should be pasted in sheet 2 of workbook 2
it will become a bigger datasheet but for now i would like to get this working.

so far I've tried the following:

Sub Button1_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook

Set wb1 = ActiveWorkbook

Workbooks.Open ThisWorkbook.Path & "\" & "book2.xlsx"

Set wb2 = ActiveWorkbook

wb1.Activate
If Range("A1") = "A" And Range("B1") = "A" Then
wb2.Activate
Sheet1.Activate
Range("A4").Select
ActiveCell.Value = "sheet1"
ElseIf Range("A1") = "A" And Range("B1") = "B" Then
wb2.Activate
Sheet2.Activate
Range("A4").Select
ActiveCell.Value = "sheet2"
End If


End Sub

but this isn't working and I don't know why it doesn't...
am I using the And function wrong?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You cannot use the sheet codenames like that in a different workbook, you will need to use the actual sheet names.
 
Upvote 0
this was a test setup, the sheetnames are sheet1 and sheet2 for now
 
Upvote 0
Try it like
VBA Code:
 Sheets("Sheet1").Activate
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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