Vba Code
Vba Code
Vba Code
Sub ReadDataFromCloseFile()
On Error GoTo ErrHandler
Application.ScreenUpdating = False
' 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 =
src.Worksheets("Sheet1").Range("B" & iCnt).Formula
Next iCnt
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub OpenWorkbook ()
strFile = Application.GetOpenFilename()
Workbooks.Open (strFile)
End Sub
Workbooks("close-open.xlsm").Close
2. The code line below closes the first opened/created workbook.
Workbooks(1).Close
Workbooks.Close
5. The code line below opens sales.xlsx.
Workbooks.Open ("sales.xlsx")
Sub OpenNewWorkbook()
Dim wb As Workbook
Set wb = Workbooks.Add
End Sub
Option Explicit
Sub test()
strRangeToCheck = "A1:IV65536"
' If you know the data will only be in a smaller range, reduce the size of the
ranges above.
Debug.Print Now
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet2").Range(strRangeToCheck) ' or whatever your
other sheet is.
Debug.Print Now
Sub Mover()
End Sub
Application.CutCopyMode = False
End Sub
Sheets("sheet1").Range("C:E").Copy Sheets("sheet2").Range("G:I")
ActiveSheet.Name = "NewSheet"
Add Sheet with Name
You can also define a Sheet name as you create the new Sheet:
1
Sheets.Add.Name = "NewSheet"
Create New Sheet with Name from a Cell
Or use a cell value to name a new Sheet:
1
Sheets.Add.Name = range("a3").value
1
Sheets.Add After:=Sheets("Input")
This will insert a new Sheet AFTER another sheet and specify the Sheet name:
1
Sheets.Add(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
Sheets.Add After:=Sheets(Sheets.Count)
1
Sheets.Add(Before:=Sheets(1)).Name = "FirstSheet"
1
2
Dim ws As Worksheet
Set ws = Sheets.Add
From here you can reference the new sheet with the variable �ws�:
1
ws.name = "VarSheet"
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
No_Of_Sheets_to_be_Added = Names_Of_Sheets.Rows.Count
For i = 1 To No_Of_Sheets_to_be_Added
'Only add sheet if it doesn't exist already and the name is longer than zero
characters
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 ThisWorkbook.Worksheets
Next
End Function
Sub FileOpenDialogBox()
'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
End Sub