Copy data as text in Excel with VBA [duplicate] - excel

I'm trying to copy entire column in sheetA to Sheet B. sheetA column has values formed with formuls. I'm copying SheetA column values only using xlPasteValues. But it is not paste the values to another sheetB. The column in sheetB is empty.
My VBA Code
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
With Sheets("SheetA")
lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
.Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues
End With
Next
Application.CutCopyMode = False
End Sub

If you are wanting to just copy the whole column, you can simplify the code a lot by doing something like this:
Sub CopyCol()
Sheets("Sheet1").Columns(1).Copy
Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues
End Sub
Or
Sub CopyCol()
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
End Sub
Or if you want to keep the loop
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
Sheets("Sheet1").Columns(arr1(i)).Copy
Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub

since you only want values copied, you can pass the values of arr1 directly to arr2 and avoid copy/paste.
code inside the For loop, inside the With block, after lastrow calculation:
Sheets("SheetB").Range(arr2(i) & firstrowDB).Resize(lastrow).Value = .Range(arr1(i) & 1).Resize(lastrow).Value

Personally, I would shorten it a touch too if all you need is the columns:
For i = LBound(arr1) To UBound(arr1)
Sheets("SheetA").Columns(arr1(i)).Copy
Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next
as from this code snippet, there isnt much point in lastrow or firstrowDB

you may use this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

You may use this too
Sub CopyPaste()
Sheet1.Range("A:A").Copy
Sheet2.Activate
col = 1
Do Until Sheet2.Cells(1, col) = ""
col = col + 1
Loop
Sheet2.Cells(1, col).PasteSpecial xlPasteValues
End Sub

I've had this problem before too and I think I found the answer.
If you are using a button to run the macro, it is likely linked to a different macro, perhaps a save as version of what you are currently working in and you might not even realize it. Try running the macro directly from VBA (F5) instead of running it with the button. My guess is that will work. You just have to reassign the macro on the button to the one you actually want to run.

selection=selection.values
this do things at a very fast way.

Related

Copying values with concatenate command via Excel macro not possible [duplicate]

I'm trying to copy entire column in sheetA to Sheet B. sheetA column has values formed with formuls. I'm copying SheetA column values only using xlPasteValues. But it is not paste the values to another sheetB. The column in sheetB is empty.
My VBA Code
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
With Sheets("SheetA")
lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
.Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues
End With
Next
Application.CutCopyMode = False
End Sub
If you are wanting to just copy the whole column, you can simplify the code a lot by doing something like this:
Sub CopyCol()
Sheets("Sheet1").Columns(1).Copy
Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues
End Sub
Or
Sub CopyCol()
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
End Sub
Or if you want to keep the loop
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
Sheets("Sheet1").Columns(arr1(i)).Copy
Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
since you only want values copied, you can pass the values of arr1 directly to arr2 and avoid copy/paste.
code inside the For loop, inside the With block, after lastrow calculation:
Sheets("SheetB").Range(arr2(i) & firstrowDB).Resize(lastrow).Value = .Range(arr1(i) & 1).Resize(lastrow).Value
Personally, I would shorten it a touch too if all you need is the columns:
For i = LBound(arr1) To UBound(arr1)
Sheets("SheetA").Columns(arr1(i)).Copy
Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next
as from this code snippet, there isnt much point in lastrow or firstrowDB
you may use this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
You may use this too
Sub CopyPaste()
Sheet1.Range("A:A").Copy
Sheet2.Activate
col = 1
Do Until Sheet2.Cells(1, col) = ""
col = col + 1
Loop
Sheet2.Cells(1, col).PasteSpecial xlPasteValues
End Sub
I've had this problem before too and I think I found the answer.
If you are using a button to run the macro, it is likely linked to a different macro, perhaps a save as version of what you are currently working in and you might not even realize it. Try running the macro directly from VBA (F5) instead of running it with the button. My guess is that will work. You just have to reassign the macro on the button to the one you actually want to run.
selection=selection.values
this do things at a very fast way.

VBA Copy and Paste Loop - .PasteSpecial xl:=PasteValues Issue [duplicate]

