Writing values to already existing worksheet - vb.net

I already have an Excel worksheet. How do I write some values to that worksheet from my VB application?

Note: This is an Excel-VBA solution; however, someone has changed the question tag to VB.NET since.
Sub MySub()
Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open("C:\MyBook.xls")
Set xlSheet = xlBook.ActiveSheet
xlSheet.Range("A1").Value = "Hello, world!"
End Sub

Related

Excel become unresponsive after running macro using vbs

From VBS, I am running a macro "Inc" in an already opened excel workbook. The macro is getting executed (this is confirmed after seeing the updated cell value after reopening the workbook), but the excel becoming soon after I run the code. Can you please let me know what I need to change in my code.
Macro code :
Sub Inc()
Dim bs As Worksheet
Set bs = Workbooks("Main.xlsm").Sheets("TimeStampWork")
bs.Range("K11").Value = bs.Range("K11").Value + 1
bs.Range("L11").Value = Now
bs.Range("L11").NumberFormat = "dd-mm-yyyy, hh:mm:ss"
End Sub
VBS code :
Option Explicit
Dim xlApp, xlBook
Set xlBook = GetObject("E:\Main.xlsm")
Set xlApp = xlBook.Application
xlApp.Visible = True
xlApp.Run "Inc"
xlBook.Save
Set xlBook = Nothing
Set xlApp = Nothing
Update:
Instead of the macro "Inc" I tried to run a macro with just a simple message box, and it works without any issues. I can't see any problems with excel.
This issue has been resolved now. Changed my macro with screenupdate option which resolved the issue
Sub Inc()
Application.ScreenUpdating = False
Dim bs As Worksheet
Set bs = Workbooks("Main.xlsm").Sheets("TimeStampWork")
bs.Range("K11").Value = bs.Range("K11").Value + 1
bs.Range("L11").Value = Now
bs.Range("L11").NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Application.ScreenUpdating = True
End Sub

Using other workbook without opening it

I created Excel macro, in which I'm searching current date in other, closed workbook and getting the number of the row where this date is. Everything works perfectly, but only if I'm opening this other workbook.
Is there a possibility to make the code works without opening this workbook? Or maybe it's possible to make this workbook not to appear, because I don't want it to appear on the screen?
I've tried many methods, e.g. Application.ScreenUpdating = false or ActiveWorkbook.Windows(1).Visible = False but it doesn't work the way I want it.
You need to create a new Excel Application to be able to hide the Window:
Dim xlApp As New Excel.Application 'New Excel Application
Dim xlWB As Excel.Workbook 'Workbook Object
xlApp.Visible = False 'Hide Application
Set xlWB = xlApp.Workbooks.Open("PATH TO FILE") 'Open File in new App
'do something here
xlWB.Sheets(1).Cells(1, 1).Value = "Hello, this is a Test"
xlWB.Close 'Close Workbook (Use SaveChanges:=True to save changes)
Set xlWB = Nothing 'Clear Object
xlApp.Quit 'Quit Excel App
Set xlApp = Nothing 'Clear Object

Excel VBA - update BuiltinDocumentProperties using value from different workbook

I want to create a "driver" workbook where someone can update values that will be applied to another workbook. The contents of cell B8 of "macros.xlsm" contains the text string I want to use for the author of "report1.xlsx". I have written the following macro but keep getting a
"Object doesn't support this property or method" error on the last line.
Sub add_properties()
Dim xL As Excel.Application
Set xL = New Excel.Application
Dim mainWB As Excel.Workbook
Dim reportWB As Excel.Workbook
Set mainWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\macros.xlsm")
Set reportWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
MsgBox mainWB.Sheets("adHoc").Range("B8").Value
mainWB.Sheets("adHoc").Range("b8").Copy
reportWB.BuiltinDocumentProperties("author").PasteSpecial (xlPasteValues)
End Sub
I am also using "macros.xlsm" to update margins, headers, etc for "report1.xlsx" - will this work on those too?
Try opening your workbooks this way. You'll need to add the Microsoft Excel Object Library to your references.
Dim xL As excel.Application
Set xL = New excel.Application
Dim mainWB As excel.Workbook
Dim reportWB as excel.Workbook
Set mainWB = xL.Workbooks.Open("macros.xlsm")
Set reportWB = xL.Workbooks.Open("report1.xlsx")
I figured out how to get it to work. Macros.xlsm is already open and contains the macro that I want to execute. I didn't have to use PasteSpecial. I think the main difference is that I had to declare a variable "author" then use the variable "author" to update the report workbook. I am new to VBA - and would appreciate any other input or explanations. Thank you for your help.
Sub add_properties()
Dim author
Dim reportWB As Excel.Workbook
Set reportWB = Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
author = Workbooks("macros.xlsm").Sheets("adHoc").Range("b8").Value
reportWB.BuiltinDocumentProperties("author").Value = author
End Sub

How to switch to open Excel spreadsheet and work on it from Word with VBA

