Excel with VB6.0 - excel

I am new to VB 6.0 ,I am trying to process text and xls files and placing the result in to Excel workbook.
Could you please anyone tell me how to create tabs in Excel sheet and place the content in that.
Regards,
Raju

Here is the code to create a new sheet in VBA:
Dim oSheet As Worksheet Variant
Set oSheet = Worksheets.Add
If you need to manipulate the sheet (rename...), see this link

A quick and easy way would be to open Excel, record a macro and select a sheet and enter some data. The VB 6 code will pretty much be the same code as in the recorded macro once You've set a reference to Excel from VB 6 (or you can use late binding and the createObject function).

Related

VBA code to update links in another workbook

Can someone help me on this? I have an excel file where I want to write a code that picks a value from a specific cell from the same workbook and paste it into another workbook. I am not sure how can I connect two workbooks and update the value.
You can also use queries for this.
If you specifically want to do this with VBA, you can open another workbook using:
Dim Tr As Workbook
Set Tr = Workbooks.Open(TFileName)
where TFileName stands for the full path of your second workbook. If you set your second workbook you can copy paste anything you want.
You can also use this:
Tr.Windows(1).Visible = False
so the workbook stays in the background

Generate Excel Spreadsheet with Macros using Microsoft Access

While generating hundreds of Office Excel spreadsheets with Office Access is certainly possible, it would be great to add macros to the generated workbooks.
I would like to add the functions to the object "ThisWorkbook" in the VBA project for each spreadsheet on generation. How would one go about doing this?
Thank you in advance!
Under the assumption that the macro's in all generated workbooks are the same,
create a template containing all VBA code (and optionally constant text like headers, footers, print range definitions, etc. - i.e. "everything except data")
create any new workbook from the template
insert your data into the WB object
save as macro enabled worksheet (Excel 2007/2010)
close it
example
Sub CreateWB()
Dim WB As Workbook
Set WB = Workbooks.Add("MacroTemp.xltm") ' contains VBA, ActiveX, etc.
WB.Worksheets("Sheet1").[A1] = "co-cooo!" ' adding data
WB.SaveAs "MyGenWB", xlOpenXMLWorkbookMacroEnabled
WB.Close
End Sub
In Excel 2007/2010 do not forget to save the template as macro enabled template (*.xltm").

Cannot see excel sheet in VBE

I am working with an Excel file that was created by somebody else.
One sheet containing Macros appears to be password protected, but what I don't understand is that I cannot see it in VBE under the sheet list. The sheet tab is visible in Excel, but I cannot see the content.
Is there any way to unhide it in VBE?
One sheet containing Macros
Does that refer to Excel 4.0 macros?
Worksheets containing Excel 4.0 macros don't appear to be visible within the list in VBE.
They do appear to be accessible from VBA to some extent: using Excel 2007 I inserted an Excel 4.0 macro sheet to a workbook then tried the following:
Public Sub TestAccessToXL4MacroSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet ' succeeds
Debug.Print ws.Name ' outputs "Macro1"
Set ws = Worksheets("Macro1") ' fails: "Subscript out of range"
End Sub
As far as I know, there is no way you can hide a sheet from VBE! However, you can rename it there (in effect changing the .CodeName of the worksheet). Thus, if you know the Excel worksheet name (the one you see in the Excel worksheet tab), but cannot find it in VBE, go to the Immediate window in VBE (Ctrl-G) and run
? Worksheets("YourName").CodeName - this should give you the name under which it can be found in the VBE Project tree.

Copy one xls's all sheets to another xls file using macro vba via customized button designed on sheet

I have one file of xls and it contains 15 sheets data and I want to copy all data to my another xls file. I have so many files like this so I want to make it customized using macro vba coding. I want to design button on first sheet which do run macro code and copy all my sheets to another file.So is it possible? I am not aware more about ms office.Would any one share me guideline,link or any related data for this.
Thank you all
This assumes both files are open and runs from the dept.xls since that seems to be the one you are preserving.
Sub emp_to_dept()
Dim wks As Worksheet
Windows("Employees.xls").Activate
For Each wks In ActiveWorkbook.Worksheets
wks.Copy After:=Workbooks("Dept.xls").Sheets(1)
Next
Set wks = Nothing
End Sub

EXCEL 2007 - Need Help creating a button which takes the contents of active worksheet and pastes it in a new worksheet

I have search throughout this site to find a answer to my problem and most of the related solutions are for a far more complicated problem. Here is what I need to have done. I created a simple form in Excel 2007. I am looking for the ability to add a button at the bottom of the form which allows the user to click on the button and copy that worksheet into a new worksheet within the same excel document. Basically just duplicating the active worksheet.
I tried to do it with macros but did not get the desired results, and most of our co-workers still use Excel 2003 so I am not sure if macros will work in the older version of excel. I do not know any VBA which is why I come here in search of help from you all.
So to recap.
One sheet Excel document with a simple form and a command button at the bottom of the active worksheet
The command button "Copy and Paste" that worksheet into a new worksheet within the same excel document
A solution that could work in both Excel 2003 and 2007 if possible. If not, for 2007.
Thanks so much ahead of time for anyone who is willing to help out a Novice Excel User.
Assuming that you know how to add a button here is a simple line of code to duplicate the active worksheet:
Sub Button1_Click()
ActiveSheet.Copy after:=ActiveSheet
End Sub
Maybe something like this (tested in Excel 2003 only):
Dim srcSheet, dstSheet
Set srcSheet = ActiveSheet
Sheets.Add
Set dstSheet = ActiveSheet
srcSheet.Activate
srcSheet.Cells.Select
Selection.Copy
dstSheet.Activate
dstSheet.Cells.Select
ActiveSheet.Paste
You should find this method will work in both Excel 2003 and Excel 2007. In your form, add the following method:
Sub CopySheet(WorkSheetName as String)
Dim WrkSht As Worksheet
Set WrkSht = Sheets(WorkSheetName)
WrkSht.Copy After:=Sheets(WorkSheetName)
Set WrkSht = Nothing
End Sub
From the button click event, call it using:
Sub Button1_Click()
Call CopySheet("WorkSheetToCopyName")
'You could also replace the string name with ActiveSheet if you so wish
End Sub
This will dump a copy of the worksheet in between the current sheet and the next one. I've tested it in Excel 2003 and Excel 2007 and it works in both. It doesn't give the second one a pretty name sadly - it just gets the same name as the source worksheet with (2) put after it.
All the formatting, protection and formulas are copied across too - it's a carbon copy of the first.
I know the question is quite old, but just wanted to note that you (and the user) can do the exact same thing with zero code: right-click on the sheet name at the bottom and select Move or Copy..., then check the Create a copy box and click Ok. Yes, it takes 4 clicks, but it is super easy and avoids code.

Resources