• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA code in different sheets

ammo4814

New Member
hi
I have workbook include 3 sheets ( sheet 1 ' sheet 2 ' sheet 3 ) . I create vb code in one sheet ( sheet 1 ) when I try to implement this code in sheet 2 and I I dosent work while it is work fine in sheet 1 . What is the reason and how I can solve this problem .

Thanks for help
 
Hi:

I am not sure what you are doing with the macro, unless and until I see the coding I can give you only a general suggestion , try adding macro in a module and run it.

Thanks
 
Hi ,

below the code i use :

Code:
Sub try()
Dim a As Integer, i As Integer
a = Range("a1").Value

For i = 1 To 2
Do While Range("a1").Value < 40
  Application.Wait (Now() + TimeValue("00:00:01"))
  Range("a1") = Range("a1").Value + 1
Loop
Application.Wait (Now() + TimeValue("00:00:01"))
Range("a1") = a
Next i

End Sub
 
Last edited by a moderator:
@ ammo4814

Pls always follow the forum rules to tag the code!!
I did for now.

I failed to understand that why u created the said macro!!

Here's u are using loop,wait function & after at all u are replacing the Range("A1") to the same.

Will u pls explain "What's ur requirement actually!!"
 
hi ,
the idea when i define this vb code on sheet 1 and try to implement on sheet 2 in the same workbook it dosnot work i want to how can make the vb code on all sheets in the workbook
 
hi ,
the idea when i define this vb code on sheet 1 and try to implement on sheet 2 in the same workbook it dosnot work i want to how can make the vb code on all sheets in the workbook

It's still unclear what u are looking to accomplish with the code.
 
actully i give code as an examole the idea i want to know how i can implement vb code in all sheet not in specific sheet only .
 
Your Code is working fine with all the Sheets only if you active that Sheet and run the code but it will run two times as For Loop is for 1 to 2.

if i understand correctly, you want to run the code only once on all the sheets. if the same is your requirement then below code will Help.

Code:
Sub try()
Dim a As Integer, ws As Worksheet


For Each ws In ThisWorkbook.Worksheets
ws.Activate
a = ws.Range("a1").Value
Do While ws.Range("a1").Value < 40
  Application.Wait (Now() + TimeValue("00:00:01"))
  ws.Range("a1") = ws.Range("a1").Value + 1
Loop
Application.Wait (Now() + TimeValue("00:00:01"))
ws.Range("a1") = a
Next ws

End Sub

this code will go through all the sheets in workbook at once.
 
Last edited:
Dear ,

Thanks for your help , just i want to explain my idea .
if i have two sheets (chart sheet , Data sheet ) when i define the code i define a1 in the code so when i assign macro in sheet 1 (chart sheet) it read the code from a1 in the chart sheet not from the data sheet so the macro not give me the expected results .

Thanks
 
Dear ,

Thanks for your help , just i want to explain my idea .
if i have two sheets (chart sheet , Data sheet ) when i define the code i define a1 in the code so when i assign macro in sheet 1 (chart sheet) it read the code from a1 in the chart sheet not from the data sheet so the macro not give me the expected results .

Thanks

You are repwtlly posting the same thing irrespective of any sample workbook with manual calculation of desired output.

Pls take pain to upload the same else i will mark the thread as spam.
 
The idea I want the chart be animated like chart in sheet "data" . Once click on the "on" button the scroll will move and the chart be animated .
The problem I have the code work ok in sheet "data" but when try to do it in other sheet not work like sheet "analysis"
 
Try the following code
Code:
Sub try()
Dim a As Integer, i As Integer
a = Sheet19.Range("a1").Value
With Sheet19
    For i = 1 To 2
        Do While .Range("a1").Value < 95
            Application.Wait (Now() + TimeValue("00:00:01"))
            .Range("a1") = .Range("a1").Value + 1
        Loop
    Application.Wait (Now() + TimeValue("00:00:01"))
    .Range("a1") = a
    Next i
End With
End Sub

Thanks
 
Thanks Dear ,

i have small question , in the vb code u wirte (sheet19) what it refere to .
if i want to implement this code in other example the sheet19 is the name of sheet since the name of sheet here is "Data" .

Thanks again for your clarification .
 
Hi You can change the name of the sheet in the code. The name Nebu Mentioned is the name of the sheet in the backend i.e. Sheet19. If you have the sheet in your excel workbook and you named that sheet as "Data", then change the code of the sheet from Sheet19 to "Data". you have to use double colen for it.

Regards,
JD
 
Hi !

Jagdev is wrong : Sheet19 in previous code is not a Name but a CodeName
The worksheet name could be "Sheet19" or anything else …

Better is not to work with worksheet name
if worksheet is in the code workbook but with its CodeName !

For example, when you open a new workbook, first worksheet is "Sheet1".
In VBE VBAProject property window you must see Sheet1 (Sheet1)
which means CodeName (Name).

These VBA statements refer all to this first worksheet :
Worksheets(1)
Worksheets("Sheet1")
Sheet1
So if this worksheet name is changed to "Data" and this worksheet is moved
after "Sheet2", first two above statements can't work anymore !
But its CodeName stays to Sheet1 ! Sheet1 (Data)

Reminder : CodeName is a ThisWorkbook only worksheet reference.

For a worksheet in another workbook,
must point out worksheet by Index or Name
 
Hi Marc ,

how i can know the worksheet codename , like my example i changed the name to "Data" how u know the sheet code is sheet19

Thanks all for ur clarification .
 
Back
Top