-6

i want to active particular cell using vba while opening the excel.

Can anyone guide me how to do it.

Thanks

        Private Sub Send_Click()
        Dim strURL As String
      strURL = "http://xxxxxxxxxx.com/excelAPI.php?customer_id=1&mobilenumber=" _
        & ActiveCell.Value & "&message=" & ActiveCell.Offset(0, 1).Value
      Call Sheets("Sheet1").WebBrowser4.Navigate(strURL)

     Application.DisplayAlerts = False

      End Sub

       Private Sub Workbook_Open()
       Worksheets("Sheet1").Range ("A1")
        End Sub
Community
  • 1
  • 1
arok
  • 107
  • 2
  • 5
  • 13
  • Your question does not make any sense. Once you close the workbook there is not question of cell being active or deactive. If you wanna a particular cell to be active when you open the workbook then use workbook_open event. – Santosh May 05 '13 at 12:32
  • ok can you tell me how to activate the particular cell when opening excel .i tried its not working .i pasted my code – arok May 05 '13 at 12:43
  • 1
    Its already answered here http://stackoverflow.com/questions/16382801/how-to-keep-the-first-cell-always-active-in-excel-using-vba – Santosh May 05 '13 at 12:44
  • when i press the button the action will perform then i close the excel it asking for save or not when i click save it save .then when i open the excel sheet it will perform the action without clicking the button .can you tell me how to stop this – arok May 05 '13 at 12:47
  • why did you take off this line `Range("A1").Activate` ? Tell me what you dont understand in the code ? – Santosh May 05 '13 at 12:49
  • when i press the button the action will perform then i close the excel it asking for save or not when i click save it save .then when i open the excel sheet it will perform the action without clicking the button .can you tell me how to stop this.i am new to vba – arok May 05 '13 at 12:50
  • did you understand what i am try to say – arok May 05 '13 at 12:51
  • when you open the workbook only `Private Sub Workbook_Open() Worksheets("YourWorksheet").Activate Range("A1").Activate End Sub` part of code will run. It means when the workbook is opened it will activate the fist cell for you as per your requirement. – Santosh May 05 '13 at 12:53
  • your `Send_Click()` will not be called when you open the workbook. – Santosh May 05 '13 at 12:54
  • this is my new code but it automatically sent message when it open Private Sub Workbook_Open() Worksheets("Sheet1").Activate Range("A1").Activate End Sub Private Sub Send_Click() Dim strURL As String strURL = "http://xxxxxxxxxxx.com/excelAPI.php?customer_id=1&mobilenumber=" _ & ActiveCell.Value & "&message=" & ActiveCell.Offset(0, 1).Value Call Sheets("Sheet1").WebBrowser4.Navigate(strURL) End Sub – arok May 05 '13 at 12:58
  • Can you upload your file on a drive and get me the link so that i understand what exactly is your issue. – Santosh May 05 '13 at 13:03
  • thia is all my code when your select the cell and click the button it will send sms its working fine.after that i close the excel and open it again message send automatically send to the number it sends before do you how to stop that – arok May 05 '13 at 13:12
  • `@Send_Click()` will not be called automatically when you open the workbook unless there is any magic. – Santosh May 05 '13 at 13:20
  • no it performing the previous action ,that means the last action before closing the excel that same action perform automatically while opening the excel – arok May 05 '13 at 13:24
  • http://stackoverflow.com/questions/16382801/how-to-keep-the-first-cell-always-active-in-excel-using-vba – glh May 06 '13 at 06:50
  • it performing the previous action ,that means the last action before closing the excel.same action perform automatically while opening the excel – arok May 06 '13 at 06:58

2 Answers2

1

I'm not sure what you are trying to do, but if you want Send_Click to run when the workbook is opened, you just call it in the Workbook_Open event:

Private Sub Workbook_Open()
    Worksheets("Sheet1").Range("A1").Select
    Send_Click
End Sub

Please note that Workbook_Open needs to be in the ThisWorkbook module, but Send_Click can be in a different module.

You should also think about referencing the cell you want directly instead of selecting a cell and then using ActiveCell. It will be a lot faster and a lot easier to avoid mistakes. See this Q+A.

Community
  • 1
  • 1
Olle Sjögren
  • 5,315
  • 3
  • 31
  • 51
0

To activate a cell you need to select it first. Also if the sheet isn't active this also needs activating.

Worksheets("Sheet1").Activate 
Worksheets("Sheet1").Range("A1").Select 

Can I suggest if the data in cell A1 is what you are after in the send_click procedure you reference it directly like so:

Private Sub Send_Click()    
    Dim strURL As String

    With Worksheets("Sheet1").Range("A1").
        strURL = "http://xxxxxxxxxx.com/excelAPI.php?customer_id=1&mobilenumber=" _
        & .Value & "&message=" & .Offset(0, 1).Value
    end with

    Call Sheets("Sheet1").WebBrowser4.Navigate(strURL)        
    Application.DisplayAlerts = False    
End Sub
glh
  • 4,900
  • 3
  • 23
  • 40
  • can you tell me how to deactivate the previous action when opening the excel – arok May 06 '13 at 06:59
  • the only way to do this is to save in memory what was selected previously and re-select that. De-activate doesn't exist just activating another. – glh May 06 '13 at 07:14
  • im not sure I understand what you mean by deactivate. – glh May 06 '13 at 07:16
  • 1
    wouldn't it be better to tell excel what cell to get its information from in the `send_click` procedure? It seems this is the only place your using the information from `A1`. – glh May 06 '13 at 07:18
  • i am new to vba this my first program in excel vba.i asked question here http://stackoverflow.com/questions/16393929/how-to-deactive-the-previous-action-in-vba-while-opening-the-excel – arok May 06 '13 at 07:22
  • what other code is in there? – glh May 06 '13 at 07:30