Correct my iRow value

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
I’m attempting to extract values from a UserForm TextBox and an open workbook with <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Code:
[FONT=Verdana]ChDir "C:\MyCompany"<o:p></o:p>[/FONT]
[FONT=Verdana]Workbooks.Open Filename:="C:\MyCompany\QBI.xls"<o:p></o:p>[/FONT]
[FONT=Verdana]Set WS = Workbooks("QBI.xls").Worksheets("Sheet1")[/FONT]
[FONT=Verdana]iRow = WS.Cells(Rows.Count, 1) _<o:p></o:p>[/FONT]
[FONT=Verdana].End(xlUp).Offset(1, 0).Row[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]WS.Cells(iRow, 1).Value = Me.TextBox1.Value<o:p></o:p>[/FONT]
[FONT=Verdana]WS.Cells(iRow, 2).Value = "GrSales"<o:p></o:p>[/FONT]
[FONT=Verdana] WS.Cells(iRow, 3).Value = Me.TextBox33.Value<o:p></o:p>[/FONT]
[FONT=Verdana]‘Then…<o:p></o:p>[/FONT]
[FONT=Verdana]   WS.Cells(iRow, 4).Value = Workbooks("Data.xls").Worksheets("Sheet1").Cells(iRow, 34).Value<o:p></o:p>[/FONT]
<o:p></o:p>
<o:p></o:p>
Everything works except…<o:p></o:p>
The value returned to “QBI.xls” WS.Cells(iRow, 4) is from “Data.xls” Row5 instead of “Data.xls” last row.

I'm puzzled.<o:p></o:p>
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
because iRow is the last row in QBI

try

Code:
ChDir "C:\MyCompany"
Workbooks.Open Filename:="C:\MyCompany\QBI.xls"
Set WS = Workbooks("QBI.xls").Worksheets("Sheet1")
iRow = WS.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

WS.Cells(iRow, 1).Value = Me.TextBox1.Value
WS.Cells(iRow, 2).Value = "GrSales"
 WS.Cells(iRow, 3).Value = Me.TextBox33.Value
‘Then…
   WS.Cells(iRow, 4).Value = Workbooks("Data.xls").Worksheets("Sheet1").Cells(Workbooks("Data.xls").Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row, 34).Value
 
Upvote 0
iRow is being defined by the last used row+1 in the book QBI Sheet1 Column A.

Try

WS.Cells(iRow, 4).Value = Workbooks("Data.xls").Worksheets("Sheet1").Cells(Rows.Count, 34).End(xlup).Value
 
Upvote 0
That is because iRow is set to be the last row of "QBI.xls", not "Data.xls"

You would need to redeclare the last row in Data.xls by using the same formula
 
Upvote 0
Try

WS.Cells(iRow, 4).Value = Workbooks("Data.xls").Worksheets("Sheet1").Cells(Rows.Count, 34).End(xlup).Value

Well...I guess my brain is getting blurry from staring at the code.

Thanks jonmo1.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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