1004 error (select method of range class failed) - excel

I am currently working on a tool, getting some data but not the formula or other format. Just the plain text. However it gives me the error of 1004.
here's my code:
Sub WBS()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("totalcosts.xlsm").Worksheets(3).Range("A3:A300")
Set targetColumn = Workbooks("Backing sheet.xlsm").Worksheets(2).Range("C6:C300")
sourceColumn.Copy
targetColumn.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Call Resource_Name
End Sub
It always give me error on the targetcolumn.select
But when I use this code to copy and paste on the line,
*"sourceColumn.Copy destination=targetcolumn"*
It paste the data that I need. However it copies the formula instead of the value.

Cut your destination down to a single cell (top-left) and let the paste operation fill the remainder.
Sub WBS()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("totalcosts.xlsm").Worksheets(3).Range("A3:A300")
Set targetColumn = Workbooks("Backing sheet.xlsm").Worksheets(2).Range("C6")
sourceColumn.Copy
targetColumn.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Call Resource_Name
End Sub
Use the Range.PasteSpecial method directly off the target range. There is no need to Range .Select method it first¹.
Since you are only transferring the Range.Value property, a direct value transfer would be the better option.
Sub WBS()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("totalcosts.xlsm").Worksheets(3).Range("A3:A300")
Set targetColumn = Workbooks("Backing sheet.xlsm").Worksheets(2).Range("C6")
with sourceColumn
targetColumn.Resize(.Rows.Count, .Columns.Count) = .Value
end with
Call Resource_Name
End Sub
¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Related

How to find next blank cell in column after autofilter

I am trying to find the next blank cell in column N in order to select and copy the vale in the same row but in column I after autofiltering column B.
My code seems to find the next blank cell but doesn't take the filter into consideration finding the blank cell in rows not included in the filter.
Hope this makes sense
Edit: Apologies about the shite code, I typically recycle and string it all together etc.
Sub Replanning_Lasers()
startofloop:
Dim Orderbook As String
Dim Supply As String
Orderbook = Sheet25.Range("C14")
Supply = Sheet25.Range("D14")
If Orderbook > Supply Then GoTo endofloop
***Sheets("List").Select
Range("$B$4:$BN$1533").AutoFilter Field:=3, Criteria1:="LASER"
NextFree = Range("N5:" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).row
Range("I" & NextFree).Select***
Selection.Copy
Sheets("Planning").Select
Range("C9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Call BUT_Planning_reset
Call BUT_Planning_Find_First
Range("M9").Select
Selection.Copy
Sheets("List").Select
Range("N" & NextFree).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
GoTo startofloop
endofloop:
End Sub
Thanks
Maybe try something like this:
Sub Replanning_Lasers()
Dim wsList As Worksheet, wsPlan As Worksheet, rngData As Range, rngBlanks As Range
Set wsList = ThisWorkbook.Sheets("List") 'Use variables for worksheets
Set wsPlan = ThisWorkbook.Sheets("Planning") ' to avoid repetition
Set rngData = wsList.Range("B4:BN1533")
rngData.AutoFilter Field:=3, Criteria1:="LASER"
On Error Resume Next 'ignore error if no visible blank cells
'find any visible blank cells in ColN of the filtered data
Set rngBlanks = rngData.EntireRow.Columns("N"). _
SpecialCells(xlCellTypeVisible). _
SpecialCells(xlCellTypeBlanks)
On Error GoTo 0 'stop ignoring errors
If rngBlanks Is Nothing Then Exit Sub 'no visible blanks
For Each c In rngBlanks.Cells 'process each blank cell in turn
If Sheet25.Range("C14") > Sheet25.Range("D14") Then
Msgbox "supply breach"
Exit For 'Orderbook > Supply ?
End If
wsPlan.Range("C9").Value = c.EntireRow.Columns("I").Value 'no need for copy/pastespecial
BUT_Planning_reset 'use of Call is deprecated...
BUT_Planning_Find_First
c.EntireRow.Columns("N").Value = wsPlan.Range("M9").Value
Next c
End Sub

How do I run a column range through a table and paste it in another column using VBA?

So I'm trying to run a column through a table in Excel using VBA. I then want to copy the result and paste in another column. I've gotten it to work for one cell, however, when I try to loop the code, it just pastes the same thing in every cell in the range I want it to paste in. How do I make it so that when it loops, it only pastes in the single cell vs. the entire range? My code is below.
Sub Test1()
'
' Test1 Macro
'
'
Dim rng As Range, cell As Range
Set rng = Range("C16:C20")
For Each cell In rng
Dim rng2 As Range, cell2 As Range
Set rng2 = Range("G16:G20")
For Each cell2 In rng2
cell.Select
Selection.Copy
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D12").Select
Application.CutCopyMode = False
Selection.Copy
rng2.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Tranpose:=False
'ActiveCell.Offset(1, 0).Select
Next cell2
Next cell
End Sub
Thanks!
Guessing you want something like this:
Sub Test1()
Dim rng As Range, cell As Range, ws As Worksheet
Set ws = ActiveSheet
Set rng = ws.Range("C16:C20")
For Each cell In rng.Cells
ws.Range("B4").value = cell.Value
cell.offset(0, 4).value = ws.Range("D12").Value 'populate in Col G
Next cell
End Sub
Note there's typically no need to select/activate anything in excel (though the macro recorder does that a lot). Worth reviewing this: How to avoid using Select in Excel VBA
Likewise if you need to transfer values between cells you can do that directly without copy/paste.

Run the macro even if the last 3 letters is renamed

Good day,
May I ask if it is possible to run the macro even if some letters or characters on the title of the workbook I am working with is renamed?
this is the code I am working with:
Sub WBS()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("Total cost.xlsm").Worksheets(3).Range("A3:A300")
Set targetColumn = Workbooks("backing sheet (Jan).xlsm").Worksheets(2).Range("D6:D300")
sourceColumn.Copy
targetColumn.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Call Resource_Name
End Sub
what I really want to do is run the macro even if the name of the workbook is changed. as you can see the value I am copying is pasted on "backing sheet (Jan).xlsm" I want to change the name to "backing sheet (Feb).xlsm" and I know it wont run because the target workbook does not exist technically.
is there any possible way to deal with this?
Create a named range in your program with the date abbreviation - say called monthAbbr. Edit this every month. Then, change the VBA code to
Sub WBS()
Dim monthAbbr As String
monthAbbr = Range("monthAbbr").Value
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("Total cost.xlsm").Worksheets(3).Range("A3:A300")
Set targetColumn = Workbooks("backing sheet " & monthAbbr & ".xlsm").Worksheets(2).Range("D6:D300")
sourceColumn.Copy
targetColumn.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Call Resource_Name
End Sub
I did it :). why did I make this code complicated.
incase others want to know the answer please see the code below.
Sub WBS()
Dim monthAbbr As String
monthAbbr = Range("monthAbbr").Value
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("Total cost.xlsm").Worksheets(3).Range("A3:A300")
Set targetColumn = ThisWorkbook.Worksheets(2).Range("D6:D300")
sourceColumn.Copy
targetColumn.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Call Resource_Name
I forgot that there is a ThisWorkbook code. I can even change the whole name of my file :)

