Copy the result of the operation in the column of the second sheet

Morrezagh

New Member
Joined
Jun 3, 2021
Messages
6
Office Version
  1. 2007
Platform
  1. Mobile
Hi
How to write this command in vba?:
If cell B1 in sheet1 was greater than 50, then cell A2 copy in c1 in sheet2
If cell B1 in sheet1 was greater than 50, then cell A2 copy in c2 in sheet2
If cell B1 in sheet1 was greater than 50, then cell A2 copy in c3 in sheet2
...........................
...........................
If cell B1 in sheet1 was greater than 50, then cell A2 copy in c(i) in sheet2
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Are you sure that you have worded that correctly?

It appears that you are saying if cell B1 on Sheet1 is greater than 50, then put the value of A2 in column C on Sheet2, from row 1 down to whatever the value of "i" is.
 
Upvote 0
Are you sure that you have worded that correctly?

It appears that you are saying if cell B1 on Sheet1 is greater than 50, then put the value of A2 in column C on Sheet2, from row 1 down to whatever the value of "i" is.


Dim i As Long

Const N As Long = 10000'<- Test with a low number then change to whatever is required

For i = 1 To N
Range("F7").Value = i
Dim wb As Workbook, sht As Worksheet
Set wb = ThisWorkbook: Set sht = wb.Sheets("sheet1")
If sht.Range("B1") > 50 Then
cell A2 copy in c1 in sheet2 By changing i (a number between 1 and 10000), the content of cell A2 will also change, so that cell A2 is copied to column C of sheet 2.

End If


DoEvents
Next i
End Sub
 
Upvote 0
Try this:
VBA Code:
Dim i As Long
Dim wb As Workbook, sht As Worksheet

Const N As Long = 10000 '<- Test with a low number then change to whatever is required

Set wb = ThisWorkbook: Set sht = wb.Sheets("sheet1")

For i = 1 To N
    Range("F7").Value = i
    If sht.Range("B1") > 50 Then
        Sheets("Sheet2").Range("C1").Value = sht.Range("A2").Value
    End If

    DoEvents
Next i

End Sub
Note that you should move the wb as ws variable declarations outside of your loop.
There is no need to re-declare and reset them for each loop, since they are not changing.
Just set them before you start the loop.
 
Upvote 0
Try this:
VBA Code:
Dim i As Long
Dim wb As Workbook, sht As Worksheet

Const N As Long = 10000 '<- Test with a low number then change to whatever is required

Set wb = ThisWorkbook: Set sht = wb.Sheets("sheet1")

For i = 1 To N
    Range("F7").Value = i
    If sht.Range("B1") > 50 Then
        Sheets("Sheet2").Range("C1").Value = sht.Range("A2").Value
    End If

    DoEvents
Next i

End Sub
Note that you should move the wb as ws variable declarations outside of your loop.
There is no need to re-declare and reset them for each loop, since they are not changing.
Just set them before you start the loop.
Thank you for the answer
But I did not get the desired answer
Maybe I asked the wrong question
Simply put, by changing the numbers from 1 to 10,000, if B1 is greater than 50, the value of cell A2 may be, for example, the following:
Ali
Morris
Mary
......

All of the above should be placed in column C of sheet 2
 
Upvote 0
Sorry, I have been out of town all week.
Simply put, by changing the numbers from 1 to 10,000, if B1 is greater than 50, the value of cell A2 may be, for example, the following:
The following what?

Yes, your question is very unclear. If you have trouble explaining it, why not show us an example instead?
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,143
Members
449,098
Latest member
Doanvanhieu

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