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

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

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

Debugging Error while generating multiple templates from master sheet

I am trying to loop through a range of cells in a column and find non-blank cells to generate multiple template worksheets based on cell value in that specific range for further data transfer. however I am getting a debugging error, can anyone solve a problem? In the attached code "CETIN is the template worksheet
Sub generateSheet()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("CETIN")
Set sh2 = Sheets("Combine")
Range("A4:A600").Select
Selection.AutoFilter
ActiveSheet.Range("$A$5:$A$116").AutoFilter Field:=1, Criteria1:="<>"
Range("A4:A64").Select
Range("A4:A64").Select
Range("A4:A600").Select
Range("A116").Activate
Selection.Copy
Range("AT5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.AutoFilter
For Each c In sh2.Range("AT5", sh2.Cells(Rows.Count, 46).End(xlUp))
sh1.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
Next
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

COPY SOURCEDATA TO 2 DIFFERENT SHEETS WITH VBA

I'm trying to get the sourcedata from a range of cells on an "input sheet" for data entry to copy this information to 2 different sheets. After this copy, I will use one sheet to leave for all data, and manipulate the other sheet for tracking.
This is what I've gotten so far, but I get an error that an object is required.
Sub CopySource()
Dim NextRow As Range
With Sheets(Array("MasterData", "MainData")).Select
Set NextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
End With
Range("SourceData").Copy
NextRow.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
Just do each copy individually, then:
Sub CopySource()
Range("SourceData").Copy
Sheets("MasterData").Cells(Sheets("MasterData").Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Sheets("MainData").Cells(Sheets("MainData").Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Application.CutCopyMode = False
End Sub
Possible fix for the original code:
Sub CopySource()
Dim NextRow() As Range 'This makes an array of ranges
With Sheets(Array("MasterData", "MainData")).Select
'not sure if this will now populate multiple elements of NextRow() with multiple
'ranges, one from each sheet, above
Set NextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
End With
Range("SourceData").Copy
NextRow(0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
NextRow(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
Not sure if that'll work, but it might be worth a try

Copy values from one worksheet to another

How can I copy some data from one worksheet to another?
I tried this code, but get an error:
Private Sub CommandButton2_Click()
Sheets("Gas Opt").Select
Range("A1:A3").Select
Selection.Copy
Sheets("ExportToPPServer").Select
Cells(3, AColumn).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
LFound = True
MsgBox "Data coped."
End Sub
Error:
Select method of Range class failed.
Something like this should work:
Private Sub CommandButton2_Click()
Dim copyRng As Range, targetRng As Range
Set copyRng = Worksheets("Gas Opt").Range("A1:A3")
Set targetRng = Worksheets("ExportToPPServer").Cells(3, AColumn)
copyRng.Copy
targetRng.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
LFound = True
MsgBox "Data coped."
End Sub
How does this look?
Sub x()
Sheets("Gas Opt").Select
Range("A1:A3").Select
Selection.Copy
ActiveWorkbook.Sheets("ExportToPPServer").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub
Edit
Is your Control button on a different sheet than "Gas Opt"? That would explain it. Try this:
Sub x()
Sheets("Sheet2").Range("A1:A3").Copy
ActiveWorkbook.Sheets("Sheet3").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub
You need to activate the sheet, else you cannot select cells in it.
Sheets("ExportToPPServer").Activate ' Instead of select

Resources