I have a combobox with 9 sheetnames.
When I select a name the macro finds the sheet.
However, I cant copy a range to my active worksheet,
its gives the error 438 on the row "wb.blad.CodeName.Range("A1:J80").Select"
All the worksheets have a codename.
I can't find the solution. Here is mij code;
Private Sub discipline_Change()
blad = databaas.discipline.Text
Set wb = Workbooks.Open("C:\Users\Genius\Desktop\db.xlsx")
'wb.Worksheets.blad.Range("B1:J80").Copy sh2.Range("B1")
For Each ws In wb.Worksheets
If ws.CodeName = blad Then
wb.blad.CodeName.Range("A1:J80").Select
Selection.Copy
sh2.Range("A1").Select
sh2.Paste
End If
Next
ActiveWindow.Close
Unload Me
End Sub
wb.blad.CodeName.Range("A1:J80").Select doesn't make sense.
Use ws.Range("A1:J80").Select
Related
I need to copy an image from my invoice template worksheet to another worksheet with variable names. For example, the name of the sheet could be "03-000008" or "04-000005" or any other name. This problem would be easy to solve if the sheets had the same name, but since they are variable, I am struggling. Any help would be much appreciated! Thank you in advance!
Tim suggested that I add the code I am working with (thanks Tim!) Here is the code that almost works, but instead of pasting the image to my new, active invoice sheet, it pastes it right on the template itself.
Sub image()
With ActiveSheet
Set i = Sheets("Template")
Set e = ActiveSheet
i.Shapes.Range(Array("Picture 4")).Select
Selection.Copy
e.Range("b1:b4").Select
ActiveSheet.Paste
End With
End Sub
Try this:
Sub image()
Dim ws As Worksheet
ThisWorkbook.Worksheets("Template").Shapes("Picture 4").Copy
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "##-######" Then 'check matches pattern [2digits-6digits]
ws.Paste Destination:=ws.Range("B1")
MsgBox "Pasted to " & ws.Name
Exit For 'no need to check further
End If
Next ws
End Sub
If all you need to do is copy the image from Template to the activesheet then:
Sub CopyLogo()
ThisWorkbook.Worksheets("Template").Shapes("Picture 4").Copy
ActiveSheet.Paste Destination:=ActiveSheet.Range("B1")
End Sub
I'm using a macro to make a copy of the active sheet, and rename it to whatever the value of cell 'C2' is. The only problem is, that when it copies the sheet, it somehow removes the form buttons from the top of my worksheet and replaces them with the code =$c$2 in cell 'AF'.
As far as i can see from the VBA code there is nothing that refers to the cell 'AF'. Can anyone tell me why it's doing this ?
Sub Copy_Rename()
Dim shtName As String
shtName = ActiveSheet.Name
ActiveSheet.Copy before:=ActiveSheet
ActiveSheet.Name = Range("C2").Value
Sheets(shtName).Activate
End Sub
Try this:
Sub Copy_Rename()
Dim sht As Worksheet
Set sht = ActiveSheet
Application.CopyObjectsWithCells = True '<< to also copy objects not just cell contents etc
sht.Copy before:=sht
'Get the just-created sheet
With Sheets(sht.Index - 1)
.Name = sht.Range("C2").Value
.Activate
End With
End Sub
I am using an excel Workbook for programtical generation. Once the workbook is created few of the sheets are having required data and few are blank with default templates only.
I need to delete all sheets having default templates (means no data). I can check specific cell to identify this however need to know how to check for all sheets and then delete sheets one by one.
I am having this piece of code:
Sub TestCellA1()
'Test if the value is cell D22 is blank/empty
If IsEmpty(Range("D22").Value) = True Then
MsgBox "Cell A1 is empty"
End If
End Sub
Try this:
Sub DeleteEmptySheets()
Dim i As Long, ws As Worksheet
' we don't want alerts about confirmation of deleting of worksheet
Application.DisplayAlerts = False
For i = Worksheets.Count To 1 Step -1
Set ws = Worksheets(i)
' check if cell D22 is empty
If IsEmpty(ws.Range("D22")) Then
Sheets(i).Delete
End If
Next
' turn alerts back on
Application.DisplayAlerts = True
End Sub
An alternative implementation using For-Each:
Sub deleteSheets()
Dim wb As Workbook
Dim sht As Worksheet
Set wb = Workbooks("Name of your Workbook")
'Set wb = ThisWorkbook You can use this if the code is in the workbook you want to work with
Application.DisplayAlerts = False 'skip the warning message, the sheets will be deleted without confirmation by the user.
For Each sht In wb.Worksheets
If IsEmpty(sht.Range("D22")) And wb.Worksheets.Count > 1 then
sht.Delete
End If
Next sht
Application.DisplayAlerts = True
End Sub
This mainly serves as a demonstration pf how you can easily loop through worksheets.
As suggested in the comments below by #Darren Bartrup-Cook , the logic according to which the sheets are deleted can and should be modified to not only suit your purposes but to also include safeguards.
Making sure there's always at least one worksheet in the workbook is one of them. This can be ensured in a multitude of ways. I updated my answer to implement one these.
Granted that I'm a newby.
I need to copy a specific cells area ("B6:C36") from a single worksheet (named "FILE MASTER") to all the other worksheets within the same workbook.
After that, I need to assign this brand new macro to a Button existing in the file master worksheet (therefore this macro has to have a name/sub otherwise I cannot assign it to a Button).
Having said that I tried to create a macro by using the recording feature of MS Excel, and it works. But it has a serious weakness: this automatedly encoding process has used/enunciated the name of every single worksheets in the source code. So if I add a new worksheet, this macro won't work correctly anymore.
Hope to have been enough clear
Thank you in advance to everybody.
You could change the code and try the below:
Option Explicit
Sub CopyYes()
Dim ws As Worksheet
With ThisWorkbook
'Copy the range
.Worksheets("FILE MASTER").Range("B6:C36").Copy
'Loop sheets
For Each ws In .Worksheets
With ws
'Avoid FILE MASTER
If .Name <> "FILE MASTER" Then
'Paste only values in A1 of each sheet
.Range("A1").PasteSpecial xlPasteValues
End If
End With
Next ws
End With
End Sub
Option Explicit
Sub CopyYes()
Dim ws As Worksheet
With ThisWorkbook
'Copy the range
.Worksheets("FILE MASTER").Range("B6:C36").Copy
'Loop sheets
For Each ws In .Worksheets
With ws
'Avoid FILE MASTER
If .Name <> "FILE MASTER" Then
'Paste values and formats in B6:C36 of each sheet
.Range("B6:C36").PasteSpecial xlPasteValues
.Range("B6:C36").PasteSpecial xlPasteFormats
End If
End With
Next ws
End With
End Sub
Is there something wrong with my formula? I'm trying to use a formula to bring me to gotoreference(I.e f5) the cell to verify that cells are indeed right before proceeding or either msgbox prompting to proceed or that the workbook/worksheet cannot be found. Also sometimes the person leaves blank because it is quarterly data, I would like it to autoextract latest data (farthest) column.
I've two workbooks: one is my current workbook (Currentworkbook.xlsx) that I'm running the macro on. The other is Jedata.xlsx of 'Mysheettab' and few other workbooks not listed here, will be feeding information into Currentworkbook.xlsx of 'Sheet1'.
Option explicit
Sub Macro3()
' Macro3 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Dim wb As Workbook
Dim ws As Worksheet
On Error Resume Next
Set wb = ActiveWorkbook("Jedata")
Set ws = ActiveWorkbook.Sheets("Mysheettab")
On Error GoTo 0
If ws Is Nothing Then
MsgBox "Data sheet not found"
Else
Activate.Windows ("wb")
Sheets("ws").Select
Application.Goto Reference:=Range("AG28:AG32").Select
Selection.Copy
Windows("Currentworkbook").Activate
Selection.Copy
Range("H10:H14").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
I can see lot of errors in your code.
First things first. You avoid the use of .Activate/.Select. INTERESTING READ
Next regarding Activate.Windows ("wb"). Anything between the " will be considered as a string. I think you wanted to try
wb.Activate
But like I mentioned, you should avoid the use of .Activate/.Select. Your code can be written as (UNTESTED)
Sub Macro3()
Dim wb As Workbook, thiswb As Workbook
Dim ws As Worksheet, thisws As Worksheet
On Error GoTo Whoa
Set thiswb = thisowrkbook
Set thisws = thiswb.ActiveSheet
Set wb = Workbooks("Jedata")
Set ws = wb.Sheets("Mysheettab")
ws.Range("AG28:AG32").Copy thisws.Range("H10")
Application.CutCopyMode = False
Exit Sub
Whoa:
MsgBox Err.Description
End Sub