I'm trying to copy entire column in sheetA to Sheet B. sheetA column has values formed with formuls. I'm copying SheetA column values only using xlPasteValues. But it is not paste the values to another sheetB. The column in sheetB is empty.
My VBA Code
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
With Sheets("SheetA")
lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
.Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues
End With
Next
Application.CutCopyMode = False
End Sub
If you are wanting to just copy the whole column, you can simplify the code a lot by doing something like this:
Sub CopyCol()
Sheets("Sheet1").Columns(1).Copy
Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues
End Sub
Or
Sub CopyCol()
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
End Sub
Or if you want to keep the loop
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
Sheets("Sheet1").Columns(arr1(i)).Copy
Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
since you only want values copied, you can pass the values of arr1 directly to arr2 and avoid copy/paste.
code inside the For loop, inside the With block, after lastrow calculation:
Sheets("SheetB").Range(arr2(i) & firstrowDB).Resize(lastrow).Value = .Range(arr1(i) & 1).Resize(lastrow).Value
Personally, I would shorten it a touch too if all you need is the columns:
For i = LBound(arr1) To UBound(arr1)
Sheets("SheetA").Columns(arr1(i)).Copy
Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next
as from this code snippet, there isnt much point in lastrow or firstrowDB
you may use this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
You may use this too
Sub CopyPaste()
Sheet1.Range("A:A").Copy
Sheet2.Activate
col = 1
Do Until Sheet2.Cells(1, col) = ""
col = col + 1
Loop
Sheet2.Cells(1, col).PasteSpecial xlPasteValues
End Sub
I've had this problem before too and I think I found the answer.
If you are using a button to run the macro, it is likely linked to a different macro, perhaps a save as version of what you are currently working in and you might not even realize it. Try running the macro directly from VBA (F5) instead of running it with the button. My guess is that will work. You just have to reassign the macro on the button to the one you actually want to run.
selection=selection.values
this do things at a very fast way.

Excel VBA copy multiple cells from one sheet and paste special to another sheet [duplicate]

I'm trying to copy entire column in sheetA to Sheet B. sheetA column has values formed with formuls. I'm copying SheetA column values only using xlPasteValues. But it is not paste the values to another sheetB. The column in sheetB is empty.
My VBA Code
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
With Sheets("SheetA")
lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
.Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues
End With
Next
Application.CutCopyMode = False
End Sub
If you are wanting to just copy the whole column, you can simplify the code a lot by doing something like this:
Sub CopyCol()
Sheets("Sheet1").Columns(1).Copy
Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues
End Sub
Or
Sub CopyCol()
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
End Sub
Or if you want to keep the loop
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
Sheets("Sheet1").Columns(arr1(i)).Copy
Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
since you only want values copied, you can pass the values of arr1 directly to arr2 and avoid copy/paste.
code inside the For loop, inside the With block, after lastrow calculation:
Sheets("SheetB").Range(arr2(i) & firstrowDB).Resize(lastrow).Value = .Range(arr1(i) & 1).Resize(lastrow).Value
Personally, I would shorten it a touch too if all you need is the columns:
For i = LBound(arr1) To UBound(arr1)
Sheets("SheetA").Columns(arr1(i)).Copy
Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next
as from this code snippet, there isnt much point in lastrow or firstrowDB
you may use this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
You may use this too
Sub CopyPaste()
Sheet1.Range("A:A").Copy
Sheet2.Activate
col = 1
Do Until Sheet2.Cells(1, col) = ""
col = col + 1
Loop
Sheet2.Cells(1, col).PasteSpecial xlPasteValues
End Sub
I've had this problem before too and I think I found the answer.
If you are using a button to run the macro, it is likely linked to a different macro, perhaps a save as version of what you are currently working in and you might not even realize it. Try running the macro directly from VBA (F5) instead of running it with the button. My guess is that will work. You just have to reassign the macro on the button to the one you actually want to run.
selection=selection.values
this do things at a very fast way.

Excel - Copy Range in ThisWorkbook and paste special in closed workbook [duplicate]

