Error with copy and paste function on script - excel

My script looks up the highest values in dailySht and pastes the values into a separate sheet recordSht, which usually works fine, but I sometimes get the error Object variable or With block variable not set. Below is the part of the code that returns the error.
Sub DailyBH()
Dim dailySht As Worksheet 'worksheet storing latest store activity
Dim recordSht As Worksheet 'worksheet to store the highest period of each day
Dim lColDaily As Integer ' Last column of data in the store activity sheet
Dim lCol As Integer ' Last column of data in the record sheet
Dim maxCustomerRng2 As Range ' Cell containing the highest number of customers
Dim maxCustomerCnt As Double ' value of highest customer count
Set dailySht = ThisWorkbook.Sheets("hourly KPI")
Set recordSht = ThisWorkbook.Sheets("#BH KPI")
With recordSht
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(58, 1), .Cells(58, lColDaily))), 2)
Set maxCustomerRng2 = .Range(.Cells(58, 1), .Cells(58, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)
.Cells(4, maxCustomerRng2.Column).Copy
recordSht.Cells(4, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(4, lCol + 1).PasteSpecial xlPasteFormats
.Cells(22, maxCustomerRng2.Column).Copy
recordSht.Cells(22, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(22, lCol + 1).PasteSpecial xlPasteFormats
.Cells(40, maxCustomerRng2.Column).Copy
recordSht.Cells(40, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(40, lCol + 1).PasteSpecial xlPasteFormats
.Cells(49, maxCustomerRng2.Column).Copy
recordSht.Cells(49, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(49, lCol + 1).PasteSpecial xlPasteFormats
.Cells(58, maxCustomerRng2.Column).Copy
recordSht.Cells(58, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(58, lCol + 1).PasteSpecial xlPasteFormats
End With
Set maxCustomerRng = Nothing
Set dailySht = Nothing
Set recordSht = Nothing
End Sub
Can someone please help me figure out that the problem is, as the code works (copies and pastes the correct values) on some cells and not others.

I recommend to use Match instead of Find and use the result of Max directly without converting it into Double to avoid floating point inaccuracies.
With dailySht
lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
Dim SearchRange As Range
Set SearchRange = .Range(.Cells(58, 1), .Cells(58, lColDaily))
Dim MaxCol As Long
On Error Resume Next 'next line throws error if nothing matched
MaxCol = Application.WorksheetFunction.Match(Application.WorksheetFunction.Max(SearchRange), SearchRange, 0)
On Error GoTo 0 're-enable error reporting !!!
If MaxCol = 0 Then
'nothing was found
Exit Sub
End If
.Cells(4, MaxCol).Copy
'your stuff here

Related

VBA Macro: Can't Paste Value

I have syntax that run smoothly. But, sadly it can't paste value. I've tried :
.PasteSpecial xlPasteValues
.PasteSpecial Paste:=xlPasteValues
This is my syntax...
Sub CopasToPenalty()
Dim LRSrc As Long, LRDest As Long, SrcRng As Range
With Sheets("RAW_DATA_PENALTY")
LRSrc = .Cells(.Rows.Count, 1).End(xlUp).Row
Set SrcRng = .Range("A2:F" & LRSrc)
End With
With Sheets("PENALTY")
LRDest = .Cells(.Rows.Count, 2).End(xlUp).Row
SrcRng.Copy .Cells(LRDest + 1, 2) 'NOT YET PASTE VALUE
End With
End Sub
Thank you.
SrcRng.Copy .Cells(LRDest + 1, 2) This will not paste just values. It will paste formats and other stuff as well.
Try the below.
Way One Using PasteSpecial
SrcRng.Copy
.Cells(LRDest + 1, 2).PasteSpecial xlPasteValues
Way Two Using Resize
Dim destRng As Range
Set destRng = .Cells(LRDest + 1, 2)
destRng.Resize(SrcRng.Rows.Count, SrcRng.Columns.Count).Value = SrcRng.Value

Cut and Paste Blocks of Data underneath first block using VBA

I have been trying to come up with/find a VBA code that copies blocks of data under my first block. Each block is 19 columns followed by a blank. The number of rows per block can vary.
See my screenshot below:
Therefore, I would like all my data continuous in the first columns A:S. Any help is highly appreciated.
I found the following code online, but this only pastes everything into the first column
Sub Column()
Dim iLastcol As Long
Dim iLastRow As Long
Dim jLastrow As Long
Dim ColNdx As Long
Dim ws As Worksheet
Dim myRng As Range
Dim ExcludeBlanks As Boolean
Dim mycell As Range
ExcludeBlanks = (MsgBox("Exclude Blanks", vbYesNo) = vbYes)
Set ws = ActiveSheet
iLastcol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Alldata").Delete
Application.DisplayAlerts = True
Sheets.Add.Name = "Alldata"
For ColNdx = 1 To iLastcol
iLastRow = ws.Cells(ws.Rows.Count, ColNdx).End(xlUp).Row
Set myRng = ws.Range(ws.Cells(1, ColNdx), _
ws.Cells(iLastRow, ColNdx))
If ExcludeBlanks Then
For Each mycell In myRng
If mycell.Value <> "" Then
jLastrow = Sheets("Alldata").Cells(Rows.Count, 1) _
.End(xlUp).Row
mycell.Copy
Sheets("Alldata").Cells(jLastrow + 1, 1) _
.PasteSpecial xlPasteValues
End If
Next mycell
Else
myRng.Copy
jLastrow = Sheets("Alldata").Cells(Rows.Count, 1) _
.End(xlUp).Row
mycell.Copy
Sheets("Alldata").Cells(jLastrow + 1, 1) _
.PasteSpecial xlPasteValues
End If
Next
Sheets("Alldata").Rows("1:1").EntireRow.Delete
ws.Activate
End Sub
Basic approach:
Sub Tester()
Dim c As Range, addr
Set c = ActiveSheet.Range("T1")
Do
Set c = c.End(xlToRight)
If c.Column = Columns.Count Then Exit Do
addr = c.Address 'strire the address since Cut will move c
c.CurrentRegion.Cut c.Parent.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Set c = ActiveSheet.Range(addr) '<< reset c
Loop
End Sub
This is a little more basic than #TimWilliams
With ThisWorkbook.Sheets("Alldata")
Dim lRow As Long, lCol As Long, cpyrng As Range
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 21 To lCol Step 20
If .Cells(1, i).Value <> "" And .Cells(1, i).Offset(, -1).Value = "" Then
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set cpyrng = .Cells(1, i).CurrentRegion
cpyrng.Cut
Sheets("Sheet2").Cells(lRow, 1).Offset(2).Insert Shift:=xlDown
End If
Next i
End With

How to reference cells in range?

Based on the text ("SNV") present in column L of the "HiddenSheet" worksheet, I would like to select and copy cells in columns 1 to 6 for all rows for which the "SNV" text is present in column L.
Then I would like to paste the values of the copied cells in the SNVReports worksheet.
Sub Macro2()
a = Worksheets("HiddenSheet").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To a
If Worksheets("HiddenSheet").Cells(i, 12).Value = "SNV" Then
Worksheets("HiddenSheet").Range(Cells(i, 1), Cells(i, 6)).Copy
Worksheets("SNVReports").Activate
b = Worksheets("SNVReports").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("SNVReports").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("HiddenSheet").Activate
End If
Next
Application.CutCopyMode = False
End Sub
I sometimes receive:
"Application-defined or object-defined error"
and it is apparently related to my range:
Worksheets("HiddenSheet").Range(Cells(i, 1), Cells(i, 6)).Copy
Your Cells(i,#) references aren't qualified. So if the SNVReports tab is active when the macro runs, it's confused as to what range you're talking about.
The whole code could do with a tidy-up:
Sub Macro2a()
Dim sourcesheet As Worksheet
Dim destsheet As Worksheet
Dim lastsourcerow as Long
Dim lastdestrow as Long
Dim i as Long
Set sourcesheet = Worksheets("HiddenSheet")
Set destsheet = Worksheets("SNVReports")
With sourcesheet
lastsourcerow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 1 To lastsourcerow
If .Cells(i, 12).Value = "SNV" Then
lastdestrow = destsheet.Cells(destsheet.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(i, 1), .Cells(i, 6)).Copy destsheet.Cells(lastdestrow + 1, 1)
End If
Next
End With
End Sub

Look for highest value in a row and copy in excel using VBA

I'm trying to search for the highest value in a row and copy specific cells, from the column (based on row with highest value) into a different sheet. The scipt was working fine, but recently it has not been selecting the highest value, and I can't figure it out. Any help would be greatly appreciated.
Sub DailyBH()
Dim dailySht As Worksheet 'worksheet storing latest store activity
Dim recordSht As Worksheet 'worksheet to store the highest period of each day
Dim lColDaily As Integer ' Last column of data in the store activity sheet
Dim lCol As Integer ' Last column of data in the record sheet
Dim maxCustomerRng2 As Range ' Cell containing the highest number of customers
Dim maxCustomerCnt As Double ' value of highest customer count
Set dailySht = ThisWorkbook.Sheets("hourly KPI")
Set recordSht = ThisWorkbook.Sheets("#BH KPI")
With recordSht
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
With dailySht
lColDaily = .Cells(59, .Columns.Count).End(xlToLeft).Column
maxCustomerCnt = Round(Application.Max(.Range(.Cells(59, 1), .Cells(59, lColDaily))), 2)
If Not maxCustomerRng2 Is Nothing Then
.Cells(32, maxCustomerRng2.Column).Copy
recordSht.Cells(32, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(32, lCol + 1).PasteSpecial xlPasteFormats
.Cells(14, maxCustomerRng2.Column).Copy
recordSht.Cells(14, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(14, lCol + 1).PasteSpecial xlPasteFormats
.Cells(59, maxCustomerRng2.Column).Copy
recordSht.Cells(59, lCol + 1).PasteSpecial xlPasteValues
recordSht.Cells(59, lCol + 1).PasteSpecial xlPasteFormats
End If
End With
Set maxCustomerRng = Nothing
Set dailySht = Nothing
Set recordSht = Nothing
End Sub

delete values of cells in the last column created

I have been working on a code that copy the last column and insert a new one, copying its formula and format. However, I need to delete the values of the cells from row 6 to 24 and then from 56 to 78 in the new column created. I couldn't find a way to refer to those cells in order to delete their values, could anyone help me on this? My code is below:
Sub Copy_Column()
Dim LastCol As Integer
With Worksheets("BO_Corretora")
LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column
Columns(LastCol).Copy
Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormats
Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormulas
End With
End Sub
You could use intersect. This keeps the rows to be deleted easy to read and you could store them in a constant for easy editing if required.
Sub Copy_Column()
Dim LastCol As Integer
Dim NewCol As String
With Worksheets("BO_Corretora")
LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column
.Columns(LastCol).Copy
.Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormats
.Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormulas
Intersect(.Columns(LastCol + 1), .Range("6:24,56:78")).ClearContents
End With
End Sub
Is this what you are trying?
Sub Copy_Column()
Dim LastCol As Integer
Dim NewCol As String
With Worksheets("BO_Corretora")
LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column
.Columns(LastCol).Copy
.Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormats
.Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormulas
'~~> Get the Column Letter of the new column
NewCol = Split(.Cells(, LastCol + 1).Address, "$")(1)
'~~> Range would be like Range("A6:A24,A56:A78")
.Range(NewCol & "6:" & NewCol & "24," & _
NewCol & "56:" & NewCol & "78").ClearContents
End With
End Sub

Resources