Using VBA range commands with ActiveX buttons in other sheets - vba

while building up a user Interface I made use of ActiveX buttons in VBA2010. I wrote the Macros first, checked them, then copied them into the ActiceX button code.
The macro then crashes at a range command of another sheet. To test it I separated the commands:
Sheets("Data").Visible = xlSheetVisible
Sheets("Data").Select
Columns("O:O").Select ' The code crashes here
As a result I get a 1004 runtime error.
I found a similar topic which seems to be unsolved aswell.
Unable to Execute Macro With ActiveX Controls (Excel VBA)
Help is well appreciated.
Thanks in advance!

Select is not recommended.Modify your code to
Dim ws As Worksheet
Set ws = Sheets("Data")
ws.Visible = xlSheetVisible
ws.Select
With ws
.Columns("O:O").Select
End With

Related

Excel macro modification ( or maybe VBA ) to update the last included worksheet

Situation
Trying to copy a range of cells that include formulas from worksheet called "Sheet1" to the rest of other worksheets I found that I could do it performing "Fill Across Worksheets". It worked fine, so my next step was to record a Macro for it to be more efficient and worked just fine too.
The Problem
The problem is that when I include a new worksheet and run the Macro, the Macro does not consider the new worksheet so this last worksheet doesn't get updated.
I am including below the code created by the macro. In it I can see that it's including only the worksheets I have now in the workbook, so this is where I need help.
( My excel is in Spanish so when you read Ctrl+Mayus+Q, Mayus means Shiftkey )
Help
What I need is a way to modify this Macro so when it runs it will check and update all worksheets. Or, maybe it's because a Macro can't do this I may need a VBA code ? If this VBA is the way to resolve it, can you help me here with this ?
I appreciate all help
Thank you
Javier
Sub Macro2()
'
' Macro2 Macro
'
' Acceso directo: Ctrl+Mayús+Q
'
Range("A5:D12").Select
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
Sheets("Sheet1").Activate
ActiveWindow.SelectedSheets.FillAcrossSheets Range:=Selection,
Type:=xlAll
Sheets("Sheet1").Select
End Sub
This is a work around, concerning that you want all the worksheets to have the value of the first worksheet in range A5:D12:
Sub TestMe()
Dim ws As Worksheet
Dim selAddress As String
selAddress = "A5:D12"
For Each ws In Worksheets
'ws.Range(selAddress).Value2 = Worksheets(1).Range(selAddress).Value2
ws.Range(selAddress).Formula = Worksheets(1).Range(selAddress).Formula
Next ws
End Sub
See How to avoid using Select in Excel VBA.

Copy as Picture Returns Run Time Error 1004 everytime

I create a new excel file I create two tabs. I record a macro where I select a range of cells (Does not matter the size) on Sheet1 then select copy as picture. I then paste that image on Sheet 2. I stop the recording and delete the image and go back to Sheet 1. I then try to play the macro and i get Runtime Error 1004. Does Anyone know how to fix my excel?
I am happy to share my code but did not think it was necessary sense it seems to be more a core problem in excel.
Adding Code Below.
Sub CopyData(tRange As String, SheetName As String)
Worksheets(SheetName).Range(tRange).CopyPicture xlPrinter, xlPicture
End Sub
Sub Test()
Call CopyData("B2:I31", "Sheet1")
End Sub
Adding new Details
In my office we have 4 macs. All of them had started requesting updating to excel 16.11.1 . This had been done on two mine and One other computer. Today I tried my code on one of the machines that does not have 16.11.1 and it worked. I updated to 16.11.1 and it stopped working. I feel confident that 16.11.1 is the problem.
This will accomplish what you are trying to do; set your worksheets and ranges as variables. You do not need to add the constants for CopyPicture if you will be using the defaults, which are: xlScreen and xlPicture...
Sub CopyData()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim Rng1 As Range
Dim Rng2 As Range
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set Rng1 = ws1.Range("B2:I31")
Set Rng2 = ws2.Range("A1") 'change the cell ref as needed
Rng1.CopyPicture xlPrinter, xlPicture
ws2.Paste Destination:=Rng2
End Sub
The Issue was caused by office being update. I followed Microsofts instructions for downgrading and it solved the issue. Thanks to all those who responded and gave support.

Copy and combine sheets to a workbook

