What have I done wrong in VBA

jameshunt83

Board Regular
Joined
Oct 2, 2010
Messages
149
Can any please help me correct this so it works. The debug shows the error as being on this line. One day I may get the hang of this VBA!

Code:
Private Sub Add1_Click()
Dim Matref As Variant, wb As Workbook
Dim i As Long

i = 1
Matref = Range("I7").Text
Set wb = Workbooks.Open("t:\shared\stocklist.xlsx")

With Rows(Matref)
Do Until Columns(i).Text = ""
    i = i + 1
Loop
[COLOR="Red"]wb.Sheets("stock").Range(i, Matref).Value = Workbooks("stock.xlsm").Sheets("Stock-Control").Range("J7").Value[/COLOR]
End With

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Should it be?

Rich (BB code):
wb.Sheets("stock").Cells(i, Matref).Value = Workbooks("stock.xlsm").Sheets("Stock-Control").Range("J7").Value
 
Upvote 0
No it still fails on that line. Error message is runtime error '1004': Application-defined or object-defined error
 
Upvote 0
In "I7" is just a number such as "2"

Basically what I want is the code to copy the value in "J7" in one workbook to the next available free cell on the row number in "I7" in another workbook
 
Upvote 0
Try

Code:
Private Sub Add1_Click()
Dim Matref As Variant, wb As Workbook
Dim i As Long

Matref = Range("I7").Text
i = Cells(Matref, Columns.Count).End(xlToLeft).Column + 1

Set wb = Workbooks.Open("t:\shared\stocklist.xlsx")
wb.Sheets("stock").Cells(Matref, i).Value = Workbooks("stock.xlsm").Sheets("Stock-Control").Range("J7").Value
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,921
Latest member
BBQKING

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