Hi I am new to VBA and have hit a wall. Tried piecing together snippets of code with the little I understand but think I am over my head. I would greatly appreciate any help constructing a block of code to achieve the following goal:
In the following worksheet
I am trying to loop through column A and identify any blank cells.
If the cells are blank I would like to copy the values in the range of 4 cells adjacent to the right of the blank cell in column A. For example: if loop identified A2 as blank cell then the loop would copy the values in range("B2:E2")
From here I would like to paste the values below the copied range to only the rows that are not blank in column A. For example: The loop would identify not blank rows in column A as ("A3:A9") and paste data below the copied range to range ("B3:E9")
The loop would stop at the next blank row in column and restart the process
Here is a screen shot of the data:
Here is what I have so far, sorry its not much Thanks in advance!
Sub select_blank()
For Each Cell In Range(ActiveCell, ActiveCell.End(xlDown))
If IsEmpty(ActiveCell.Value) = True Then
ActiveCell.Offset(, 1).Resize(, 5).copy
End If
Next
End Sub
Your code only needs a few tweaks (plus the PasteSpecial!) to get it to work:
Sub select_blank()
Dim cel As Range
With ActiveSheet
'specify that the range to be processed is from row 2 to the
'last used cell in column A
For Each cel In .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
If IsEmpty(cel.Value) Then
'If the cell is empty, copy columns B:F
cel.Offset(, 1).Resize(, 5).Copy
Else
'If the cell is not empty, paste the values previously copied
'NOTE: This relies on cell A2 being empty!!
cel.Offset(, 1).PasteSpecial
End If
Next
End With
Application.CutCopyMode = False
End Sub
I cannot make much sense of what you want, it seems to contradict itself. But, since I highly doubt anyone else is going to help you with this (per the rules), I'll give you a much better start.
Sub Test()
Dim nRow As Integer
nRow = 1
Do Until Range("A" & nRow) = "" And Range("A" & nRow + 1) = ""
If Range("A" & nRow) = "" Then
' do stuff here in the loop
End If
nRow = nRow + 1
Loop
End Sub
Sub copyRange()
Dim rngDB As Range, vDB, rng As Range
Set rngDB = Range("a2", Range("a" & Rows.Count).End(xlUp))
For Each rng In rngDB
If rng = "" Then
vDB = rng.Offset(, 1).Resize(1, 4)
Else
rng.Offset(, 1).Resize(1, 4) = vDB
End If
Next rng
End Sub
Related
I have two Sheets, sheet1 and sheet 2.
I am looking into column T of sheet1 and pasting the complete row if T contains 1 in sheet 2.
The code, works good, but it paste the result in sheet2 in the same row in sheet1.
This results in blanks, between the rows. Can anyone suggest, what i should Change with my code, so that i get them in sequence without any blank rows.
Also, how can I copy the Header in row 1 from sheet 1 to sheet2?
Sub Test()
For Each Cell In Sheets(1).Range("T:T")
If Cell.Value = "1" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets(2).Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets(1).Select
End If
Next
End Sub
There's no need to use Select and Selection to copy paste, it will only slows down your code's run-time.
Option Explicit
Sub Test()
Dim Cell As Range
Dim NextRow as Long
Application.ScreenUpdating = False
For Each Cell In Sheets(1).Range("T1:T" & Sheets(1).Cells(Sheets(1).Rows.Count, "T").End(xlUp).Row)
If Cell.Value = "1" Then
NextRow = Sheets(2).Cells(Sheets(2).Rows.Count, "T").End(xlUp).Row
Rows(Cell.Row).Copy Destination:=Sheets(2).Range("A" & NextRow + 1)
End If
Next
Application.ScreenUpdating = True
End Sub
Not For Points
Apologies, but I couldn't stop myself from posting an answer. It pains me when I see someone wanting to use an inferior way of doing something :(
I am not in favor of looping. It is very slow as compared to Autofilter.
If you STILL want to use looping then you can make it faster by not copying the rows in the loop but in the end in ONE GO...
Also if you do not like living dangerously then always fully qualify your object else you may end up copying the wrong row.
Option Explicit
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Dim lRow As Long, i As Long, r As Long
Dim copyRng As Range
Set wsI = Sheet1: Set wsO = Sheet2
wsO.Cells.Clear
'~~> first available row in sheet2
r = 2
With wsI
lRow = .Range("T" & .Rows.Count).End(xlUp).Row
'~~> Copy Headers
.Rows(1).Copy wsO.Rows(1)
For i = 1 To lRow
If .Range("T" & i).Value = 1 Then
If copyRng Is Nothing Then
Set copyRng = .Rows(i)
Else
Set copyRng = Union(copyRng, .Rows(i))
End If
End If
Next i
End With
If Not copyRng Is Nothing Then copyRng.Copy wsO.Rows(r)
End Sub
Screenshot
I am a VBA newbie...
I am looking for value "A" in column A. I would then like to use the row number which value "A" is located at, and copy the existing function in Column F into Column E.
This is what I tried and which clearly does not work...
Dim A_Row As Long
A_Row = Application.WorksheetFunction.Match("A", Range("A:A"), 0)
Range("E" & A_Row).Select
ActiveCell.Select
ActiveCell.Offset(0, 5).Select
Selection.Copy
ActiveCell.Offset(0, -1).Select
ActiveSheet.Paste
Thank you in advance for your help!
In my opinion, if you are going to use vba then avoid using worksheet functions unless totally necessary.
Sub caroll()
Dim ws As Worksheet
Dim A_row As Long
Dim rng As Range
Set ws = ActiveSheet
'Loop through column A
For Each rng In ws.Range(ws.Cells(1, 1), ws.Cells(ws.Rows.Count, 1).End(xlUp))
'Test whether cell = "A","B", or "Z"
If VarType(rng) <> vbError Then
If rng.Value = "A" Or rng.Value = "B" Or rng.Value = "Z" Then
'If true copy column F of that row into Column E
rng.Offset(, 5).Copy rng.Offset(, 4)
End If
End If
'loop
Next rng
End Sub
I have written a script which checks a range of cells in column range 4 (Column D) for non-blank values, if it finds a non blank value, it copies that value and pastes it to a cell in column range 6 (Column F). The script runs, but it is awfully slow, the script takes 5 minutes to process and complete its run. Is there any way to improve this script so that it can pre-check the range before it copies and pastes the values across? It seems that the copy / paste function is slowing it down.
Code below
Sub ArrayCopyPaste()
Dim J as Integer
Application.Calculation = xlCalculationManual
For J = 2 To 500
If Cells(J, 4).Value <> "" Then
Cells(J, 4).Copy
Cells(J, 6).PasteSpecial Paste:=xlPasteValues
End If
Next J
Application.Calculation = xlCalculationAutomatic
End Sub
Here's one way:
Sub test()
Dim r1, r2, n As Long
With Sheets("Sheet1") '~~> change to suit
Dim lrow As Long
lrow = .Range("D" & .Rows.Count).End(xlUp).Row
r1 = Application.Transpose(.Range("D2:D" & lrow))
r2 = Application.Transpose(.Range("F2:F" & lrow))
For n = LBound(r1) To UBound(r1)
If r1(n) <> "" Then r2(n) = r1(n)
Next
.Range("F2:F" & lrow) = Application.Transpose(r2)
End With
End Sub
Transfer range data to array, then do the comparison process array to array.
Then return the array to range. HTH.
Important: Application.Transpose have limitation. I can handle only a few thousand data.
Follow up: Try this for deleting
Dim rngToDelete As Range, k As Long
With Sheets("Sheet1") '~~> change to suit
For k = 2 To 500
If .Cells(k, 6).Value = "" Then
If rngToDelete Is Nothing Then
Set rngToDelete = .Cells(k, 6)
Else
Set rngToDelete = Union(rngToDelete, .Cells(k, 6))
End If
End If
Next
rngToDelete.Delete xlUp
'rngToDelete.EntireRow.Delete xlUp ~~> use this if you want to delete entire row.
End With
Determine all the target range first then delete in one go. HTH.
Try simply doing this first and see if it makes a difference:
Dim currentCalculation As Variant
currentCalculation = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
For J = 2 To 500
If Cells(J, 4).Value <> "" Then
Cells(J, 4).Copy
Cells(J, 6).PasteSpecial Paste:=xlPasteValues
End If
Next J
Application.ScreenUpdating = True
Application.Calculation = currentCalculation
Another thought. Have you tried just doing this?
For J = 2 To 500
If Cells(J, 4).Value <> "" Then
Cells(J, 6).Value = Cells(J, 4).Value
End If
Next J
It won't make any difference to your target column if the blanks are copied, so don't bother checking for them. Don't loop -- just copy the entire column.
Sub CopyColumn()
' copying this way does not use your clipboard
Columns("D").Copy Columns("F")
End Sub
If you only need a portion of the column, specify the range to copy rather than the entire column:
Sub CopyPartOfColumn()
' copying this way does not use your clipboard
Range("D2:D500").Copy Range("F2:F500")
End Sub
You mention in a comment below your question that you want the resulting column to be a consolidated list of the values without the blanks. You can quickly do that by deleting the blanks from the column or range, once again without looping. Run this after you've copied the values you want.
Sub RemoveBlanks()
Range("F2:F500").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
End Sub
I have been trying to create a macro that will go through a spreadsheet and copy figures in Cells E, then paste them into Cell K and L, then repeat as the macro transverse column E. i.e. E1 will be copied to K1 and L1, E2 will be copied to K2, L2 etc...
This is what i have done so far:
Sub uy()
'
' Macro1 Macro
' lo
'
Range("E299").Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value < 0 Then
Selection.Copy
Range("K299").Select
Do Until IsEmpty(ActiveCell)
ActiveSheet.Paste
Loop
Range("L299").Select
Do Until IsEmpty(ActiveCell)
ActiveSheet.Paste
Loop
Else
Range("L299").Select
Do Until IsEmpty(ActiveCell)
ActiveSheet.Paste
Loop
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
When i run the macro it just highlights cell E229 with a broken line box and Cells K299, L299 are left blank. I feel Range("K299").Select, Do Until IsEmpty(ActiveCell), ActiveSheet.Paste part is selecting and copying a blank cell, so it will terminate itself as it meets the "Do Until IsEmpty(ActiveCell)" criteria.
Is there a way for me to fix this?
I'm not quite sure if i got you right.
but if you want to just copy one range to an other then
this would do it.
Private Sub CommandButton1_Click()
For i = 1 To Cells(Rows.Count, 5).End(xlUp).Row
If Cells(i, 5) <> "" Then
Cells(i, 11).Value = Cells(i, 5).Value
Cells(i, 12).Value = Cells(i, 5).Value
End If
Next i
End Sub
This marco works as long as cell 'i' in column E isn't empty
and takes the value of cell 'i' in column E and puts it into column K and L
Kind regards
First, don't use Activate or Select. They're 99% useless in most code. Next, don't use copy and paste. It's slow for this kind of approach.
The following code is much more streamlined and faster.
Sub EtoKL()
Dim WS As Worksheet
Dim LRow As Long, Iter As Long
Set WS = ThisWorkbook.Sheets("Sheet1") 'Change as necessary.
With WS
LRow = .Range("E" & .Rows.Count).End(xlUp).Row 'Get last used row in Column E.
For Iter = 1 To LRow 'Iterate from 1 to last used row.
Union(.Range("K" & Iter), .Range("L" & Iter)).Value = .Range("E" & Iter).Value
Next
End With
End Sub
Let us know if this helps.
I think something like this would work.
Sub Copy()
Dim intRowCount as Long
Dim intLastRow as Long
intRowCount = 2
intLastRow = Application.CountA(Sheets(1).Range("e:e"))
For intRowCount = 2 To intLastRow
Sheets(1).Range("K" & intRowCount ).Value = Sheets(1).Range("E" & intRowCount ).Value
Sheets(1).Range("L" & intRowCount ).Value = Sheets(1).Range("E" & intRowCount ).Value
Next
End Sub
Private Sub CommandButton1_Click()
Dim rCell As Range
Dim i As Long
Dim rNext As Range
'loop through the cells in column A of the source sheet
For Each rCell In Sheet1.Range("A3:U25")
'loop as many times as the value in column U of the source sheet
For i = 1 To rCell.Offset(0, 22).Value
'find the next empty cell to write to in the dest sheet
Set rNext = Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(1, 0)
'copy A and B from source to the dest sheet
rCell.Resize(1, 22).Copy rNext.Resize(1, 1)
Next i
Next rCell
End Sub
Ok this works great except how do I copy the values not formulas of cells in sheet1 to sheet2? Like a date transfers as 1/0/1900, when it needs to be 5/5/2011
You need to use the PasteSpecial method with the xlPasteValues as the PasteType. Something like:
Sheet2.Cells(1,1).PasteSpecial xlPasteType.xlPasteValues
Private Sub CommandButton1_Click()
Dim rCell As Range
Dim i As Long
Dim rNext As Range
'loop through the cells in column A of the source sheet
For Each rCell In Sheet4.Range("A3:U25")
'loop as many times as the value in column U of the source sheet
For i = 1 To rCell.Offset(0, 23).Value
'find the next empty cell to write to in the dest sheet
Set rNext = Sheet12.Cells(Sheet12.Rows.Count, 1).End(xlUp).Offset(1, 0)
'copy A and B from source to the dest sheet
rCell.Resize(1, 23).Copy
rNext.Resize(1, 1).PasteSpecial (xlPasteValues)
Next i
Next rCell
End Sub
Now I'm getting a runtime-13 type mismatch on below part of the code. When it errors, click end and it works fine. Don't want to have to click end.
For i = 1 To rCell.Offset(0, 23).Value