I need VBA code for Excel which: will be activated by a button in an empty workbook, loop through open workbooks, copies only sheets called "specificsheetname" from workbooks and pastes it into a new worksheet in the button activator workbook. So idea is that it will combine many worksheets from different workbooks into a one workbook. I tried this:
Sub workbookFetcher()
Dim book As Workbook, sheet, wsNew, wsCurr As Worksheet
Set wsCurr = ActiveSheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
Set wsNew = Sheets.Add(After:=wsCurr)
book.Worksheets("COOLING_RAW").Copy
Set wsNew = book.Worksheets("COOLING_RAW")
End If
Next sheet
Next book
End Sub
It kind of works but it pastes all the copied worksheets to a new workbook. That's not what I want, I want them to pasted in the same workbook.
As I said in my comment:
Sub workbookFetcher()
Dim book As Workbook, sheet as Worksheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
book.Worksheets("COOLING_RAW").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
Next sheet
Next book
End Sub
If you want it to be after the ActiveSheet and the ActiveSheet is in the middle of other sheets, you can still use your wsCurr and just increment the index.
If you've got Excel 2016, then the newly bundled PowerQuery functionality under the Get & Transform part of the ribbon is by far the best way to do this. Suggest you google something like PowerQuery Combine Workbooks and you'll see heaps of great tutorials showing you exactly what to do. It pretty much makes lots of VBA redundant, and it is childs-play to learn compared to VBA.
If you've got any other version of Excel from 2010 up and have admin rights on your machine, you can download and install PowerQuery from Microsoft's site...it's a free add-in
you don't need to iterate through each single workbook worksheets, while you just try to get the wanted sheet and copy it if it actually exists
moreover you want to avoid searching ThisWorkbook itsel for wanted worksheet, too!
Option Explicit
Sub workbookFetcher()
Dim book As Workbook, sht As Worksheet
For Each book In Workbooks
If book.Name <> ThisWorkbook.Name Then ' skip ThisWorkbook and avoid possible worksheet duplication
If GetWorksheet(book, "COOLING_RAW", sht) Then sht.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' if currently searched workbook has wanted worksheet then copy it to ThisWorkbook
End If
Next
End Sub
Function GetWorksheet(book As Workbook, shtName As String, sht As Worksheet) As Boolean
On Error Resume Next ' prevent subsequent statement possible error from stoping the function
Set sht = book.Worksheets(shtName) ' try getting the wanted sheet in the passed workbook
GetWorksheet = Not sht Is Nothing ' return 'True' if successfully got your sheet in the passed workbook
End Function

Excel Vba - Calling a sheet with a variable sheet name

I've been coding in VBA for some time, but this one has really stumped me.
I'm creating a workbook which creates technical certificates for machines. We have varying templates depending on the machine type and I am attempting to get my code to select the correct sheet from a user input and then populate the sheet. FYI these template sheets will be hidden and the user can only interact with the userforms.
Heres the code that is failing:
Machine = MachineType.Text '<-- input from userform, for example Machine = "Vertex 251"
Set wsCopy = ThisWorkbook.Sheets(Machine) '<--- select that machine's sheet
wsCopy.Copy '<--Run time Error 1004: Method copy of object_worksheet failed
I've tried numerous different types including just sheets(machine).copy or
Sheets(machine).activate
Activesheet.copy
but nothing has worked so far - I cannot tell if I am doing something fundamentally wrong.
Any help would be be appreciated.
Cheers.
You must unhide the sheet before copying it (at least to a new workbook as lturner notes) - you can then re-hide it
Dim shtTemplate as Worksheet, sheetWasHidden As Boolean
Set shtTemplate = ThisWorkbook.Sheets(Machine)
'handle the case where the sheet to be copied is Hidden
If shtTemplate.Visible = xlSheetHidden Then
shtTemplate.Visible = xlSheetVisible
sheetWasHidden = True
End If
shtTemplate.Copy
If sheetWasHidden Then shtTemplate.Visible = xlSheetHidden 're-hide if needed
When you have the worksheet object and use the Copy method, Excel seems to be making assumptions (or not) about where you want to put the new sheet. I pretty much always use the After option to define where the new sheet should go.
Option Explicit
Sub test()
Dim wsCopy As Worksheet
Set wsCopy = ActiveSheet
wsCopy.Copy After:=wsCopy
End Sub

Select worksheet in Excel add-in to run VBA macro

I have created an add-in using VBA which is a workbook with calculations. The add-in has a userform to extract relevant information from access database and populates the workbook. After the data is populated, calculations are performed in Sheet1. I need to paste the worksheet "Sheet1" from the add-in worksheet to a new workbook on running the add-in macro.
When I run the add-in however, the worksheet appears to be hidden so my data is not updating. I get this error:
" Run-time error '1004': Method 'Worksheets' of object '_Global' failed".
Can someone tell me how to work with an add-in which has a worksheet where the required calculations are performed?
The intriguing part is when I load the add-in after removing it from the list of add-ins in excel, it runs perfectly. But when I re-run the macro, the worksheet becomes hidden, so the same error appears. I am fairly new to VBA so any suggestions would be appreciated!
Edit
Code:
Private Sub OptionOK_Click() 'On selecting OK from userform
Dim ws1 As Worksheet
Sheets("Sheet1").Visible = True
Set ws1 = Worksheets("Sheet1")
'User Form Validation
If Trim(Me.cboData.value) = "" Then
Me.cboData.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copies data to given cell in excel
ws1.Range("A1").value = Me.cboData.value
'To copy selection from "Sheet1" into new workbook
Workbooks("myaddin.xlam").Sheets(1).Copy
End Sub
I get the error on ...> Sheets("Sheet1").Visible = True.
I just realized that I had to use "ThisWorkbook" in the add-in VBA code.
Set ws1 = ThisWorkbook.Sheets ("Sheet1")
VBA code within a workbook should use "ThisWorkbook" to reference to sheets or ranges inside the add-in.
If you know what sheet it is and you have access to the add-in code just make sure it's visible before the line that throws the error.
Sheets("Sheet3").Visible = True
I suspect you have another problem though because you can still reference a hidden sheet in code.
Are you sure this line is correct:
Workbooks("myaddin.xlam").Sheets(1).Copy
Before you were referencing the name of the sheet now your referencing the position of the sheet in the workbook.