Search code examples
vbaexcelexcel-2010export-to-excel

Looping through each cell in a range and updating SAP database


Currently I have values in each cell of a range "A1:D10". I need to copy each cell value at a time into SAP Application and I am using the below code to do that. But the problem is it is copying one cell (A1) 4 times instead of A1, B1, C1, D1. Once the A1 to D1 cells are updated into the SAP application, then the data will be saved in the SAP application and macro should start from B2 to D2 and do the same. Here is the code that I am using currently.

Here is the example

with current code macro is copying (A1) 100 4 times and then moving to (B1) 10 4 times and moving to (C1) 1234 4 times and moving to (D1) BO 4 times, then it will go to row A2 and do the same.

But what I want is it should copy A1 value 100 into session.findById("wnd[8]/usr/ctxtANLA-BRSKT1").Text = cell.Value and B1 value 10 into session.findById("wnd[8]/usr/ctxtANLA-MNSRTS2").Text = cell.Value and C1 value 1234 into session.findById("wnd[8]/usr/ctxtANLA-BUHFT3").Text = cell.Value and D1 value BO into session.findById("wnd[8]/usr/ctxtANLA-BUYTS4").Text = cell.Value .

I hope this is more helpful

Dim rng As Range
Dim Row As Range
Dim Col As Range

Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("A2:D" & Lastrow)

For Each Row In rng.Rows
    For Each cell In Row.Cells

    If cell.Value <> "" Then

        session.findById("wnd[8]/usr/ctxtANLA-BRSKT1").Text = cell.Value
        session.findById("wnd[8]/usr/ctxtANLA-MNSRTS2").Text = cell.Value
        session.findById("wnd[8]/usr/ctxtANLA-BUHFT3").Text = cell.Value
        session.findById("wnd[8]/usr/ctxtANLA-BUYTS4").Text = cell.Value

    End If

Next cell

Next Row

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Solution

  • Is this what you are trying (Untested)?

    Sub Sample()
        Dim i As Long, Lastrow As Long
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        With ActiveSheet
            Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
    
            For i = 2 To Lastrow 
                If .Cells(i, 1).Value <> "" Then _
                session.findById("wnd[8]/usr/ctxtANLA-BRSKT1").Text = .Cells(i, 1).Value
    
                If .Cells(i, 2).Value <> "" Then _
                session.findById("wnd[8]/usr/ctxtANLA-MNSRTS2").Text = .Cells(i, 2).Value
    
                If .Cells(i, 3).Value <> "" Then _
                session.findById("wnd[8]/usr/ctxtANLA-BUHFT3").Text = .Cells(i, 3).Value
    
                If .Cells(i, 4).Value <> "" Then _
                session.findById("wnd[8]/usr/ctxtANLA-BUYTS4").Text = .Cells(i, 4).Value
            Next i
        End With
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub
    

    For a large range, I would still recommend using an array instead of looping through rows as I mentioned in the 2nd comment below your question.