vba value special paste values all sheets same place in specific range - excel

I am beginner in Excel VBA
I need to to do some specific range to make value in my all worksheet at once
I am using this code but it's taking a long time and not working for all sheet at once
can anyone correct this code
Sub Saveasvalue()
'Updateby Extendoffice
Dim wsh As Worksheet
For Each wsh In ThisWorkbook.Worksheets
Range("B11:F40").Value = Range("B11:F40").Value
Next
Application.CutCopyMode = False
End Sub

Perhaps this is what you want to do:-
Sub Saveasvalue()
Dim Wsh As Worksheet
For Each Wsh In ThisWorkbook.Worksheets
If Not Wsh Is ActiveSheet Then
Wsh.Range("B11:F40").Value = Range("B11:F40").Value
End If
Next Wsh
End Sub
Watch out for the ActiveSheet in two ways.
If you don't specify a sheet the ActiveSheet is presumed by default. In your code Range("B11:F40").Value = Range("B11:F40").Value copies the range from the ActiveSheet to the ActiveSheet on each loop. Therefore nothing will ever change.
If you loop through all sheets in the workbook one of them will be the Activesheet. In the above code that sheet is excluded from the action.
So, the above code loops through all the sheets and copies the specified range from the active sheet to each called sheet. But it will omit that action when the called sheet is the ActiveSheet.

Related

How to copy an entire sheets values as static into another sheet using vba

