0

Hy, I am using Microsoft Excel as an Integrated Google Map. It working fine for one row. Here is its code.

Private Sub CommandButton1_Click()
WebBrowser1.Navigate ActiveSheet.Range("D2").Value
End Sub

enter image description here

Basically what i want, I want when I click on the Next Map, the row 3 shold be populate in the map. It works fine for one record at a time and only for D2. But I want when I move "Next Map" button it should work for D3 and other as like for D2. I am using below code but it does not work.

    Sub SelectC()
Dim x As Variant
x = Cells(ActiveCell.Row, "D").Offset(1).Select
    WebBrowser1.Navigate ActiveSheet.Range.Selection.Value
End Sub

It gives an error as under.

enter image description here

Please guide me where I am writing wrong code. thanks

braX
  • 11,506
  • 5
  • 20
  • 33

2 Answers2

2

I would do this slightly different. I would completely avoid the use of Activecell/Selection etc. You may want to see How to avoid using Select in Excel VBA

I will use 1 cell to store the current row number and use that. Here is an example. I am going to use say Cell D1 and make its font white so that it will not show. For demonstration purpose, I am not making it white. Change it to whatever you want. You have asked for NEXT button. I am also including a code which you can use for PREVIOUS.

See this example

Option Explicit

Dim ws As Worksheet
Dim lRow As Long
Dim NextRow As Long

Sub NextMap()
    '~~> Set this to the relevant worksheet
    Set ws = Sheet1
    
    With ws
        '~~> Get last row of the Col D
        lRow = .Range("D" & .Rows.Count).End(xlUp).Row
        
        '~~> Get the next row
        NextRow = .Range("D1").Value2 + 1
        
        '~~> Check if it is greater than last row
        If NextRow > lRow Then
            MsgBox "End of data reached"
        Else
            'WebBrowser1.Navigate .Range("D" & NextRow).Value2
            
            '~~> Store the row number in D1
            .Range("D1").Value = .Range("D1").Value + 1
        End If
    End With
End Sub

Sub PreviousMap()
    Set ws = Sheet1
    
    With ws
        NextRow = .Range("D1").Value2 - 1
        
        If NextRow < 2 Then
            MsgBox "Begining of data reached"
        Else
            'WebBrowser1.Navigate .Range("D" & NextRow).Value2
            
            .Range("D1").Value = .Range("D1").Value - 1
        End If
    End With
End Sub

In ACTION

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
1

Minimally, the code WebBrowser1.Navigate ActiveSheet.Range.Selection.Value should be changed to something along the lines of WebBrowser1.Navigate ActiveCell.Value. The syntax you originally used is poorly formed.

basodre
  • 5,720
  • 1
  • 15
  • 23