Loop / Do problem for Newbie

cagsy

New Member
Joined
Jul 17, 2011
Messages
2
Hi Guys,

I'm pretty average in VBA as is evidenced by this question however hopefully this will be a piece of cake for someone. I had a rough idea of how to do it and did have a go, however jumping from worksheet to worksheet seemed to make the loop lose focus and I couldn't figure out the active cell offset etc. Ended up as a mess basically:(

I have one Excel workbook with 2 worksheets "Input" & "Data".

Worksheet "Input" is a sheet with a number of specific cell values to be used in a macro (which already exists and works fine). Two of the cell values F2 and F8 need to be updated before a macro is run. At this stage this data is being entered manually. All other values are fixed.

Worksheet "Data" is the data source and consists of 2 columns an ID (col A) & a volume for that ID (col B). This could have anywhere from 4 to 200 values down the page. There are no column headings.

I need to take the first 2 cell values A1 and B1 from "Data" worksheet and paste A1 into F2 of "Input" sheet and B1 into F8 of "Input" sheet
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
After the data is populated I need to call up a macro lets call it "calculate"
<o:p> </o:p>
Once the macro successfully runs and closes I want to clear cell F2 and F8 contents. I need to then return to the "Data" worksheet and repeat the same process for the next row of cells A2, B2 then A3, B3 etc until I reach a blank cell.

Any ideas / ways forward would be very much appreciated

Many thanks
Andrew
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

Welcome to the forum

Maybe something like (not fully tested)
Try it on a test-workbook

Code:
Sub test()
    Dim wkData As Worksheet, wkInput As Worksheet
    Dim lastRow As Long, i As Long
    
    Set wkData = Sheets("Data")
    Set wkInput = Sheets("Input")
    
    With wkData
        
        'Getting the last row with data
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        'Looping through row 1 till lastRow
        For i = 1 To lastRow
            wkInput.Range("F2") = .Range("A" & i)
            wkInput.Range("F8") = .Range("A" & i).Offset(, 1)
            'Call Calculate '<--Uncomment this line to call your macro
        Next i
        
        wkInput.Range("F2") = ""
        wkInput.Range("F8") = ""
        
    End With
    
End Sub

HTH

M.
 
Upvote 0
Hi,

Welcome to the forum

Maybe something like (not fully tested)
Try it on a test-workbook

Code:
Sub test()
    Dim wkData As Worksheet, wkInput As Worksheet
    Dim lastRow As Long, i As Long
    
    Set wkData = Sheets("Data")
    Set wkInput = Sheets("Input")
    
    With wkData
        
        'Getting the last row with data
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        'Looping through row 1 till lastRow
        For i = 1 To lastRow
            wkInput.Range("F2") = .Range("A" & i)
            wkInput.Range("F8") = .Range("A" & i).Offset(, 1)
            'Call Calculate '<--Uncomment this line to call your macro
        Next i
        
        wkInput.Range("F2") = ""
        wkInput.Range("F8") = ""
        
    End With
    
End Sub

HTH

M.
Hey Marcelo,

thanks very much for such a quick reply. I ran it through and it works exactly as required. That little bit of code will come in quite handy for a bunch of other things I had planned

All the best

cheers
Andrew
 
Upvote 0
Hey Marcelo,

thanks very much for such a quick reply. I ran it through and it works exactly as required. That little bit of code will come in quite handy for a bunch of other things I had planned

All the best

cheers
Andrew

Andrew,

You are welcome. Glad to help :)

M.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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