Option Explicit
Private Sub Workbook_Open()
Call ReadDataFromCloseFile
End Sub
Sub ReadDataFromCloseFile()
On Error GoTo ErrHandler
[Link] = False
Dim src As Workbook
' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
Set src = [Link]("C:\[Link]", True, True)
[Link]
' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
Dim iTotalRows As Integer
iTotalRows = [Link]("sheet1").Range("B1:B" & Cells([Link],
"B").End(xlUp).Row).[Link]
' COPY DATA FROM SOURCE (CLOSE WORKGROUP) TO THE DESTINATION WORKBOOK.
Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("B" & iCnt).Formula =
[Link]("Sheet1").Range("B" & iCnt).Formula
Next iCnt
' CLOSE THE SOURCE FILE.
[Link] False ' FALSE - DON'T SAVE THE SOURCE FILE.
Set src = Nothing
ErrHandler:
[Link] = True
[Link] = True
End Sub
Sub OpenWorkbook ()
Dim strFile As String
strFile = [Link]()
[Link] (strFile)
End Sub
Dim MyFile As String
MyFile = [Link]()
[Link] (MyFile)
1. The code line below closes [Link].
Workbooks("[Link]").Close
2. The code line below closes the first opened/created workbook.
Workbooks(1).Close
3. The code line below closes the active workbook.
[Link]
4. The code line below closes all workbooks that are currently open.
[Link]
5. The code line below opens [Link].
[Link] ("[Link]")
Sub OpenNewWorkbook()
Dim wb As Workbook
Set wb = [Link]
End Sub
Option Explicit
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "A1:IV65536"
' If you know the data will only be in a smaller range, reduce the size of the
ranges above.
[Link] Now
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet2").Range(strRangeToCheck) ' or whatever your
other sheet is.
[Link] Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
' Cells are different.
' Code goes here for whatever it is you want to do.
End If
Next iCol
Next iRow
Set wbkA = [Link](filename:="C:\[Link]")
Set varSheetA = [Link]("Sheet1") .
Sub Mover()
Dim aSheet As Worksheet, pSheet As Worksheet
Dim alr As Long, i As Long, plr As Long
Set aSheet = [Link]
Set pSheet = [Link]("Sheet2") 'SET PASTE SHEET NAME HERE
alr = [Link]([Link], 1).End(xlUp).Row 'FIND LAST ROW OF COLUMN A (1)
For i = 2 To alr 'START FROM ROW 2 TO LAST ROW
If [Link](i, 1) <> "" And [Link](i, 15) = "" Then 'IF A IS
NOT EMPTY AND O IS EMPTY
plr = [Link]([Link], 1).End(xlUp).Row + 1 'FIND
LAST ROW ON P SHEET
[Link](i).Copy Destination:=[Link](plr) 'COPY
FROM A SHEET TO P SHEET
End If
Next
End Sub
'Copy the data
Sheets("Sheet1").Range("A1:B10").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Range("E1").Select
'Paste in the target destination
[Link]
[Link] = False
End Sub
Sheets("sheet1").Range("C:E").Copy Sheets("sheet2").Range("G:I")
[Link] = "NewSheet"
Add Sheet with Name
You can also define a Sheet name as you create the new Sheet:
1
[Link] = "NewSheet"
Create New Sheet with Name from a Cell
Or use a cell value to name a new Sheet:
1
[Link] = range("a3").value
Add Sheet Before / After Another Sheet
You might also want to choose the location of where the new Sheet will be inserted.
You can use the After or Before properties to insert a sheet to a specific location
in the workbook.
Insert Sheet After Another Sheet
This code will insert the new sheet AFTER another sheet:
1
[Link] After:=Sheets("Input")
This will insert a new Sheet AFTER another sheet and specify the Sheet name:
1
[Link](After:=Sheets("Input")).Name = "NewSheet"
Notice the extra parenthesis required in the second example (the first example will
generate an error if the second parenthesis are added).
Add Sheet To End of Workbook
To add a Sheet to the end of the workbook:
1
[Link] After:=Sheets([Link])
Add Sheet To Beginning of Workbook:
To add a Sheet to the beginning of the workbook:
1
[Link](Before:=Sheets(1)).Name = "FirstSheet"
Add Sheet to Variable
This code assigns the new Sheet to a variable as the sheet is created:
1
2
Dim ws As Worksheet
Set ws = [Link]
From here you can reference the new sheet with the variable �ws�:
1
[Link] = "VarSheet"
More Add Sheet Examples
Create Sheet if it Doesn�t Already Exist
You might want to create a sheet only if it doesn�t already exist.
Create Worksheets From List of Names
The following routine will look at the contents of a single column set up Excel
worksheets within the current workbook with these names. It makes a call to another
function to see if a sheet with that name already exists, and if so the sheet isn�t
created.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Private Sub CommandButton1_Click()
Call CreateWorksheets(Sheets("Sheet2").Range("A1:a10"))
End Sub
Sub CreateWorksheets(Names_Of_Sheets As Range)
Dim No_Of_Sheets_to_be_Added As Integer
Dim Sheet_Name As String
Dim i As Integer
No_Of_Sheets_to_be_Added = Names_Of_Sheets.[Link]
For i = 1 To No_Of_Sheets_to_be_Added
Sheet_Name = Names_Of_Sheets.Cells(i, 1).Value
'Only add sheet if it doesn't exist already and the name is longer than zero
characters
If (Sheet_Exists(Sheet_Name) = False) And (Sheet_Name <> "") Then
[Link]().Name = Sheet_Name
End If
Next i
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Function Sheet_Exists(WorkSheet_Name As String) As Boolean
Dim Work_sheet As Worksheet
Sheet_Exists = False
For Each Work_sheet In [Link]
If Work_sheet.Name = WorkSheet_Name Then
Sheet_Exists = True
End If
Next
End Function
Sub FileOpenDialogBox()
'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in fullpath variable
With [Link](msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection
options
.[Link] "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
fullpath = .[Link](1)
End With
'It's a good idea to still check if the file type selected is accurate.
'Quit the procedure if the user didn't select the type of file we need.
If InStr(fullpath, ".xls") = 0 Then
Exit Sub
End If
'Open the file selected by the user
[Link]
[Link] fullpath
End Sub