I have a workbook with various worksheets. Each sheet has a button to open a userform which i used to input data in the respective sheet. E.g. button 1 in "sheet1" to open userform1 to input data in sheet1. Button 2 in "sheet2" to open userform2 to input data in sheet2 and so on.
I wanted to create shortcut buttons for each button 1,2 in the main sheet to directly open respective userform i wanted.
Thanks for your help in advance.
I supposed that the buttons are form controls and you rename their caption "Form1", "Form2" and so on ..
The first sheet name is "Main" >> so try this code that will copy your buttons to the Main sheet
Sub Test()
Dim ws As Worksheet
Dim sh As Worksheet
Dim shp As Shape
Dim rw As Long
Set ws = ThisWorkbook.Worksheets("Main")
rw = 5
Application.ScreenUpdating = False
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> ws.Name Then
sh.Activate
For Each shp In ActiveSheet.Shapes
If shp.Type = msoOLEControlObject Or shp.Type = msoFormControl Then
If Left(shp.AlternativeText, 4) = "Form" Then
shp.Copy
Application.Goto Sheets("Main").Range("G" & rw)
ActiveSheet.Paste
rw = rw + 2
End If
End If
Next shp
End If
Next sh
Application.ScreenUpdating = True
End Sub
Related
Sorry for the oddly worded question. I have code (below) that creates new sheets based on column data. After the sheets are created VBA copies and pastes every row from the master sheet into the category sheet. I just want excel to save the .csv file and close. It closes but only keeps the last sheet. Is this due to it being a .csv file? If I manually Save As and convert to .xlsx then the columns remain. But I tried adding VBA code to do the same thing and it just saved an empty .xlsx file. I'm not sure what to do...
Sub Loading_Summary_Breakout()
'Prevents Clipboard Pop-up from appearing.
Application.DisplayAlerts = False
'Prevents screen flicker and makes the macro run faster.
Application.ScreenUpdating = False
'Opens Loading Summary workbook.
Workbooks.Open Filename:=Environ("USERPROFILE") & "\Dropbox (Gotham Enterprise)\Operations Management\#MASTER SCHEDULE\Shop Schedule V4\Loading Summary.csv"
Workbooks("Loading Summary.csv").Activate
Call DeleteRowsSpecialChartrs
Dim cell As Range, v
Dim SheetName As String, wb As Workbook, ws As Worksheet
Set ws = ActiveSheet
Set wb = ws.Parent
'Creates new worksheet/tab for every unique value in Column B (Customer Code Column)
For Each cell In ws.Range(ws.Range("B2"), ws.Range("B" & Rows.Count).End(xlUp)).Cells
v = cell.Value
If Len(v) > 0 Then cell.EntireRow.Range("A1:O1").Copy _
GetSheet(v, wb).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Next
Call DeleteDuplicates
ActiveWorkbook.Save
Application.ScreenUpdating = True
End Sub
'Return a named sheet in wb (or create if doesn't exist)
Private Function GetSheet(ByVal SheetName As String, wb As Workbook)
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Worksheets(SheetName)
On Error GoTo 0
If ws Is Nothing Then
Set ws = wb.Worksheets.Add(after:=wb.Sheets(wb.Sheets.Count))
ws.Name = SheetName
End If
Set GetSheet = ws
End Function
Public Sub DeleteRowsSpecialChartrs()
Dim rng As Range
Dim pos As Integer
Set rng = ActiveSheet.Range("B:B")
For i = rng.Cells.Count To 1 Step -1
pos = InStr(LCase(rng.Item(i).Value), LCase("/"))
If pos > 0 Then
rng.Item(i).EntireRow.Delete
End If
Next i
End Sub
Public Sub DeleteDuplicates()
Dim ws As Worksheet
Dim wkbk1 As Workbook
Dim w As Long
Set wkbk1 = Workbooks("Loading Summary.csv")
wkbk1.Activate
With wkbk1
For w = 1 To .Worksheets.Count
With Worksheets(w)
.Range("A:O").RemoveDuplicates Columns:=1, Header:=xlYes
End With
Next w
End With
End Sub
I wonder what the text in this message means...
I's the text you see when you 'Save As'/'CSV'.
I have numerous sheets, all with a button which activates a macro.
With VBA, how can I pick up the name of the macro attached to the shape on each sheet?
I have the code to look at each sheet, but can't see how to get the macro name from the shape properties.
Sub LoopandExamine()
Dim ws As Worksheet
Dim s As Shape
For Each ws In Worksheets
For Each s In ws.Shapes
If s.OnAction <> "" Then
Debug.Print s.Name & ";" & s.OnAction
End If
Next s
Next ws
End Sub
I am wanting to have a button open a userform with a list of all open Workbooks. The user selects the workbook they want and the code copies data from a fixed range in the current workbook and pastes it into a fixed range in the user selected workbook.
While searching around I found this code, that works similarly but copies from the selected workbook and pastes into the current one.
Option Explicit
Const PSWD = "atari"
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub CopyPasteButton_Click()
ActiveSheet.Unprotect Password:=PSWD
'This code will be executed when the "Copy" button is clicked on the userform.
Dim wsData As Worksheet
Dim rCopy As Range
Dim CopyRw As Long
Set wsData = ThisWorkbook.Sheets("SALES Details")
With Application
.DisplayAlerts = False
.ScreenUpdating = True
With wsData
.Unprotect PSWD
CopyRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
End With
On Error GoTo exit_err
With Workbooks(Me.ListBox1.Value).Sheets("Master Sheet")
Set rCopy = .Cells(10, 1).CurrentRegion
Set rCopy = rCopy.Offset(1, 0).Resize(rCopy.Rows.Count - 1, 40)
rCopy.Copy ThisWorkbook.Sheets("SALES Details").Cells(CopyRw, 1)
End With
Unload Me
exit_err:
wsData.Protect Password:=PSWD
.DisplayAlerts = True
.ScreenUpdating = True
.CutCopyMode = False
End With
End Sub
Private Sub UserForm_Activate()
'Populate list box with names of open workbooks, excluding main workbook.
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then ListBox1.AddItem wb.Name
Next wb
End Sub
This code works great, for what it does. I have been trying to edit it without luck. How can I edit this to reverse the direction and have it copy from a fixed range in the current sheet (A50:J57) to a fixed range on the user selected sheet (A4:J11)?
I think this should work. Of course you have to adapt the sheet names in code.
Private Sub CopyPasteButton_Click()
Dim mySheet As Worksheet, otherSheet As Worksheet
On Error GoTo exit_err
Application.DisplayAlerts = False
Set mySheet = ThisWorkbook.Sheets("SheetXYZ")
Set otherSheet = Workbooks(Me.ListBox1.Value).Sheets("SheetABC")
mySheet.Range("A50:J57").Copy Destination:=otherSheet.Range("A4:J11")
exit_err:
Application.DisplayAlerts = True
End Sub
UPDATE
For copying the values and not the formulas of the range use this code instead of the Copy function:
mySheet.Range("A50:J57").Copy
otherSheet.Range("A4:J11").PasteSpecial xlPasteValuesAndNumberFormats
For further options of the PasteSpecial function see the documentation.
I would like to have a code that unchecks all checkboxes named "CheckBox1" for all sheets across the workbook. My current code unfortunately doesn't work, and I'm not sure why - it only works for the active sheet.
Private Sub CommandButton1_Click()
Dim Sheet As Worksheet
For Each Sheet In ThisWorkbook.Worksheets
Select Case CheckBox1.Value
Case True: CheckBox1.Value = False
End Select
Next
End Sub
This code iterates through all sheets (except sheets named Sheet100 and OtherSheet) and unchecks all your ActiveX checkboxes named CheckBox1
Sub uncheck_boxes()
Dim ws As Worksheet
Dim xbox As OLEObject
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet100" And ws.Name <> "OtherSheet" Then
For Each xbox In ws.OLEObjects
ws.OLEObjects("CheckBox1").Object.Value = False
Next
End If
Next
End Sub
To uncheck all ActiveX checkboxes in all sheets disregarding the names used
Sub uncheck_all_ActiveX_checkboxes()
Dim ws As Worksheet
Dim xbox As OLEObject
For Each ws In ThisWorkbook.Worksheets
For Each xbox In ws.OLEObjects
ws.OLEObjects(xbox.Name).Object.Value = False
Next
Next
End Sub
To uncheck all Form Control checkboxes on a spreadsheet use
Sub uncheck_forms_checkboxes()
Dim ws As Worksheet
Dim xshape As Shape
For Each ws In ThisWorkbook.Worksheets
For Each xshape In ws.Shapes
If xshape.Type = msoFormControl Then
xshape.ControlFormat.Value = False
End If
Next
Next
End Sub
[edited following comments]
Try this:
Sub test()
Dim ws As Excel.Worksheet
Dim s As Object
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Definitions" And ws.Name <> "fx" Then
Set s = Nothing
On Error Resume Next
Set s = ws.OLEObjects("CheckBox1")
On Error GoTo 0
If Not s Is Nothing Then
s.Object.Value = False
End If
End If
Next ws
End Sub
This is a global function (it doesn't belong to a particular sheet), but you can put it inside CommandButton1_Click() if you want.
You might not need the error blocking if your sheets (other than Definitions and fx) always contain CheckBox1. Alternatively remove that if statement.
I have a file on .csv format and from A-S columns, it has some records like a table. My complete program will insert/remove/delete/add some rows, columns and editing cell values etc. I managed to code all the operations that i need, now i'm trying to integrate it with a gui.
What I want is to display cells from Ax1 to the last column that has record on VBA user form. How can i do that?
*ps: again, my file's format is .csv and I am using Excel 2007
You can use a multi column Listbox to show the data.
LOGIC
Import the text (Csv) file in the temp sheet
Show that data in the multicolumn Listbox
Delete the temp sheet in the Userform unload event
Import the text (Csv) file in the temp sheet
Private Sub CommandButton1_Click()
Dim wb As Workbook, wbTemp As Workbook
Dim wsTemp As Worksheet
Set wb = ThisWorkbook
Set wbTemp = Workbooks.Open("C:\MyCsv.Csv")
wbTemp.Sheets(1).Copy After:=wb.Sheets(wb.Sheets.Count)
Set wsTemp = ActiveSheet
wbTemp.Close SaveChanges:=False
End Sub
And now you can display that data in a multicolumn listbox.
Show that data in the multicolumn Listbox
I am taking an example of 3 Columns and up till tow 20. Change as applicable
Private Sub CommandButton1_Click()
Dim wb As Workbook, wbTemp As Workbook
Dim wsTemp As Worksheet
Set wb = ThisWorkbook
Set wbTemp = Workbooks.Open("C:\MyCsv.Csv")
wbTemp.Sheets(1).Copy After:=wb.Sheets(wb.Sheets.Count)
Set wsTemp = ActiveSheet
wbTemp.Close SaveChanges:=False
With ListBox1
.ColumnCount = 3
.ColumnWidths = "50;50;50"
.RowSource = wsTemp.Range("A1:C20").Address
End With
End Sub
SCREENSHOT
Delete the temp sheet in the Userform unload event
To Delete the temp sheet, declare the wsTemp on the top of the code so that you can access that in the UserForm_QueryClose event. See this complete example
Option Explicit
Dim wsTemp As Worksheet
Private Sub CommandButton1_Click()
Dim wb As Workbook, wbTemp As Workbook
Set wb = ThisWorkbook
Set wbTemp = Workbooks.Open("C:\MyCsv.Csv")
wbTemp.Sheets(1).Copy After:=wb.Sheets(wb.Sheets.Count)
Set wsTemp = ActiveSheet
wbTemp.Close SaveChanges:=False
With ListBox1
.ColumnCount = 3
.ColumnWidths = "50;50;50"
.RowSource = wsTemp.Range("A1:C20").Address
End With
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
HTH