I am trying to take a sheet like:
A3,B3,C3 are the sum of the 2 values above them.
Copy this entire sheet into another sheet with only static values ie the sum formulas are gone and 5,73,55 are just the values.
Public Sub CopyEntireSheetValues()
Sheets("Static Data").Range("A1:M100").Value = Sheets("MAIN").Range("A1:M100").Value
End Sub
This works but ideally, i wouldn't define this range and copy all values from one sheet to another
Here's commented code for how I'd accomplish this task via .UsedRange
Sub CopyEntireSheetValues()
'Define and declare workbook and worksheet variables
Dim wb As Workbook: Set wb = ThisWorkbook
Dim wsSrc As Worksheet: Set wsSrc = wb.Worksheets("Main")
Dim wsDst As Worksheet: Set wsDst = wb.Worksheets("Static Data")
'Set the range you want to copy values from
Dim rCopy As Range: Set rCopy = wsSrc.UsedRange
'Bring only the values to the destination sheet
wsDst.Range("A1").Resize(rCopy.Rows.Count, rCopy.Columns.Count).Value = rCopy.Value
End Sub
Two ways. The exact method to do what you want is use the pastespecial method which would be as follows:
Public Sub CopyEntireSheetValues()
Sheets("MAIN").Range("A1:C100").Copy
Sheets("Static Data").Range("A1").PasteSpecial (xlPasteValues)
End Sub
The other is as explained in comment just setting values.
Sheets("Static Data").Range("A1:C100").Value = Sheets("MAIN").Range("A1:C100").Value
In general the setting values is probably the best. The only time I've used the pasteValues is sometimes dates don't quite behave the same when using the second method, but the end result should be the same.
You updated your question, but this could be dynamic (I didn't test).
Public Sub CopyEntireSheetValues()
Sheets("MAIN").UsedRange.Copy
Sheets("Static Data").Range(Sheets("MAIN").UsedRange.Address).PasteSpecial (xlPasteValues)
End Sub

Why am I getting a Subscript Out of Range Error 9 when referencing another Workbook/Worksheet in VBA?

I have a Macro (within a Master Workbook) that is getting data from another Workbook/Worksheet using .value2.
I've tried different changes, within the code. I double checked that both workbooks are open. However, I keep getting the Subscript out of range (Error 9).
Sub NielsenScorecard_DataPaste()
Dim WbNielsenScorecard As Workbook
Set WbNielsenScorecard = Workbooks("Nielsen Scorecard_Template.xlsm")
TotalUS_DataPaste
End Sub
Sub TotalUS_DataPaste()
**Subscript out of range (Error 9)**
With Workbooks("Power Query - Meijer_Walmart_Total US xAOC.xlsm").Worksheets("PQTotalUS")
Dim Data(0) As Variant
'Copy Data Range
Data(0) = .Range(.Cells(.Rows.Count, "A").End(xlUp), "AA2").Value2
End With
'Worksheet Code Name within this Workbook
With wsTotalUS
Debug.Print wsTotalUS.Name
.AutoFilter.ShowAllData
.Range("A2:AA" & .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Row).ClearContents
With .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(UBound(Data(0)))
.Resize(ColumnSize:=UBound(Data(0), 2)).Value2 = Data(0)
End With
End With
End Sub
You can reference a sheet by its codename, however it is a different format and must be in ThisWorkbook. A drawback is that you cannot reference a sheet in another workbook by its codename. Worksheets("PQ Total US").Activate versus PQTotalUS.Activate. If your goal is to shorten the code and not have to repeat a long name, then another option is to do the following:
Dim wb1 as Workbook
Dim ws1 as Worksheet
Set wb1 = Workbooks("Power Query Meijer_Walmart_Total US xAOC.xlsm")
Set ws1 = wb1.Worksheets("PQ Total US")
With ws1
'Do something
End with

Copy from multiple sheets But not all into one master from last row

Still new to VBA. I’m trying to copy entire rows from multiple sheets (excluding a few). All sheets are set up the same with headers. I’ve tried using case as you can see below and copy to last row.
I’m not sure how or why I’ve gone wrong.
Any help is appreciated.
Sub copyall()
Dim wb As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Set wb = ActiveWorkbook 'Working in active workbook
Set trg = wb.Worksheets("all_rs_tenancies")
Application.ScreenUpdating = False
'We can start loop
Select Case Name
Case "data_supply", "Options", "all_rs_tenancies"
Case Else
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
sht.Range(sht.Range("A2:AA2"), sht.Range("A2:AA2").End(xlDown)).Copy
'Put data into the Master worksheet
trg.Range("A", Rows.Count).End(xlDown).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
End Select
Application.ScreenUpdating = True
End Sub
You're missing the code to loop through all worksheet; using For Each will set sht. Name needs a reference to the object (i.e. sht). I wouldn't use the Select Case statement to exclude the three sheets (and If would do) ... but have left it assuming you're going to do something else in that code. This at least will compile. You can debug from here if needed.
Option Explicit
Sub copyall()
Dim wb As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Set wb = ActiveWorkbook 'Working in active workbook
Set trg = wb.Worksheets("all_rs_tenancies")
Application.ScreenUpdating = False
'We can start loop
For Each sht In wb.Worksheets
Select Case sht.Name
Case "data_supply", "Options", "all_rs_tenancies"
Case Else
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
sht.Range(sht.Range("A2:AA2"), sht.Range("A2:AA2").End(xlDown)).Copy
'Put data into the Master worksheet
trg.Range("A", Rows.Count).End(xlDown).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
End Select
Next
Application.ScreenUpdating = True
End Sub

How to restrict VBA code to a specific worksheet?

I am trying to restrict the macro to a specific the Test! worksheet When I activate the macro pasted below and I am within the Test! worksheet, the macro works. However, when I try activating the macro on a different sheet, I receive an error. Any idea on what I need to do to modify the VBA code in order for it to be work any other worksheet within the workbook? Thanks
Sub Test ()
' Test Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Range ("Test!B1").Select
Selection.ClearContents
Range ("Test!B2").Select
Selection.ClearContents
Range ("Test!B3").Select
Selection.ClearContents
End Sub
You must reference the worksheet directly:
Dim ws as worksheet
set ws = Thisworkbook.Worksheets("Test")
with ws
.Range("B1:B3").ClearContents
end with
Other notes:
Don't use .Select or Selection. Simply operate on the objects directly.
You have String issues in your original code that would cause compile-time errors (fixed in my code)
You can clear the entire range in one line of code, rather than cell by cell
If the worksheet exists at compile-time in ThisWorkbook (i.e. the same file that's hosting the VBA code), then you don't need to retrieve the sheet at all - not by name, not by index.
VBA is already declaring a global Worksheet object variable for that sheet (and every sheet in ThisWorkbook). Verify the sheet's (Name) property; change it to TestSheet, then you can use TestSheet as an identifier anywhere in your code to refer to that sheet.
TestSheet.Range("B1:B3").ClearContents
And this code will work regardless of whether the user moved the sheet to another index/position in the workbook, or whether the user renamed the sheet's "tab" to something else.
ThisWorkbook.Worksheets(1).Range("B1:B3").ClearContents ' breaks if sheet is moved
ThisWorkbook.Worksheets("Test").Range("B1:B3").ClearContents ' breaks if sheet/tab is renamed
The Basics
If you want to learn something you should study versions 1 and 2, which are elaborate versions of versions 3 and 4 respectively.
Use Option Explicit to quickly find mistakes.
Use constants to be able to quickly change values.
Use object references to not have to type names on and on.
It is assumed that you will copy these codes in any module in the workbook where the worksheet 'Test' resides.
Option Explicit
Sub Test1()
Const cStrWs As String = "Test" 'Worksheet Name
Const cStrRng As String = "B1:B3" 'Range to 'DEL'
Dim oWs As Worksheet 'Worksheet Object
Dim oRng As Range 'Range Object
Set oWs = ThisWorkbook.Worksheets(cStrWs) 'Create a reference to the worksheet
Set oRng = oWs.Range(cStrRng) 'Create a reference to the range
oRng.ClearContents
End Sub
Sub Test2()
Const cStrWs As String = "Test" 'Worksheet Name
Const cStrCell As String = "B1" 'Cell to 'DEL'
Const cLngCells As Long = 3 'Number of cells
Dim oWs As Worksheet 'Worksheet Object
Dim oRng As Range 'Range Object
Set oWs = ThisWorkbook.Worksheets(cStrWs) 'Create a reference to the worksheet
Set oRng = oWs.Range(cStrCell) 'Create a reference to the cell range
oRng.Resize(3, 1).ClearContents
End Sub
Sub Test3()
ThisWorkbook.Worksheets("Test").Range("B1:B3").ClearContents
End Sub
Sub Test4()
ThisWorkbook.Worksheets("Test").Range("B1").Resize(3, 1).ClearContents
End Sub

Excel VBA - How to select colums in a specific sheet?

I need to select columns on a specific sheet. Somehow this is not working:
Dim ws As Worksheet
Set ws = Worksheets("Mysheet")
ws.Columns("A:S").Select
Selection.EntireColumn.AutoFit
And simple Columns("A:S").Selectdoesn't activate the sheet I need
I tested your code and it works fine as follows.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Mysheet")
ws.Columns("A:S").EntireColumn.AutoFit
End Sub
No need to Select anything, so I put the two statements together without the Select.
I added ThisWorkbook to (more) fully qualify your ws declaration. Make sure the worksheet Mysheet is in ThisWorkbook otherwise change that to state which workbook the sheet resides in.

Resources