I'm trying to copy entire column in sheetA to Sheet B. sheetA column has values formed with formuls. I'm copying SheetA column values only using xlPasteValues. But it is not paste the values to another sheetB. The column in sheetB is empty.
My VBA Code
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
With Sheets("SheetA")
lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
.Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues
End With
Next
Application.CutCopyMode = False
End Sub
If you are wanting to just copy the whole column, you can simplify the code a lot by doing something like this:
Sub CopyCol()
Sheets("Sheet1").Columns(1).Copy
Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues
End Sub
Or
Sub CopyCol()
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
End Sub
Or if you want to keep the loop
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
Sheets("Sheet1").Columns(arr1(i)).Copy
Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
since you only want values copied, you can pass the values of arr1 directly to arr2 and avoid copy/paste.
code inside the For loop, inside the With block, after lastrow calculation:
Sheets("SheetB").Range(arr2(i) & firstrowDB).Resize(lastrow).Value = .Range(arr1(i) & 1).Resize(lastrow).Value
Personally, I would shorten it a touch too if all you need is the columns:
For i = LBound(arr1) To UBound(arr1)
Sheets("SheetA").Columns(arr1(i)).Copy
Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next
as from this code snippet, there isnt much point in lastrow or firstrowDB
you may use this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
You may use this too
Sub CopyPaste()
Sheet1.Range("A:A").Copy
Sheet2.Activate
col = 1
Do Until Sheet2.Cells(1, col) = ""
col = col + 1
Loop
Sheet2.Cells(1, col).PasteSpecial xlPasteValues
End Sub
I've had this problem before too and I think I found the answer.
If you are using a button to run the macro, it is likely linked to a different macro, perhaps a save as version of what you are currently working in and you might not even realize it. Try running the macro directly from VBA (F5) instead of running it with the button. My guess is that will work. You just have to reassign the macro on the button to the one you actually want to run.
selection=selection.values
this do things at a very fast way.

VBA: How come pausing my macro makes it faster?

As suggested yesterday, I'm splitting my question into two parts, although I think they might be connected:
I have an Excel-macro that basically works, but it gets slower the more sheets are added by the macro. when I create only a few sheets (~100) it's ok but sometime it creates up to a few hundred sheets and every sheet is a different report, and I have to keep all the sheets. An then the issue starts.
Afer the macro creates all those sheets, I do tings like sorting an printing. But before the macro continues with this tasks it takes a very long time, depending on how many sheets I've just produced.
A few years ago I had an issue with a slow macro. Then I found the hint with a forced pause. I've tried it with this macro again, and it improved the speed by a huuuge amount of time. How come a pause speeds up a macro?
The code is a bit longer so I've reduced it to address the issue and marked it in the code:
Sub My_Issues()
Dim ColumnLetter As String, item As String
Dim cell As Range
Dim sheetCount As Integer, TotalRow As Integer, TotalCol As Integer
Dim uniqueArray As Variant
Dim lastRow As Long, x As Long
Application.ScreenUpdating = False
'Get unique brands:
With Sheets("Brand")
.Columns(1).EntireColumn.Delete
Sheets("Sales").Columns("R:R").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("A1"), Unique:=True
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If .Range("A3:A" & lastRow).Cells.Count = 1 Then
ReDim uniqueArray(1, 1)
uniqueArray(1, 1) = .Range("A3")
Else
uniqueArray = .Range("A3:A" & lastRow).Value
End If
End With
TotalRow = Sheets("Sales").UsedRange.Rows.Count
TotalCol = Sheets("Sales").UsedRange.Columns.Count
ColumnLetter = Split(Cells(1, TotalCol).Address, "$")(1) 'Num2Char
sheetCount = 0 'Counter for statusbar
For x = 1 To UBound(uniqueArray, 1)
item = uniqueArray(x, 1) 'item=Brand
'Filter sales for each brand:
With Sheets("Sales")
.Range(.Cells(2, 1), .Cells(TotalRow, TotalCol)).AutoFilter Field:=18, Criteria1:=item
End With
With Sheets("Agents")
'Delete old...
.Range(.Cells(2, 1), .Cells(2, 1).End(xlDown)).Clear
'...and get new
Sheets("Sales").Range(Sheets("Sales").Cells(3, 2), Sheets("Sales").Cells(2, 2).End(xlDown)).SpecialCells(xlCellTypeVisible).Copy
.Range("A2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
'List with all agents
For Each cell In Worksheets("Agents").Range("A2", Worksheets("Agents").Range("A1").End(xlDown))
With Sheets("Report")
.Range("I4") = cell 'Copy agent and update the formulas within the report
.Range(.PageSetup.PrintArea).Copy
Sheets.Add After:=Sheets("Report")
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value 'Replace all formulas with values
Application.CutCopyMode = False
ActiveSheet.Name = cell
sheetCount = sheetCount + 1
If sheetAnz Mod 10 = 0 Then Application.StatusBar = sheetAnz 'Get statusupdate every 10 sheets
End With
Next
'->Issue: I create up to 400 sheets and when I want to continue and do some sorting of the sheets for example it takes a very long time.
'But if I add this break for a second, it works reasonably fine again. Why is that? Does vba needs the break to catch up with itself?
'Since the issue is not the sorting and the other stuff after the pause.
Application.Wait (Now + TimeValue("0:00:01"))
'Continue with other stuff.... sorting sheets and so on
Next
Application.ScreenUpdating = True
End Sub
Any ideas on this issue?

Resources