convert column of a sheet to value only (remove formulas)

Could you please help me out with below formula? It gives object defined or app defined error. Thanks a lot.
Sub cellstovalues()
Sheets("Parsing").Select
Columns("B:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
As an alternative, you can simply assign the values to the Value2 (or Value) property of the range:
Sub cellstovalues()
With Sheets("Parsing")
With Intersect(.Range("B:B"), .UsedRange)
.Value2 = .Value2
End With
End With
End Sub
or for a specific range:
Sub cellstovalues()
With Sheets("Parsing").Range("B1:C10")
.Value2 = .Value2
End With
End Sub
Your code is having issues from you selecting the entire column B. Try using this. This should find the last used cell in your column, then copy and paste to convert the formulas to values like you want.
Sub cellstovalues()
Dim ws As Worksheet
Dim LastRow As Integer
Dim rng As Range
Set ws = ThisWorkbook.Sheets("Parsing")
With ws
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set rng = .Range(.Cells(1, "B"), .Cells(LastRow, "B"))
End With
rng.Copy
rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

Copy and Paste to next available column

I'm trying to track weekly quantities I have in my spread sheet. So far I've made a macro to copy and paste the info where I need it. But it will only paste it to the spot I chose while recording the macro. I'd like it to paste the info into the next available column.
I'd also like to schedule the macro to run once a week on Friday morning.
Macro I'm using now.
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Range("G4:G33").Select
Selection.Copy
Range("B35").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I've tried putting & lastrow into the range, but it gets a compile error. Any help would be greatly appreciated.
At first sight maybe slightly more complex, but in a way a more pretty way of tackling the movement of values is to avoid using the clipboard with code like this:
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Dim targetRng As Excel.Range
Dim destRng As Excel.Range
Set targetRng = Range("G4:G33")
Dim lc As Long
With Excel.ThisWorkbook.Sheets("Sheet1")
lc = .Cells(35, .Columns.Count).End(Excel.xlToLeft).Column
Set destRng = .Range(.Cells(35, lc), .Cells(35, lc)).Offset(0, 1).Resize(targetRng.Rows.Count, targetRng.Columns.Count)
destRng.Value = targetRng.Value
End With
End Sub
The above can be simplified to the following so you don't need to worry about using the last row variable:
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Dim targetRng As Excel.Range
Dim destRng As Excel.Range
Set targetRng = Range("G4:G33")
With Excel.ThisWorkbook.Sheets("Sheet1")
Set destRng = .Cells(35, .Columns.Count).End(Excel.xlToLeft).Offset(0, 1).Resize(targetRng.Rows.Count, targetRng.Columns.Count)
destRng.Value = targetRng.Value
End With
End Sub
You can work out the column number of the last column like this:
Sub CopyPaste()
'
' CopyPaste Macro
'
Dim lastCol As Long
' this finds the number of the last column
lastCol = Cells(35, Columns.Count).End(xlToLeft).Column
Range("G4:G33").Copy
' Range("B35").Select
' no need to select. paste into the cell in row 35, one to the right of the last column
Cells(35, lastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
You could also add the 1 right in the lastCol definition, like
lastCol = Cells(35, Columns.Count).End(xlToLeft).Column + 1
Range("G4:G33").Copy
Cells(35, lastCol).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For scheduling the macro look at these two questions here and here

Resources