I'm just trying to work on an open Excel spreadsheet from Word using VBA. I did a search on this and found the following code on How to get reference to an open Excel spreadsheet from Word?. The problem is, it seems to open a separate instance of Excel rather than the one I already have open, and then closes it after the action. How can I get the procedure to switch to the open Excel spreadsheet, perform the desired actions, and leave the spreadsheet open?
Sub DoStuffWithExcelInWord()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.csv")
Set wk = wkbk.Sheets(1)
Debug.Print wk.Cells(1, 1).Value 'Here's where I would like to insert my code
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Thanks for any assistance!
Thanks again to dcromley for his earlier response. In the meantime, here's what I came up after a couple more searches on the Internet. It takes care of the situation where the Excel document is already open. Hopefully that will help others with similar situations, although it's not exhaustive (for example, if several Excel documents are open).
Sub DoStuffWithExcelInWordRevised()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If xl Is Nothing Then
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.xlsx")
Set wk = wkbk.Sheets(1)
End If
xl.Visible = True
With xl
' Insert Excel code here
End With
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Try:
Add xl.Visible = True
Del xl.Quit
It appears that you are trying to do this with a Workbook that is already open. Since you want to use the open workbook / application then you need to:
Set xl = GetObject(,"Excel.Application")

How to define an object inside a for loop for excel vba

I want to import data from multiple workbooks, all from the same sheet index (3).
I'm new to vba, and I figured out how to open multiple files up, and also to copy data from one sheet to another sheet in a different workbook for a single file, but I can't seem to figure out how to do that for multiple files.
I highlighted where the error is, it tells me "object doesn't support this property or method"
Could you please help?
Thanks
Sub dataimport()
' Set Vars
Dim ArbinBook As Workbook, DataBook As Workbook
Dim i As Integer, j As Integer
Dim Caption As String
Dim ArbinFile As Variant, DataFile As Variant
' make weak assumption that active workbook is the target
Set DataBook = Application.ActiveWorkbook
' get Arbin workbook
Caption = "Please select an input file"
' To set open destination:
' ChDrive ("E")
' ChDir ("E:\Chapters\chap14")
' With Application
'Set "arbinfile" as variant, the "true" at end makes it into an array
ArbinFile = Application.GetOpenFilename(, , Caption, , True)
'Exit when canceled
If Not IsArray(ArbinFile) Then
MsgBox "No file was selected."
Exit Sub
End If
Dim targetSheet As Worksheet
Set targetSheet = DataBook.Sheets(1)
'Open for every integer i selected in the array "arbinfile"
For i = LBound(ArbinFile) To UBound(ArbinFile)
Set ArbinBook = Workbooks.Open(ArbinFile(i))
targetSheet.Range("A2", "G150").Value = ArbinBook.Sheets(3).Range("A2", "G150").Value
**ERROR at the line above**
Workbooks(DataSheet).Activate 'Reactivate the data book
Worksheets(1).Activate 'Reactivate the data sheet
ActiveWorkbook.Sheets(1).Copy _
after:=ActiveWorkbook.Sheets(1)
Workbooks(ArbinFile(1)).Activate 'Reactivate the arbin book(i)
ArbinBook.Close
Next i
Beep
End Sub
My instinct tells me that ArbinBook.Sheets(3) is a Chart-sheet, not a WorkSheet (or, at least, it is something other than a WorkSheet). It might be hidden as well, but it will still be indexed as (3).
If so, change Sheets(3) to Worksheets(3).
Added: BTW If true, this also demonstrates why using index-numbers is unreliable. If at all possible, refer to a worksheet by its name. (I appreciate that this may not always be possible.)
Added (from comments) There is nothing named DataSheet in your code. Add Option Explicit to the top of your module to indicate all such errors.
Try changing the line Set ArbinBook = Workbooks.Open(ArbinFile(i))
to Set ArbinBook = Workbooks(arbinfile(i))
I could be wrong, but I think it's trying to set your workbook object to become the action of opening another workbook, instead of labeling it as the workboook.
Sub Multiple()
Application.DisplayAlerts = False
Application.EnableEvents = False
Dim exlApp As Excel.Application
Dim exlWb1 As Excel.Workbook
Dim exlWb2 As Excel.Workbook
Dim exlWb3 As Excel.Workbook
Dim exlWs1 As Excel.Worksheet
Dim exlWs2 As Excel.Worksheet
Dim exlWs3 As Excel.Worksheet
Set exlApp = CreateObject("Excel.Application")
Set exlWb1 = exlApp.Workbooks.Open("C:\yourpath1\file1.xls")
Set exlWb2 = exlApp.Workbooks.Open("C:\yourpath2\file2.xls")
Set exlWb3 = exlApp.Workbooks.Open("C:\yourpath3\file3.xls")
Set exlWs1 = exlWb.Sheets("Sheet1")
Set exlWs2 = exlWb.Sheets("Sheet1")
Set exlWs3 = exlWb.Sheets("Sheet1")
exlWb1.Activate
exlWb2.Activate
exlWb3.Activate
'code
exlWb.Close savechanges:=True
exlWb.Close savechanges:=True
exlWb.Close savechanges:=True
Set exlWs1 = Nothing
Set exlWs2 = Nothing
Set exlWs3 = Nothing
Set exlWb1 = Nothing
Set exlWb2 = Nothing
Set exlWb3 = Nothing
exlApp.Quit
Set exlApp = Nothing
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub