VBA code correction

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
What’s wrong with this code?
C:\MyCompany\Data (Sheet1) routine does not work.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Code:
[FONT=Verdana]Private Sub CommandButton1_Click()<o:p></o:p>[/FONT]
[FONT=Verdana]Application.DisplayAlerts = False<o:p></o:p>[/FONT]
[FONT=Verdana]Application.ScreenUpdating = False<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]Dim iRow As Long<o:p></o:p>[/FONT]
[FONT=Verdana]Dim WS As Worksheet<o:p></o:p>[/FONT]
[FONT=Verdana]Set WS = Worksheets("Sheet1")<o:p></o:p>[/FONT]
[FONT=Verdana]  iRow = WS.Cells(Rows.Count, 1) _<o:p></o:p>[/FONT]
[FONT=Verdana].End(xlUp).Offset(1, 0).Row<o:p></o:p>[/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 = Me.TextBox2.Value<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]ThisWorkbook.Save<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]ChDir "C:\Company"<o:p></o:p>[/FONT]
[FONT=Verdana]Workbooks.Open Filename:="C:\MyCompany\Data "<o:p></o:p>[/FONT]
[FONT=Verdana]      Set WS = Worksheets("Sheet1")<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]iRow = WS.Cells(Rows.Count, 1) _<o:p></o:p>[/FONT]
[FONT=Verdana].End(xlUp).Offset(1, 0).Row<o:p></o:p>[/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 = Me.TextBox2.Value<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]ThisWorkbook.Save<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]Application.Quit<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]End Sub[/FONT]
<o:p></o:p>
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
Workbooks.Open Filename:="C:\MyCompany\Data "<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

This line is not opening a workbook. If your workbook is named "Data.xls", then you need to have it read:

Code:
Workbooks.Open Filename:="C:\MyCompany\Data.xls"<o:p></o:p>

Then the Set WS line should read:
Code:
Set WS = Workbooks("Data.xls").Worksheets("Sheet1")
<o:p></o:p>
 
Upvote 0
In this part of the code:
Code:
Workbooks.Open Filename:="C:\MyCompany\Data "<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
What is the name of the file?
Where do you define the file extension, e.g. ".xls" in your code?
 
Upvote 0
Thanks MrKowz

That's the ticket...except this doesn't save C:\MyCompany\Data.xls
It does work in the first instance.
Code:
[FONT=Verdana]ThisWorkbook.Save[/FONT]
 
Upvote 0
Instead of using ThisWorkbook.Save, tell VBA what specific workbook to save:

Code:
Workbooks("Data.xls").Save
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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